Thursday 15 November 2012

Calling Server Side Function Using Javascript

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

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


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.

Alternative Approach:

In design :
In the head section:

 <script type="text/javascript" language="javascript">

    function callme(chkCall)


       var chkCall=document.getElementById(chkCall);












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 + "')");



    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.

back to top