简体   繁体   English

使用CONFIRM获取用户输入

[英]Getting user input with CONFIRM

i need to get the user to click OK or CANCEL: 我需要让用户单击“确定”或“取消”:

protected void Button1_Click(object sender, EventArgs e)
    {
        CallRecursive(TreeView1);

        string confirmationMessage;
        confirmationMessage = @"Please review the data before submitting:" +  "\r\n"
        + "Sample Received Date: " + received_dateTextbox.Text + "\r\n"
        + "Site of Ocurrence: " + site_of_occurrenceTextBox.Text + "\r\n"
        + "Occurrence Date: " + occurrence_dateTextBox.Text + "\r\n"
        + "Report Date: " + report_byTextBox.Text + "\r\n"
        + "Specimen ID: " + spec_idTextBox.Text + "\r\n"
        + "Batch ID: " + batch_idTextBox.Text + "\r\n"
        + "Report Initiated By: " + report_byTextBox.Text + "\r\n"
        + "Problem Identified By: " + RadioButtonList1.SelectedValue + "\r\n"
        + nodetexts;

        HiddenFieldConfirmation.Value = confirmationMessage;

************i need function ConfirmWithUser() to run here from javascript*************


            if (HiddenFieldUserConfirmed.Value != "no")
        {
            SubmitData();

            CallRecursive(TreeView1);
            nodetexts += ";";
        }
    }

here is the javascript: 这是JavaScript:

function ConfirmWithUser() {
            if (confirm(document.getElementById('HiddenFieldConfirmation').value) == true)

                            { document.getElementById('HiddenFieldUserConfirmed').value='yes'; }

                                else

                            { document.getElementById('HiddenFieldUserConfirmed').value='no';}

how do i run this function ConfirmWithUser() on the button click inside of the code behind code as shown above: 我如何在按钮上运行此功能ConfirmWithUser() ,在代码背后的代码内部单击,如上所示:

another words i need: 我需要的另一句话:

  1. after user clicks button, first part of codebehind execeutes 用户单击按钮后,将执行代码的第一部分
  2. javascript inside of codebehind exeuctes 后台代码背后的javascript
  3. the last part of code behind executes 后面的代码的最后一部分执行

There is no Javascript variable named confirmationMessage . 没有名为confirmationMessage Javascript变量。
You need to call HttpUtility.JavaScriptStringEncode and insert the server-side variable's value as a Javascript string literal. 您需要调用HttpUtility.JavaScriptStringEncode并将服务器端变量的值作为Javascript字符串文字插入。

However, your design cannot possibly work; 但是,您的设计可能无法正常工作。 the if (HiddenFieldUserConfirmed.Value != "no") will run (on the server) before the Javascript client-side code. if (HiddenFieldUserConfirmed.Value != "no")将在Javascript客户端代码之前(在服务器上)运行。

You need to understand how HTTP and client/server separation works. 您需要了解HTTP和客户端/服务器分离的工作方式。

It's not clear from your code where the code is run. 从您的代码中尚不清楚代码在何处运行。

You need to register the script in the page_load inside an !ispostback section. 您需要在!ispostback部分的page_load中注册脚本。 You then need to check the hidden value in response to a button click handler for example. 然后,您需要检查隐藏值以响应例如按钮单击处理程序。

More detail after edit 编辑后更多细节

You need to move the code that creates your javascript message to your page_load & register an onclientclick handler with the javascript function. 您需要将创建javascript消息的代码移至page_load并使用javascript函数注册onclientclick处理程序。 This will then fire that javascript when you click the button, populating your hidden field and then submitting the form to be handled server side. 然后,当您单击按钮,填充隐藏字段并提交要在服务器端处理的表单时,将触发该javascript。

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

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