Saturday, 12 August 2017

Add HTML Button to Page from Code Behind with Click Event

Page:
<div id='PlaceHolder' runat='server'></div>

<script type="text/javascript">
$('.className').on('click', function (event) {
event.stopPropagation();
event.stopImmediatePropagation();

$.ajax({
type: "POST",
url: "DesirePage.aspx/DesireMethod",
data: '{object: "' + this.id + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
if (response.d) {
window.location = (response.d).ToString();
}
else {
alert('Not Successfull');
}
},
failure: function (response) {
alert('Failed');
}
});
});
</script>



Code Behind:
public static void Test()
{
Button btn = new Button();
btn.ID = empRow[0].ToString().Trim();
btn.Text = "click here";
btn.Attributes.Add("class", "className");
StringBuilder sb = new StringBuilder();
StringWriter writer = new StringWriter(sb);
HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
cloneButton.RenderControl(htmlWriter);

PlaceHolder.InnetHtml = sb.ToString();
}


[System.Web.Services.WebMethod]
public static object DesireMethod (object input)
{
// do some operation and return result
}


Saturday, 5 August 2017

C# Split a List into Sub-Lists and run in Parallel

static void Main(string[] args)
{
    // create a dummy list
    List<string> data = GetTheListOfData();
  
    // split the list into sub-lists - in this case N items in each sub-list
    List<List<string>> subLists = splitList<string>(data, N);

    // get each list from sub-lists
    foreach (List<string> list in subLists)
    {
        // parallel operation on each element on selected list
        Parallel.ForEach(list, element =>
  {
            Console.WriteLine(element);
  });
    }  
}

// split the list into sub-lists 
public static List<List<string>> splitList<T>(List<string> bigList, int numElementsInEachSubList)
{
    var list = new List<List<string>>();

    for (int i = 0; i < bigList.Count; i += numElementsInEachSubList)
        list.Add(bigList.GetRange(i, Math.Min(numElementsInEachSubList, bigList.Count - i)));

    return list;
}

Parallel.ForEach uses some intelligence to decide how many parallel threads to run simultaneously, with a max of 63. To set the max degree of parallelism, add 
new ParallelOptions { MaxDegreeOfParallelism = 5 } 
as the second argument to Parallel.ForEach

Parallel.ForEach(list, new ParallelOptions { MaxDegreeOfParallelism = 10 }, element =>
{
  Console.WriteLine(element);
});


Monday, 31 July 2017

HTTP Error 503 When Enabling 32-Bits Application in IIS

When enabling the 32-bits application mode in IIS, the application pool stops and and generate HTTP Error 503 indicating that the service is not available.

The problem might be due to corrupted IIS URL Rewrite module or incompatibility of the versions due to server upgrade. To resolve this issue, un-install the URL Rewrite module and re-install it again.


Resolve HTTP Error 500.19 - 0x8007007e

To fix this, disable the WSUS compression by running the following command on the server:

%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /-[name='xpress']

To enable WSUS compression again by running this command:

%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /+[name='xpress',doStaticCompression='false',dll='%windir%\system32\inetsrv\suscomp.dll']


Wednesday, 28 June 2017

SQL Server - HTML to TEXT

CREATE FUNCTION [dbo].[HTMLtoText] (@HTMLText VARCHAR(MAX))
RETURNS VARCHAR(MAX) AS

BEGIN
DECLARE @Start INT
DECLARE @End INT
DECLARE @Length INT
SET @Start = CHARINDEX('<',@HTMLText) SET @End = CHARINDEX('>',@HTMLText,CHARINDEX('<',@HTMLText)) 
SET @Length = (@End - @Start) + 1 WHILE @Start > 0
AND @End > 0
AND @Length > 0
BEGIN
SET @HTMLText = STUFF(@HTMLText,@Start,@Length,'')
SET @Start = CHARINDEX('<',@HTMLText) SET @End = CHARINDEX('>',@HTMLText,CHARINDEX('<',@HTMLText))
SET @Length = (@End - @Start) + 1
END
  RETURN LTRIM(RTRIM(@HTMLText))
END
GO

Command to Test:
SELECT dbo.HTMLtoText('<h1>Everything is possible, nothing is <span style="color: red"><b>impossible.</b> </span>:)</h1>')


Tuesday, 27 June 2017

C# ASP.NET Rich Text Box/Editor

The most-used free ASP.NET WYSIWYG HTML editor featured in open source and commercial projects more...


Friday, 26 May 2017

Register ASP.NET 4.x

Dialog box may be displayed to users when opening projects in Microsoft Visual Studio after installation of Microsoft .NET Framework 4.6 more...


Analysis Service in Power BI Report Server

 We couldn’t connect to the Analysis Services server. Make sure you’ve entered the connection string correctly... link