Calling Server Side Function Using Javascript
Many of us needs a server side function to be called from JavaScript. We
can do this easily using __doPostBack
as
Javascript Code
function CallServerCode() { __doPostBack('btnTest', ''); }
and in HTML
<button type="button" onclick="javascript:CallServerCode();">Call Server Code</button> <asp:linkbutton id="btnTest" runat="server" text="ASP .Net Button"> </asp:linkbutton>
and the Server side code
Protected Sub btnHidden_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnTest.Click Response.Write("Hello") End Sub
Now note that whenever the client button with caption "Call Server Code"
will be clicked it will call Serverside event of btnTest using Javascript.
In the head section:
In the body section:
In the CS part :
Alternative Approach:
In design :In the head section:
<script type="text/javascript" language="javascript"> function callme(chkCall) { var chkCall=document.getElementById(chkCall); if(chkCall.checked==true) { chkCall.checked=true; PageMethods.GetContactName(); } else { chkCall.checked=false; } } </script>
In the body section:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" /> <asp:CheckBox ID="chkCall" runat="server" />
In the CS part :
protected void Page_Load(object sender, EventArgs e) { chkCall.Attributes.Add("onclick", "javascript:callme('" + chkCall.ClientID + "')"); } [System.Web.Services.WebMethod] public static void GetContactName() { Response.Write("your name"); }
No comments:
Post a Comment
Thank You for Your Comments. We will get back to you soon.