2014-02-12 89 views
1

您好我有一個GridView,我在編輯模式下加載。然後我有一個更新按鈕和一個刪除按鈕,允許用戶更新數據或刪除一行。由於某種原因,刪除方法不起作用,我傳遞的id已經變爲null,我找不到原因。我的GridView的代碼如下刪除方法中的GridView id null

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1"> 
    <Columns> 
     <asp:CommandField ShowDeleteButton="True" /> 
     <asp:TemplateField>     
      <ItemTemplate> 
       <%--<asp:Label ID="lblID" Visible='<%# IsInEditMode %>' runat="server" Text='<%# Bind("intExaminerID") %>'></asp:Label>--%> 
       <asp:TextBox ID="txtID" Visible='<%# IsInEditMode %>' runat="server" Text='<%# Bind("intExaminerID") %>'></asp:TextBox> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField> 
      <ItemTemplate> 
       <asp:TextBox ID="txtDOID" Visible='<%# IsInEditMode %>' runat="server" Text='<%# Bind("sintDOLDistrictOfficeID") %>'></asp:TextBox> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="First Name">     
      <ItemTemplate> 
       <asp:TextBox ID="txtFName" Visible='<%# IsInEditMode %>' runat="server" Text='<%# Bind("vcharFirstName") %>'></asp:TextBox> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Last Name"> 
      <ItemTemplate> 
       <asp:TextBox ID="txtLName" Visible='<%# IsInEditMode %>' runat="server" Text='<%# Bind("vcharLastName") %>'></asp:TextBox> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Email"> 
      <ItemTemplate> 
       <asp:TextBox ID="txtEmail" Visible='<%# IsInEditMode %>' runat="server" Text='<%# Bind("vcharEmailAddress") %>'></asp:TextBox> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Extension"> 
      <ItemTemplate> 
       <asp:TextBox ID="txtPhone" Visible='<%# IsInEditMode %>' runat="server" Text='<%# Bind("vcharPhoneExtension") %>'></asp:TextBox> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="SSN Block"> 
      <ItemTemplate> 
       <asp:TextBox ID="txtSSNStart" Visible='<%# IsInEditMode %>' runat="server" Text='<%# Bind("vcharSSNBlockStart") %>' Width="5em"></asp:TextBox> 
       <asp:TextBox ID="txtSSNEnd" Visible='<%# IsInEditMode %>' runat="server" Text='<%# Bind("vcharSSNBlockEnd") %>' Width="5em"></asp:TextBox> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Action(s)"> 
      <ItemTemplate> 
       <asp:Button ID="btnUpdate" runat="server" Text="Update" CommandName="Update" /> 
       <%--<asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="UpdateRow" />--%> 
       <asp:Button ID="btnDelete" runat="server" Text="Delete" CommandName="Delete" /> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

這裏是我的代碼背後:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
     if ((e.CommandName == "Update")) 
     { 
      int index = Convert.ToInt32(e.CommandArgument); 
      GridViewRow row = GridView1.Rows[index]; 


      //Label id = (Label)row.FindControl("lblID"); 
      TextBox id = (TextBox)row.FindControl("txtID"); 
      TextBox doid = (TextBox)row.FindControl("txtDOID"); 
      TextBox fname = (TextBox)row.FindControl("txtFName"); 
      TextBox lname = (TextBox)row.FindControl("txtLName"); 
      TextBox email = (TextBox)row.FindControl("txtEmail"); 
      TextBox phone = (TextBox)row.FindControl("txtPhone"); 
      TextBox start = (TextBox)row.FindControl("txtSSNStart"); 
      TextBox end = (TextBox)row.FindControl("txtSSNEnd"); 

      DOLBLL.UpdateExaminers(Convert.ToInt32(id.Text), Convert.ToInt16(doid.Text), fname.Text, lname.Text, email.Text, phone.Text, start.Text, end.Text); 


     } 

     if ((e.CommandName == "Delete")) 
     { 
      int index = Convert.ToInt32(e.CommandArgument); 
      GridViewRow row = GridView1.Rows[index]; 

      TextBox id = (TextBox)row.FindControl("txtID"); 

      DOLBLL.deleteClaimExaminer(Convert.ToInt32(id.Text)); 
     } 

    } 

誰能告訴我什麼,我做錯了什麼?謝謝。

回答

2

您可能需要設置DataKeyNames屬性以使索引正常工作。

同類問題here

+1

Hink。非常感謝。不能相信我沒有想到這一點。 :) – hollyquinn