Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
1.60/5 (2 votes)
See more:
Here I want to call javascript confirm from c# button OnClick event. Please help me.

C#
protected void btnSave_Click(object sender, EventArgs e)
{
string Confirmvalu=string.Empty;
bool check = checkattachments();//Returns bool value
if (check)
{
//Then I Would like to Call Javascript Confirm 
//And based on confirm value i.e. Confirmvalu, I have to do something like       
if(Confirmvalu=="Yes")
{ 
//Do Something1
}
else
{
//Do Something2
}
}
else
{
//Javascript no need
}
}


What I have tried:

Please help me to resolve this
Posted
Updated 7-Jul-16 4:22am

Better to add code in Html designer as below.

ASP.NET
<asp:button id="Button1" xmlns:asp="#unknown">
           Text="Submit"
           onclick="return confirm('Are you sure');"
           runat="server"/></asp:button>
 
Share this answer
 
Agreed you should code like this in 4.0:-

ASP.NET
<asp:button onclientclick="return confirm('Are you sure, you want to continue?');" runat="server" id="btnTest" onclick="btnTest_Click" text="Click Here" xmlns:asp="#unknown" />


As you can see in latest version we are getting both "onclientclick" and "onclick" events to handle. "onclientclick" will handle the client side click event.

So on click first the clientside code that will show the confirm box, if you click ok it will move next to serverside code where you do whatever you wanted to do or else this will cancel the server side click event by returning false.

Hope this will be of help to you.
 
Share this answer
 
v2
Comments
Matsya Ganesh Vegi 7-Jul-16 9:13am    
I don't want to do with onclientclick. I just want to do in if condition.

If my "check" value is true then only i want confirm box. otherwise I don't want any confirm or alert. If I use onclientclick confirm will come in each click , i don't want it.
F-ES Sitecore 7-Jul-16 10:37am    
You can't do it in the "if" function, you need to take time to understand the asp.net page lifecycle. Your server code runs in its entirety and once finished all generated html is sent to the client and only then can you do client things like run javascript, so you can't run js as your server code is running, it just doesn't work like that.

Instead you need to split your code into two steps, first you do the prep for the action and show a confirm box, and after the user has confirmed you then do the next step which is the action itself. See "Basic" and "BasicDialog" on the link Karthik posted to understand the basics, then maybe look at the PurAjax version.
SRS(The Coder) 7-Jul-16 9:36am    
but in the case if you register the script to page, the confirm message will display after the page gets loaded next time.
Matsya Ganesh Vegi 7-Jul-16 9:39am    
Ya, That's what I found. But that's not my requirement. I have spent too much time on it. But still researching.
SRS(The Coder) 8-Jul-16 2:14am    
it is difficult to achieve the way you want to get it done, because as told by "F-ES Sitecore" it is against the usual flow how asp.net works...
also there is not confirmation message can be done directly in asp.net,a s it does not support the same, rather you can try some third party or do the operation using ajax.

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