使用CONFIRM获取用户输入

时间:2011-10-12 16:30:55

标签: c# javascript asp.net

我需要让用户点击OK或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 += ";";
        }
    }

这是javascript:

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

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

                                else

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

如何在代码隐藏代码内部按钮上运行此函数ConfirmWithUser(),如上所示:

我需要的另一个词:

  1. 用户点击按钮后,代码隐藏的第一部分执行
  2. codebehind exeuctes里面的javascript
  3. 执行后面的代码的最后一部分

2 个答案:

答案 0 :(得分:2)

没有名为confirmationMessage的Javascript变量 您需要调用HttpUtility.JavaScriptStringEncode并将服务器端变量的值作为Javascript字符串文字插入。

然而,你的设计不可能奏效; if (HiddenFieldUserConfirmed.Value != "no")将在Javascript客户端代码之前运行(在服务器上)。

您需要了解HTTP和客户端/服务器分离的工作原理。

答案 1 :(得分:1)

从代码中运行代码的位置并不清楚。

您需要在!ispostback部分的page_load中注册脚本。然后,您需要检查隐藏值以响应按钮单击处理程序。

修改后的详细信息

您需要将创建javascript消息的代码移至page_load&使用javascript函数注册onclientclick处理程序。然后,当您单击按钮,填充隐藏字段,然后提交要在服务器端处理的表单时,将触发该javascript。

相关问题