简体   繁体   English

ASP.NET中的确认框OnClientClick

[英]Confirmation Box OnClientClick in ASP.NET

I'm trying to work on this website wherein I need to filter the data which are to be inserted into the database. 我正在这个网站上工作,我需要过滤要插入数据库的数据。 For instance, I need to insert 'Hello World' to the database. 例如,我需要在数据库中插入“ Hello World”。 However, it already exists. 但是,它已经存在。 Therefore, I need to pop-up a confirmation box saying "The string Hello World already exists in the database. Are you sure you want to continue?" 因此,我需要弹出一个确认框,指出“字符串Hello World已存在于数据库中。确定要继续吗?”

My problem is that I don't know in which place in my code should I include my confirmation message. 我的问题是我不知道在代码中的哪个位置添加确认消息。 Please see below for referrence: 请参阅下面的参考资料:

private bool CheckData(string myString)
{
    //Check database if myString already exists. 
    return;
}

private void btnSave_Click(Object sender, EventArgs e)
{
    CheckData(myString)
    //If the above is true, then the confirmation box should appear.
    //Do something to save myString to the database.
}

I'm programatically adding an OnClientClick Event handler to my button using the code below: 我正在使用以下代码以编程方式将OnClientClick事件处理程序添加到我的按钮:

btnSave.OnClientClick = "confirmation('The string " + myString + " already exists in the database. Are you sure you want to continue?')";

My problem is basically what is the best way to handle this kind of scenario? 我的问题基本上是处理这种情况的最佳方法是什么? Since I can't place this code on the btnSave_Click event (it will add the OnClientClick handler but it will only fire the next time the button was clicked). 由于我无法将此代码放在btnSave_Click事件上(它将添加OnClientClick处理程序,但只会在下次单击该按钮时触发)。

to do this via ajax, have the onClientClick call a js function that calls a .net handler to check if the file exists, and either: 要通过ajax执行此操作,请让onClientClick调用js函数,该函数调用.net处理程序以检查文件是否存在,并且:

  1. return true if no file exists so the postback occurs 如果不存在文件,则返回true,从而发生回发
  2. if the file already exists, popup a confirmation box and return the result of the confirmation box 如果文件已经存在,则弹出确认框并返回确认框的结果

otherwise, you can check if the file exists in the postback, but then you'll have to display a message and another confirm button to allow the user to overwrite the file. 否则,您可以检查回发中是否存在该文件,但随后必须显示一条消息和另一个确认按钮,以允许用户覆盖该文件。 It will create two postbacks if the user decides to upload and overwrite an existing file. 如果用户决定上传并覆盖现有文件,它将创建两个回发。

//add this function in your program //将此函数添加到程序中

public static void ShowAlertMessage(string error)
{
        Page page = HttpContext.Current.Handler as Page;
        if (page != null)
        {
            error = error.Replace("'", "\'");
            ScriptManager.RegisterStartupScript(page, page.GetType(), "err_msg", "alert('" + error + "');", true);
        }
    }

//check the value returned by your function //检查函数返回的值

private void btnSave_Click(Object sender, EventArgs e)
{
    bool flag=CheckData(myString);
    //If the above is true, then the confirmation box should appear.
    //Do something to save myString to the database.
if (flag==true)
{
ShowAlertMessage("The data Could not be added");
}
else
{
//do the desired operation of adding in the database.
}

}

Hope it helps.. 希望能帮助到你..

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

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