How to get prompt value in ASP.NET?

后端 未结 1 1183
暖寄归人
暖寄归人 2021-01-13 18:24

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 the

相关标签:
1条回答
  • 2021-01-13 19:07

    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.

    0 讨论(0)
提交回复
热议问题