我正在尝试在这个网站上工作,我需要过滤要插入数据库的数据。例如,我需要将“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 处理程序,但只会在下次单击按钮时触发)。