Call JavaScript function from Codebehind in ASP.Net

Call JavaScript function from Codebehind

Here, we learn how to call javascript function from codebehind in asp.net with c#.
We need to first know about javascript, Javascript is a client side scripting language there are many javascript events and javascript function.
Generally we  write the javascript function in Head section of asp.net web forms, but here we will write javascript function at the code behind asp.net web forms.
we will here use onclick event in javascript function and call this onclick event in javascript to asp.net button server control click event.
Write the below code on  Button Click event at the asp.net web forms codebehind page.

protected void Button1_Click(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(GetType(), “Msg”, “callalert(‘” + TextBox1.Text.Replace(“‘”, “\’”) + “‘);”, true);
}

 

We can write JavaScript function in Codebehind file also like this…..

protected void Button1_Click(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(GetType(), “Msg”, ““);
}

I hope this onclick event in javascript function call from codebehind in asp.net will help you……

Leave a Reply

Your email address will not be published. Required fields are marked *

6 + 2 =


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">