简体   繁体   English

无法使用ASP.NET C#中的RegisterStartupScript收集JavaScript var值

[英]Cannot collect JavaScript var value using RegisterStartupScript in ASP.NET c#

I want to collect action response using JS in ASP.NET C#. 我想在ASP.NET C#中使用JS收集动作响应。 So on an ASP.NET Button click I am doing following 所以在ASP.NET按钮上单击我正在执行的操作

Button on ASPX page ASPX页面上的按钮

<asp:Button ID="btn_add" runat="server" CssClass="button btn-entry"
                        Text="Add" OnClick="btn_add_Click"

Inside btn_add_Click , I have 在btn_add_Click里面,我有

bool continue_ = isEmpty_InputForm(input_form_index);

        if (!continue_)
        {

            Page.ClientScript.RegisterStartupScript(this.GetType(), 
                "onclick", 
                "<script type=\"text/javascript\">"
                + " var action_res = document.createElement(\"INPUT\");"
                + " action_res.type = \"hidden\";"
                + " action_res.name = \"action_res\";" 
                + " action_res.value = null;"
                + " if (confirm('Are you sure?')) {"
                + " action_res.value = 'Yes';"
                + " }"
                + " document.forms[0].appendChild(action_res);"
                //+ "confirm('Are you really sure?');"
                + " </script>", 
                false);
        }

Then to collect the value of action_res JS var I am doing following 然后要收集action_res JS var的值,我正在执行以下操作

string action_res = Request.Form["action_res"];

The page gives me the confirm popup just fine, but action_res value is always null ! 该页面为我提供了确认弹出窗口,但action_res值始终为null

I am new to JS, can someone help me spotting the error please. 我是JS新手,有人可以帮助我发现错误。

Think about it this way - if the posted page is generating that script, the server code is not waiting for the information from that confirmation dialog. 可以这样考虑-如果发布的页面正在生成该脚本,则服务器代码不会等待该确认对话框中的信息。 It's always going to be null because the server doesn't know about var action_res ; 因为服务器不知道var action_res ,所以它将始终为null; it wasn't establish before post back. 在发回邮件之前尚未确定。 Maybe handle the confirmation on the client side with JavaScript, set a hidden input up on your page ( input id="confirmation_res" type="hidden" runat="server" /> ) and call string action_res = action_res.Value; 也许使用JavaScript在客户端进行确认,在页面上设置一个隐藏的输入( input id="confirmation_res" type="hidden" runat="server" /> )并调用string action_res = action_res.Value; in your C# code? 在您的C#代码中?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM