0

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

我这样做:

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

}

4

3 回答 3

0

这看起来像代码隐藏 (C#) 代码。对话发生在客户端。您可以使用 jQuery(甚至原生 JavaScript 代码)相对轻松地做到这一点,或者使用 Ajax Control Toolkit 之类的东西ConfirmButton

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

要对流程进行更多控制,您还可以试一试 JuiceUI:http: //juiceui.com/controls/dialog

于 2013-05-03T05:52:24.937 回答
0

尝试这个

ddlAction.Attributes.Add("onchange", "return confirm('Are you sure you would like to remove the selected items?');");
于 2013-05-03T05:47:05.430 回答
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">

于 2013-05-03T06:14:26.697 回答