Thursday, 15 November 2012

Calling Javascript using Server-Side Controls


Calling Javascript using Server-Side Controls


One can call JavaScript client side function directly from code behind using RegisterStartupScript. If one wants JavaScript to be embedded into codebehind, then make use of RegisterClientScriptBlock:

<script>

function fnShowMessage()

{

       alert(" Invoke Javascript function from Server Side Code Behind ");

}

</script>

You can call the above Javascript Function using the following codes in the Button Click Event:


protected void Button1_Click(object sender, EventArgs e)

{

        ClientScript.RegisterStartupScript (GetType(),"Javascript", "javascript: fnShowMessage(); ",true);

}

or,


protected void Button2_Click(object sender, EventArgs e)

{

       ClientScript.RegisterClientScriptBlock(GetType(), "Javascript",  "<script>alert('Record Added Successfully')</script>");

}

No comments:

Post a comment

Thank You for Your Comments. We will get back to you soon.

back to top