Monday, May 18, 2009

Calling C# Method from Inline JavaScript

This Post will help in calling a C# method from Inline JavaScript.

Inline Javascript:

string strMessage = "Some message";
StringBuilder sb = new StringBuilder();
sb.Append("< language="'javascript'">"); //Remove the in between space of script tag
sb.Append("var results = confirm('" + strMessage + "');");
sb.Append("if (results == true)");
sb.Append("document.getElementById(\"btnHdn\").click();"); //call methodTrue
sb.Append(" < /" + "script >"); //Remove the in between space of closing script tag
this.RegisterStartupScript("startup", sb.ToString());

When "Ok" is clicked from Confirm dialog box. btnHdn button's Click event gets fired.

Note:

1. Define the button btnHdn as mentioned below,

< id="btnHdn" runat="server" onclick="btnHdn_Click">

2. Instead of making button Hidden, use the below properties to show & hide a button

btnHdn.Attributes["Style"] = "display:none"; //To Hide button
btnHdn.Attributes["Style"] = "display:block"; //To display button

No comments:

Post a Comment