简体   繁体   English

显示确认框,并在后面的代码中获取其值

[英]Getting a Confirmation Box to appear and get its value in the code behind

Here's what I want to do. 这就是我想要做的。 I have a form that the user fills out (creating sections and subsections) and when they click save, I want to check the database to see if they have named a section the same as one that already exists. 我有一个供用户填写的表单(创建节和子节),当他们单击“保存”时,我想检查数据库以查看他们是否已命名与已经存在的节相同的节。 If they have, I want to get a confirmation from them to let them know they wil create a duplicate if they proceed. 如果有,我想从他们那里得到确认,以使他们知道,如果继续进行下去,他们将创建副本。 If they click yes, I need to continue, else I need to abort. 如果他们单击“是”,则我需要继续,否则需要中止。 Here is some psuedocode for what I have so far. 这是到目前为止我所掌握的一些伪代码。

 protected void SaveButton_Click(object sender, EventArgs e)
{
    try
    {
        if (CheckForDuplicates())
        {
            //proceed normally
        }
    }
}

private bool CheckForDuplicates()
{
    //check database

            if (/*there are duplicates*/)
            {
                string message = "A duplicate name exists. Would you like to continue?";
                string scriptString = "<script language='javascript' 
                    type='text/javascript'>" + "return confirm('" + message + "');</script>";

                ScriptManager.RegisterStartupScript(this, this.GetType(), 
                    "script", scriptString, false);

                //here i would like to return their confirmation
            }
        }
    }
    return true;
}

All help is appreciated and thanks in advance! 感谢所有帮助,并在此先感谢!

Add Javascript such that if user confirms, you can call the JavaScript __doPostBack('','UserConfirmed'); 添加Javascript,以便用户确认时可以调用JavaScript __doPostBack('','UserConfirmed'); function. 功能。 Just add the logic in your codebehind along with your confirmation logic that you are registering with the ScriptManager . 只需在代码后面添加逻辑,以及在ScriptManager中注册的确认逻辑。 When the postback occurs, you can then check to ensure that the postback was, in fact, initiated by the user's confirmation (as opposed to some other action on the page): 回发发生时,您可以检查以确保回发实际上是由用户的确认发起的(与页面上的其他操作相反):

public void Page_Load(object sender, EventArgs e)
{
  string parameter = Request["__EVENTARGUMENT"];
  //if parameter equals "UserConfirmed"
  //       User confirmed, so do whatever
  //
}

Information on __doPostBack : Understanding the JavaScript __doPostBack Function 有关__doPostBack信息: 了解JavaScript __doPostBack函数

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

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