2013-07-15 56 views
1

我需要在按鈕上彈出確認單擊從c#使用asp.net gridview刪除行。 下面是按鈕點擊從gridview刪除行的代碼。刪除前我需要彈出確認。Popup確認

下面是代碼

protected void annDelete_Click(object sender, EventArgs e) 
{ 
      foreach (GridViewRow row in GridView1.Rows) 
      { 
       CheckBox cb = (CheckBox)row.FindControl("chk_delete"); 

       if (cb.Checked && cb != null) 
       { 
        { 
         int ID_No = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value); 
         MySqlCommand com = new MySqlCommand(); 

         com.CommandText = "Delete from announcement where id = '" + ID_No + "'"; 
         com.Connection = con; 

         //con.Open(); 
         com.ExecuteNonQuery(); 
         con.Close(); 
         GridView1.DataSource = null; 

        } 

       } 

      } 
      LoadIssueData(); 
} 
+0

你是什麼錯誤?你想要做什麼? –

回答

1

假設annDelete是一個asp.net按鈕控制你可以將下面的代碼來問客戶確認:

HTML:

<asp:Button ID="annDelete" OnClientClick="if(!confirm('Confirm delete?')) return false;"></asp:Button> 

return false將避免服務器端命中。

+1

所有你需要的是'return confirm('Confirm delete?')',因爲'confirm'已經返回'true'或'false' ...不需要任何if語句!編寫更好的代碼總是好的;) – balexandre

+0

您的解決方案正在工作。謝謝噸 – VimalSingh

+0

謝謝ton.Its工作 – VimalSingh

0

讓我們假設你有一個控制。

只需添加OnClientClick事件,如:

<asp:Button id="btnDelete" OnClick="annDelete_Click" 
      OnClientClick="return confirm('Are you sure?');" /> 

confirm javascript調用會打開一個OK/Cancel警覺,並會返回true,當用戶按下假OK,否則,你將返回按鈕。

只有當您返回true時,該按鈕纔會繼續調用__Post事件。

+0

謝謝了。 - Vimal – VimalSingh

0

您可以添加的OnClientClick屬性:

<asp:Button ID="annDelete" OnClientClick="return confirm('do you really want to delete?');" runat="server" Text="Button" OnClick="annDelete_Click" /> 

如果用戶將選擇確定確認函數將返回真正和__doPostBack()函數將被調用,否則 - 不。

+0

它正在工作。謝謝一噸 - Vimal Singh – VimalSingh

2

這種方式更適合我的GridView的刪除按鈕: HTML:

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" 
         AllowPaging="True" AllowSorting="True" 
              onpageindexchanging="gv_PageIndexChanging" 
              onsorting="gv_Sorting" GridLines="None" DataKeyNames="ID" 
               PageSize="20" onrowdeleting="gv_RowDeleting" 
               onrowdatabound="gv_RowDataBound" > 
         <PagerSettings FirstPageText="First" LastPageText="Last" 
        Mode="NumericFirstLast" NextPageText="Next" PageButtonCount="5" 
        PreviousPageText="Previous" /> 

         <Columns> 
          <asp:BoundField DataField="SECURITIES_NAME" HeaderText="Securities Name" 
           SortExpression="SECURITIES" /> 

          <asp:BoundField DataField="FIN_YEAR" HeaderText="Financial Year" 
           SortExpression="FIN_YEAR" /> 
          <asp:BoundField DataField="RECORD_DATE" HeaderText="Record Date" 
           SortExpression="RECORD_DATE" /> 
          <asp:BoundField DataField="AGM_DATE" HeaderText="AGM Date" 
           SortExpression="AGM_DATE" /> 
          <asp:BoundField DataField="CA_SEQ_NO" HeaderText="CA SEQ NO" 
           SortExpression="CA_SEQ_NO" /> 
          <asp:TemplateField HeaderText="Action"> 
           <ItemTemplate> 
            <div class="actions"> 
             <asp:LinkButton ID="lbView" runat="server" CausesValidation="True" CommandName="Select" Text="View"></asp:LinkButton> 
             <asp:LinkButton ID="lbEdit" runat="server" CausesValidation="True" CommandName="Edit" Text="Edit"></asp:LinkButton> 
             <asp:LinkButton ID="lbDelete" runat="server" OnClientClick="return confirm('Are you sure want to delete the Corporate Action?')" CausesValidation="False" 
              CommandName="Delete" Text="Delete"></asp:LinkButton> 
            </div> 
           </ItemTemplate> 
          </asp:TemplateField>    
         </Columns> 
         <PagerStyle CssClass="pagination-flickr" BorderStyle="None" /> 
         <HeaderStyle BorderStyle="None" /> 
         <AlternatingRowStyle CssClass="altrow" /> 
        </asp:GridView> 
