如何从下拉列表中显示确认对话框?

时间:2013-05-03 05:43:49

标签: c# asp.net

我有一个显示数据库数据的网格,我在左侧有一个自定义列,带有复选框,我选择要删除的记录,我有一个下拉列表,这将触发服务器端的事件来删除记录,在我删除那些记录之前,我想要显示一个确认对话框,例如“你确定吗?有确定并取消”,该怎么做?有什么想法吗?

我这样做:

 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 = "";

}

3 个答案:

答案 0 :(得分:0)

试试这个

ddlAction.Attributes.Add("onchange", "return confirm('Are you sure you would like to remove the selected items?');");

答案 1 :(得分:0)

这看起来像代码隐藏(C#)代码。对话发生在客户端。您可以使用jQuery(甚至是vanilla JavaScript代码)相对轻松地完成此操作,或者使用类似Ajax Control Toolkit的ConfirmButton

http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/ConfirmButton/ConfirmButton.aspx

为了更多地控制这个过程,你也可以给JuiceUI一个:http://juiceui.com/controls/dialog

答案 2 :(得分:0)

您不仅应该显示确认提醒,还要检查用户是否选择了某行。下面的代码完成了两者。

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

您的下拉列表:

<asp:DropDownList ID="ddlAction" onChange="javascript:if( checkIfSelected() == false){return false};" AutoPostBack="true" runat="server" OnSelectedIndexChanged="yourID_SelectedIndexChanged">