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
}


No comments:

SQL: Generate a range of numbers

SELECT ones.n + 10*tens.n + 100*hundreds.n + 1000*thousands.n FROM       (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) ones(n),      (VALU...