Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
I am using following code to create javascript popup in C# code and want to save reply of user (OK/Cancel) in some C# variable. If you have any solution, than please post it here.

JavaScript
Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language='javascript'> confirm('This is confirm popup')</script>");


Thank you
Posted
Updated 28-Nov-15 15:59pm
v2

1 solution

One idea can be to put a hidden field in the form and set the value on user responding to the confirm box.
Lets say, the hidden field is -
ASP.NET
<asp:hiddenfield id="hdnConf" runat="server" xmlns:asp="#unknown" />

Now, let's modify your code accordingly.
C#
Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language="'javascript'">if (confirm('This is confirm popup')) document.getElementById('<%=hdnConf.ClientID%>').value='true'; else document.getElementById('<%=hdnConf.ClientID%>').value='false';</script>");

//get confirmation value
bool confResult=bool.Parse(hdnConf.Value.ToString());


This is purely untested code and hence you may need to modify a little.

Hope, it helps :)
 
Share this answer
 
v3
Comments
Member 11656452 28-Nov-15 22:51pm    
Thank you for your reply.
But this code is not working. Because value of hidden field is not updated using this code. When we write such code in script tag on aspx page, than this code works but when we write this code on C# page than value of hidden field is not modified.
Krunal Rohit 29-Nov-15 0:59am    
Okay, don't store it in a hidden field. Rather use the label & use style="display: none", then store the value in it like solution suggests. It's just a wild guess, but you can give it a shot.

-KR

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