如何从下拉列表中显示确认对话框
本文关键字:显示 确认 对话框 下拉列表 | 更新日期: 2023-09-27 18:12:07
我有一个显示数据库数据的网格,我在左侧有一个自定义列,我选择要删除的记录,我有一个下拉列表,这将触发服务器端删除记录的事件,在我删除这些记录之前,我想显示一个确认对话框,如"你确定吗?用ok和cancel,怎么做呢?任何想法吗?
我这样做:
if(ddlAction.SelectedValue == "Delete")
{
string id = string.Empty;
int i = 0;
List<int> idx = new List<int>();
foreach (GridViewRow rowitem in gvDept.Rows)
{
CheckBox itemchk = (CheckBox)rowitem.FindControl("cbSelectOne");
if (itemchk != null & itemchk.Checked)
{
id += rowitem.Cells[3].Text.ToString() + ',';
idx.Add(i);
}
i = i + 1;
}
id = id.Trim(",".ToCharArray());
List<string> objRemoveKeys = id.Split(',').ToList();
if (objRemoveKeys.Count > 0)
{
ddlAction.Attributes.Add("OnChange", "javascript:return confirmDeletion('Are you sure you would like to remove the selected items?');"); // this part not working.
AirAsiaLinqDataContext LinqDataCtx = new AirAsiaLinqDataContext();
var record = from a in LinqDataCtx.departements
where objRemoveKeys.Contains(a.departementcode)
select a;
LinqDataCtx.departements.DeleteAllOnSubmit(record);
LinqDataCtx.SubmitChanges();
for (int j = 0; j < idx.Count; j++)
{
gvDept.DeleteRow(idx[j]);
}
}
ddlAction.SelectedValue = "";
}
这看起来像代码隐藏(c#)代码。对话发生在客户端。使用jQuery(甚至是普通的JavaScript代码)可以相对容易地做到这一点,或者使用Ajax控制工具包的ConfirmButton
:
对于更多的控制过程,你也可以给JuiceUI一个尝试:http://juiceui.com/controls/dialog
try this
ddlAction.Attributes.Add("onchange", "return confirm('Are you sure you would like to remove the selected items?');");
您不仅应该显示确认警报,还应该检查用户是否选择了某些行。下面的代码实现了这两个功能。
javascript函数:
function checkIfSelected() {
if (yourGrid.GetSelectedRowCount() == 0) {
alert("You must select atleast one.");
return false;
}
else {
if (confirm("Are you sure you want to proceed?")) { // This is what you want
}
else {
return false;
}
}
}
dropdownlist
:
<asp:DropDownList ID="ddlAction" onChange="javascript:if( checkIfSelected() == false){return false};" AutoPostBack="true" runat="server" OnSelectedIndexChanged="yourID_SelectedIndexChanged">