簡體   English   中英

如何從下拉列表中顯示確認對話框?

[英]how to show confirmation dialog from drop down list?

我有一個顯示數據庫中數據的網格,並且我在左側的復選框中有一個自定義列,我選擇要刪除的記錄,並且我有一個下拉列表,這將觸發服務器端的一個事件,在我刪除記錄之前刪除那些我想顯示確認對話框的記錄,例如“確定嗎?單擊確定並取消”,該怎么做? 任何想法?

我這樣做:

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

}

嘗試這個

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

這看起來像是代碼隱藏(C#)代碼。 對話發生在客戶端。 您可以使用jQuery(甚至是ConfirmButton JavaScript代碼)相對容易地做到這一點,或者使用類似Ajax Control Toolkit的ConfirmButton

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

要進一步控制該過程,您還可以嘗試使用JuiceUI: http ://juiceui.com/controls/dialog

您不僅應該顯示警報以進行確認,還應該檢查用戶是否選擇了某些行。 下面的代碼完成了這兩者。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM