问题描述
我正在尝试从 C# 中的代码隐藏显示是/否"消息框.如果用户单击是",我想调用AddRecord"过程,如果用户单击否",则什么都不做.
I am trying to display a "Yes / No" messagebox from codebehind in C#. I want to call an "AddRecord" procedure if the user clicks "Yes", and do nothing if the user clicks "No".
理想情况下,我想使用下面的代码,但来自代码隐藏:
Ideally, I want to use the code below, but from codebehind:
OnClientClick = "return confirm('Are you sure you want to delete?');"
我在 SO 和 google 上搜索,但找不到任何有用的东西.
I search on SO and google, but was not able to find anything helpful.
推荐答案
在您的添加记录"按钮上,只需执行以下操作:
on your Add Record button, just do the following:
<asp:button ID="AddRecordbutton" runat="server" Text="Add Record"
onclick="AddRecordButton_Click" onclientclick="return confirm('add record?');" />
在后面的代码中,只需将添加记录代码放在您的 AddRecordButton_Click 事件处理程序中.只有当他们在弹出窗口中单击是"时才会调用它.
In your code behind, just put the add record code in your AddRecordButton_Click event handler. It will only be called if they click Yes on the popup.
或者,您可以让代码隐藏在按钮最初呈现时分配 onclientclick 代码.
Alternatively, you could have your codebehind assign the onclientclick code when the button is initially rendered.
例如:
protected void Page_Load(object sender, EventArgs e) {
AddRecordButton.OnClientClick = @"return confirm('Add Record?');";
}
这篇关于显示“是/否"后面的 C# 代码中的警告框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!