Click here to Skip to main content
15,896,444 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
Hi All,

How can i fire a javascript function at the end of a asp button click event.

My code as follows:

protected void btnexamsubmit_Click(object sender, EventArgs e)
{
//here some server codes
.
.
//End server code

//here Fire javascript function 'ShowConfirm'

}

Java Scrit: in ma .aspx
XML
<script type="text/javascript" language="javascript">
      function ShowConfirm()
      {
      confirm("Welcome");
      }
   </script>



Does this is a better flow of code or any other way to do the Same.I am very new to javascript
Please kindly assist.

Thank you in advance.

Best Regards,

Shafeequl
Posted
Updated 16-Jun-13 20:32pm
v2

You can call a javascript function from code behind by using ScriptManager Class[^].
C#
protected void btnexamsubmit_Click(object sender, EventArgs e)
{
    //here some server codes
    //....
    //End server code
    //..
    //here Fire javascript function 
    ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "Script1", "ShowConfirm();", true);
}


But javascript confirm box will not work in this case. For server side confirmation you can try one of the following:
Server Side (Code Behind) Yes No Confirmation Message Box in ASP.Net[^]
A Simple ASP.NET Server Control: Message Box & Confirmation Box[^]

Also check a similar thread : Confirmation Message Box in asp.net code behind[^]


--Amit
 
Share this answer
 
Comments
Shafeequl 17-Jun-13 2:47am    
Thanks for your replay sir
does alert or prompt box work with the above code?
_Amy 17-Jun-13 2:54am    
Yes sure.. It'll work..
Shafeequl 17-Jun-13 3:12am    
changed 'confirm' to 'alert' but doesnt fire the javascript,no error
_Amy 17-Jun-13 3:15am    
Try this:
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "Script1", "alert('Hii, I am an alert box');", true);
Shafeequl 17-Jun-13 4:25am    
sir can u pls give me some more references about this...The above references seems unusefull to ma case..please help me..
Please refer below code
protected void btnexamsubmit_Click(object sender, EventArgs e)
     {
         string script = "<script>ShowConfirm()</script>";
         ClientScript.RegisterStartupScript(this.GetType(),"", script);
     }
 
Share this answer
 
first take script manager on your aspx page

XML
<asp:ScriptManager ID="ScriptManager1" runat="server">
  </asp:ScriptManager>


after use the following server side code

C#
protected void btnexamsubmit_Click(object sender, EventArgs e)
{
//here some server codes
.
.
//End server code

//here Fire javascript function 'ShowConfirm'

   ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "test", "ShowConfirm();", true);

}
 
Share this answer
 
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "test", "ShowConfirm();", true);
 
Share this answer
 
In your code behind where you want to fire javascript write the following line

Response.Write(@"<script language='javascript'>Confirm('Welcome');</script>");
 
Share this answer
 
Create a javascript function:

function GetData()
{
alert("hello......!!!");
}

now, write code on button click event:

protected void Button1_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Click", "GetData();", true);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900