How to get prompt value in ASP.NET?

What I want to do is when the user enters a text in the prompt box and presses the OK button, the prompt box will send back the value to string prmt; and if they cancelled the prompt, it will do nothing.

Codes:

string prmt; 
if(ren>=1)
{

    ClientScript.RegisterStartupScript(this.GetType(), "prompt", "prompt('Enter your message here.')", true);

//if(ok){}
//if(cancel){}

}

Sorry for my grammar.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

For this problem, the best solution I can think of would be AJAX, but there is an another way. Create a hidden field in the HTML file like this:

<form id="theform" runat="server">
        <input type="hidden" id="hidValue" runat="server" />
</form>

Now, the next thing that should be done is to add a JavaScript block to your HTML file. This snippet will get the prompt’s value and store it in the hidden field that we had created before. Something like this will probably work:

<script type="text/javascript">
        function storeinput(value) {
            document.getElementById("<%=hidValue.ClientID%>").value = value;
        }
</script>

This has created a storeinput function, which when called will set the hidden field’s value to the provided value argument. Next thing we need to do is to wire them together with the ASP.NET.
Instead of

ClientScript.RegisterStartupScript(this.GetType(), "prompt", "prompt('Enter your message here.')", true);

Do this:

ClientScript.RegisterStartupScript(this.GetType(), "prompt", "var value = prompt('Enter your message here.'); storeinput(value);", true);

Or something like that (I am not really experienced with ASP.NET, but I guess that will work fine). And after you just simple check the hidValue field’s value using your favourite ASP.NET way and you are good to go.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x