the codebehind cs: 
protected void Page_Load(object sender, EventArgs e) 
    { 
     IsAuthenticated.CheckSession(); 
     if (!IsPostBack) 
     { 
      if (Session["setFlash"] != null) 
      { 
       if (Session["setFlash"].ToString() == "1") 
       { 
        ltrMsg.Text = @"<div class='message-green'>Save Successfully</div>"; 
        Session["setFlash"] = null; 
       } 
       else 
       { 
        ltrMsg.Text = @"<div class='message'>Not Save Successfully</div>"; 
        Session["setFlash"] = null; 
       } 
      } 

      common.gv(@"Select SECURITIES_ID,BO_FNC_GET_SECURITIES_NAME(SECURITIES_ID) SECURITIES_NAME, 
       CA_SEQ_NO,FIN_YEAR,RECORD_DATE,AGM_DATE,SETUP_DATE,ID,ROWID From CA_MST", gv); 
      ViewState["sortingOrder"] = string.Empty; 
      DataBindGrid("", ""); 

     } 
    } 
    protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e) 
    { 
     string wh = "ID='" + gv.DataKeys[e.RowIndex].Values[0].ToString() + "'"; 
     DataProcess.Save("CA_MST", wh); 

     common.gv(@"Select SECURITIES_ID,BO_FNC_GET_SECURITIES_NAME(SECURITIES_ID) SECURITIES_NAME, 
       CA_SEQ_NO,FIN_YEAR,RECORD_DATE,AGM_DATE,SETUP_DATE,ID,ROWID FROM CA_MST", gv); 
    } 

    protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e) 
    { 
     gv.PageIndex = e.NewPageIndex; 
     common.gv(@"Select SECURITIES_ID,BO_FNC_GET_SECURITIES_NAME(SECURITIES_ID) SECURITIES_NAME, 
       CA_SEQ_NO,FIN_YEAR,RECORD_DATE,AGM_DATE,SETUP_DATE,ID,ROWID FROM CA_MST", gv); 

    } 
    private void DataBindGrid(string sortExpr, string sortOrder) 
    { 
     // GetDataTable returns a filled table 
     DataTable dt = DataProcess.getDataTable(@"Select SECURITIES_ID,BO_FNC_GET_SECURITIES_NAME(SECURITIES_ID) SECURITIES_NAME, 
       CA_SEQ_NO,FIN_YEAR,RECORD_DATE,AGM_DATE,SETUP_DATE,ID,ROWID FROM CA_MST"); 
     // any check to validate dt 
     if (dt != null) 
     { 
      DataView dv = dt.DefaultView; 

      if (sortExpr != string.Empty) 
       dv.Sort = sortExpr + " " + sortOrder; 

      this.gv.DataSource = dv; 
      this.gv.DataBind(); 
     } 
     else 
     { 
      this.gv.DataSource = null; 
      this.gv.DataBind(); 
     } 
    } 
    protected void gv_Sorting(object sender, GridViewSortEventArgs e) 
    { 
     DataBindGrid(e.SortExpression, sortingOrder); 
    } 
    public string sortingOrder 
    { 
     get 
     { 
      if (ViewState["sortingOrder"].ToString() == "desc") 
       ViewState["sortingOrder"] = "asc"; 
      else 
       ViewState["sortingOrder"] = "desc"; 

      return ViewState["sortingOrder"].ToString(); 
     } 
     set 
     { 
      ViewState["sortingOrder"] = value; 
     } 
    } 
    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      LinkButton lbv = (LinkButton)e.Row.FindControl("lbView"); 
      lbv.PostBackUrl = "CaMstVw.aspx?rid=" + gv.DataKeys[e.Row.RowIndex].Values[0].ToString(); 

      LinkButton lb = (LinkButton)e.Row.FindControl("lbEdit"); 
      lb.PostBackUrl = "CaMstIns.aspx?rid=" + gv.DataKeys[e.Row.RowIndex].Values[0].ToString() ; 
     } 
    } 
+0

Thakns,其工作 – VimalSingh

+0

然後plz使它標記以便其他人它可以幫助 –