2017-06-13 112 views
0

我嘗試刪除使用twitter引導確認後在GridView中顯示的數據。我與模態的對話中的GridView如下從asp:GridView中刪除數據使用模式對話框

<div class="panel panel-default"> 
    <div class="panel-heading"> 
     <h2 class="panel-title clearfix "> 
      <div> 
       <b>Name</b> 
       <div style="float: left;"> 
       </div> 

      </div> 
     </h2> 
    </div> 




    <div class="panel-body"> 
     <div> 
      <asp:GridView ID="gridView" DataKeyNames="Id" runat="server" 
       CssClass="table table-striped table-bordered table-hove" 
       AutoGenerateColumns="False" HeaderStyle-Font-Bold="true" 
       OnRowEditing="gridView_RowEditing" 
       OnRowCancelingEdit="gridView_RowCancelingEdit" 
       OnRowUpdating="gridView_RowUpdating" 
       OnRowDeleting="gridView_RowDeleting" 
       OnRowCommand="gridView_RowCommand"> 
       <Columns> 
        <asp:TemplateField HeaderText="ID" ControlStyle-Width="0px"> 
         <ItemTemplate> 
          <asp:Label ID="txtid" runat="server" Text='<%#Eval("Id") %>' /> 
         </ItemTemplate> 
         <EditItemTemplate> 
          <asp:Label ID="lblid" runat="server" Width="40px" Text='<%#Eval("Id") %>' /> 
         </EditItemTemplate> 
         <FooterTemplate> 
         </FooterTemplate> 
        </asp:TemplateField> 

        <asp:TemplateField HeaderText="Name"> 
         <ItemTemplate> 
          <asp:Label ID="lblLName" runat="server" Text='<%#Eval("Location_name") %>' /> 
         </ItemTemplate> 
         <EditItemTemplate> 
          <asp:TextBox ID="txtLName" CssClass="form-control" runat="server" Text='<%#Eval("Location_name") %>' /> 
         </EditItemTemplate> 
         <FooterTemplate> 
          <asp:TextBox ID="LocName" AutoCompleteType="Disabled" runat="server" /> 
          <asp:RequiredFieldValidator ID="ReqBrName" runat="server" ControlToValidate="BrName" Display="Dynamic" CssClass="valid" ErrorMessage="?" ValidationGroup="validaiton" /> 
         </FooterTemplate> 
        </asp:TemplateField> 





        <asp:TemplateField HeaderText="Options"> 
         <ItemTemplate> 
          <asp:LinkButton ID="lbEdit" CssClass="btn btn-default btn-sm " runat="server" CommandName="Edit">                
                   <span class="glyphicon glyphicon-edit" aria-hidden="true"></span> Edit</asp:LinkButton> 

          <asp:LinkButton ID="lbDelete" CssClass="btn btn-default btn-sm " runat="server" CommandArgument='<%#Eval("Id")%>' OnClientClick="return ConfirmDelete()">                
                   <span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete</asp:LinkButton> 




          <%-- Delete Form --%> 
          <div class="modal fade" id="DeleteModal" tabindex="-1" role="dialog" aria-labelledby="delete" aria-hidden="true"> 
           <div class="modal-dialog"> 
            <div class="modal-content"> 
             <div class="modal-header"> 
              <button type="button" class="close glyphicon glyphicon-remove" data-dismiss="modal" aria-hidden="true"></button> 
              <h4 class="modal-title" id="">Delete the data</h4> 
             </div> 



             <div class="modal-body"> 
              Are You sure? 
              <asp:Label ID="lblLName1" runat="server" Text='<%#Eval("id") %>' /> 
              <br /> 

             </div> 
             <div class="modal-footer"> 
              <button type="button" class="btn btn-default glyphicon glyphicon-remove" data-dismiss="modal">Cancel</button> 
              <asp:LinkButton ID="lbde" CssClass="btn btn-default btn-sm " runat="server" CommandName="Delete">                
                   <span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete 
              </asp:LinkButton> 
             </div> 





            </div> 
           </div> 
          </div> 
          <%-- Delete Form --%> 
         </ItemTemplate> 
         <EditItemTemplate> 
          <asp:LinkButton ID="lbUpdate" CssClass="btn btn-default btn-sm " runat="server" CommandName="Update">                
                   <span class="glyphicon glyphicon-refresh" aria-hidden="true"></span> Update 
          </asp:LinkButton> 

          <asp:LinkButton ID="lbCancel" CssClass="btn btn-default btn-sm " runat="server" CommandName="Cancel">                
                   <span class="glyphicon glyphicon-remove-sign" aria-hidden="true"></span> Cancel 
          </asp:LinkButton> 



         </EditItemTemplate> 
         <FooterTemplate> 
         </FooterTemplate> 
        </asp:TemplateField> 

       </Columns> 

       <HeaderStyle Font-Bold="True"></HeaderStyle> 
      </asp:GridView> 

     </div> 
    </div> 

