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);
});


Upgrade Windows 11 Home to Windows 11 Pro

Disable internet connection (Wi-Fi, Internet, etc.) Change the product key using the following Generic product key:                     VK7J...