繁体   English   中英

ASP.NET中的确认框OnClientClick

[英]Confirmation Box OnClientClick in ASP.NET

我正在这个网站上工作,我需要过滤要插入数据库的数据。 例如,我需要在数据库中插入“ Hello World”。 但是,它已经存在。 因此,我需要弹出一个确认框,指出“字符串Hello World已存在于数据库中。确定要继续吗?”

我的问题是我不知道在代码中的哪个位置添加确认消息。 请参阅下面的参考资料:

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.
}

我正在使用以下代码以编程方式将OnClientClick事件处理程序添加到我的按钮:

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

我的问题基本上是处理这种情况的最佳方法是什么? 由于我无法将此代码放在btnSave_Click事件上(它将添加OnClientClick处理程序,但只会在下次单击该按钮时触发)。

要通过ajax执行此操作,请让onClientClick调用js函数,该函数调用.net处理程序以检查文件是否存在,并且:

  1. 如果不存在文件,则返回true,从而发生回发
  2. 如果文件已经存在,则弹出确认框并返回确认框的结果

否则,您可以检查回发中是否存在该文件,但随后必须显示一条消息和另一个确认按钮,以允许用户覆盖该文件。 如果用户决定上传并覆盖现有文件,它将创建两个回发。

//将此函数添加到程序中

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);
        }
    }

//检查函数返回的值

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.
}

}

希望能帮助到你..

暂无
暂无

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

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