</div> 

和我的JavaScript函數是

<script> 

     function ConfirmDelete() { 
      $('#DeleteModal').modal() 
      return false; 
     } 
    </script> 

所以當我嘗試使用它不工作的彈出模型刪除的記錄,該模型將正常工作,但數據在lblLName1允許將第一個記錄ID,所以當我確認第一個記錄將被刪除

任何人都可以幫助我?

回答

0

謝謝大家

嘗試了幾種方法後,我找到了解決方案。 首先,從

<asp:LinkButton ID="lbDelete" CssClass="btn btn-default btn-sm " runat="server" CommandArgument='<%#Eval("Id")%>' OnClientClick="return ConfirmDelete()"> <span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete</asp:LinkButton> 

改變呼叫按鈕

<asp:LinkButton ID="lbDelete" CssClass="btn btn-default btn-sm" runat="server" CommandArgument='<%#Eval("Id")%>' 
           OnCommand="lbDelete_Command" CommandName='<%#Eval("Location_name")%>'>                
                   <span class="glyphicon glyphicon-trash" aria-hidden="true"></span> حذف</asp:LinkButton> 

,並在C#中調用函數下lbDelete_Command

protected void lbDelete_Command(object sender, CommandEventArgs e) 
    { 
     foreach (GridViewRow row in gridView.Rows) 
     { 

      HiddenField hf_id = row.FindControl("hf_Del_ID") as HiddenField; 
      Label l_name = row.FindControl("lb_Del_Name") as Label; 
      l_name.Text = e.CommandName.ToString(); 
      hf_id.Value = e.CommandArgument.ToString(); 

     } 

     ScriptManager.RegisterStartupScript(this, this.GetType(), "LaunchServerSide", "$(function() { ConfirmDelete(); });", true); 
    } 

和我下模體編輯字段部門如跟隨

<div class="modal-body"> 
<asp:HiddenField ID="hf_Del_ID" runat="server" /> 
Are You sure? 
<h4><asp:Label ID="lb_Del_Name" runat="server"/></h4> 
</div> 

我使用隱藏域hf_Del_ID進行刪除目的 和標籤lb_Del_Name僅用於顯示

並且還刪除SQL在C#作爲跟隨

try 
    { 
     HiddenField hf_id = (HiddenField)gridView.Rows[e.RowIndex].FindControl("hf_Del_ID"); 

     con.Open(); 
     SqlCommand cmd = new SqlCommand("delete from TABLE_NAME where Id=" + hf_id.Value, con); 
     int result = cmd.ExecuteNonQuery(); 
     con.Close(); 
     if (result == 1) 
     { 
      loadData(); 

      lbl_msgDet.ForeColor = Color.Red; 
      lbl_msgDet.Text = " delete done"; 
     } 
    } 
    catch (SqlException ex) 
    { 
     lbl_msgDet.ForeColor = Color.Red; 
     lbl_msgDet.Text = (String)GetGlobalResourceObject("Err_msg", "MsgDeleteErr") + "<br/>" + ex.Message; 
    } 
    ScriptManager.RegisterStartupScript(this, this.GetType(), "LaunchServerSide", "$(function() { CallMsg(); });", true); 

希望解決方案幫助

謝謝

相關問題