use*_*084 1 c# asp.net visual-studio
我有一个保存按钮,单击该按钮时,应在数据库中进行一些更改.
if (bFound== false)
{
// Giving the warning message
// If user presses cancel then abort
// Prepare the list of dbId needs to be deleted
deletedBSIds.Add(dbId);
}
Run Code Online (Sandbox Code Playgroud)
这里如果该bFound字段为true,则不应执行上述语句,但如果为false,则应该进入该条件,然后询问用户是否要保存更改"是"或"否".
如果用户说是,则应该转到命令" deletedBSIds.Add(dbId);"并继续执行,但如果用户按下否则它应该基本上中止并且什么都不做.
有没有办法做到这一点?任何帮助,将不胜感激.
这是服务器端事件.所以我认为不能在我的按钮中添加点击事件/ ..
如果bFoung字段为false,则仅弹出消息框.否则它根本不会弹出.
如果你觉得我错了,请纠正我.
谢谢
您需要将以下内容添加到按钮:
button.OnClientClick = "return ConfirmThis();";
Run Code Online (Sandbox Code Playgroud)
然后,您需要将ConfirmThis函数添加到页面:
Page.ClientScript.RegisterScriptBlock(GetType(), "ConfirmThis",
@"function ConfirmThis() {
if(condition) { //where condition checks the bfound element.
return confirm(""Are you sure you want to delete this?"");
}
return true;
}");
Run Code Online (Sandbox Code Playgroud)
做这种方法你会想尝试并能够在javascript中测试客户端的bfound条件.如果该bfound值存储在一个textbox或者HiddenField你应该使用的document.getElementById功能.如果在bfound创建页面时已知该值,则可以ConfirmThis直接将其注入函数,并将其ConfirmThis作为参数传递给函数.
尝试从用户那里获得确认时,您有两个选项:
在这两个示例中,第一个选项是更清晰,不需要临时内存,并为用户节省了额外的回发.
因为这两个选项都要求你重新修改要求确认的逻辑,如果可能的话,我会尝试转换条件所需的逻辑,以显示确认对话框,以便能够使用javascript在客户端的计算机上执行.
有没有什么方法可以预先计算bfound变量,或者至少发送足够的信息以便在客户端上计算?
使用以下代码(基于http://www.dotnetspider.com/resources/1521-How-call-Postback-from-Javascript.aspx:
if(bfound)
{
//save all the information you need in temporary information
ViewState["InformationINeedToFinishAfterPostback"] = ImportantInformation;
Page.ClientScript.RegisterScriptBlock(GetType(), "postbackmethod", Page.ClientScript.GetPostBackEventReference(this, "MyCustomArgument"));
Page.ClientScript.RegisterStartupScript(GetType(), "startupconfirm",
@"if(confirm(""are you sure?"") {
__doPostBack('__Page', 'MyCustomArgument');
}");
}
Run Code Online (Sandbox Code Playgroud)
现在要处理回发,请将以下代码添加到page_load:
if(Request("__EVENTARGUMENT") == "MyCustomArgument")
{
ImportantInformation = (CastToAppropriateType)ViewState["InformationINeedToFinishAfterPostback"];
//finalize the desired action here.
}
Run Code Online (Sandbox Code Playgroud)
但是......我仍然会推荐第一个选项.但是现在你有了两个选项所需的代码.此外,我没有测试这段代码,所以你必然会遇到语法问题,但它会让你走上正轨.