2012-07-23 131 views
1

我有一個Gridview允許用戶編輯和刪除記錄。我正在使用.net 3.5使用C#。Gridview編輯模式多一行

當我只有一個記錄我的編輯命令工作正常,但問題出現時,我的gridview有更多的1條記錄。

在這種情況下,綁定控件時,它無法第二次找到控件,從而引發錯誤。

這是我的代碼:

protected void gdScheduleNew_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     if (ViewState["Updating"].Equals(true)) 
     { 
      TextBox txtactsta = (TextBox)e.Row.FindControl("txtactualstartDate"); 
      TextBox txtactend = (TextBox)e.Row.FindControl("txtactualendDate"); 
      LinkButton lnkUpdate = (LinkButton)e.Row.FindControl("lnkUpdate"); 
      txtactsta.Attributes.Add("ReadOnly", "true"); 
      txtactend.Attributes.Add("ReadOnly", "true"); 
     } 
    } 
} 

的標記代碼:

<asp:GridView ID="gdScheduleNew" runat="server" Width="100%" SelectedIndex="0" 
         CellPadding="4" AutoGenerateColumns="False" ForeColor="#333333" Height="100px" 
         OnRowDataBound="gdScheduleNew_RowDataBound" onrowediting="gdSchedule_RowEditing"> 
    <FooterStyle Font-Bold="True" ForeColor="White" /> 
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 
    <Columns> 
     <asp:TemplateField HeaderText="Planned Efforts"> 
     <ItemTemplate> 
     <asp:Label ID="lblscheduleid" runat="server" Text='<%# Bind("scheduleid") %>' 
                Visible="false"></asp:Label> 
     <asp:Label ID="lblPlannedEffort" runat="server" Text='<%# Bind("sepcet") %>'></asp:Label> 
     </ItemTemplate> 
     <EditItemTemplate> 
     <asp:Label ID="lblscheduleid" runat="server" Text='<%# Bind("scheduleid") %>' 
     Visible="false"></asp:Label> 
       <asp:TextBox ID="txtsepcet" runat="server" Text='<%# Bind("sepcet") %>' 
        Width="88px"></asp:TextBox> 
      </EditItemTemplate> 
      <FooterTemplate> 
       <asp:TextBox ID="ftxtsepcet" runat="server" Width="88px"></asp:TextBox> 
      </FooterTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Actual Efforts"> 
      <ItemTemplate> 
       <asp:Label ID="lblActualEffort" runat="server" 
        Text='<%# Bind("actualeffort") %>'></asp:Label> 
      </ItemTemplate> 
       <EditItemTemplate> 
       <asp:TextBox ID="txtactualeffort" runat="server" 
         Text='<%# Bind("actualeffort") %>' Width="88px"></asp:TextBox> 
      </EditItemTemplate> 
      <FooterTemplate> 
       <asp:TextBox ID="ftxtactualeffort" runat="server" Width="88px"></asp:TextBox> 
      </FooterTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Planned Start Date"> 
      <ItemTemplate> 
       <asp:Label ID="lblPlannedStart" runat="server" 
        Text='<%# Bind("plannedstartDate") %>'></asp:Label> 
      </ItemTemplate> 
       <EditItemTemplate> 
       <asp:TextBox ID="txtplannedstartDate" runat="server" 
         Text='<%# Bind("plannedstartDate") %>' class="dateclass" Width="88px"></asp:TextBox> 
      </EditItemTemplate> 
      <FooterTemplate> 
       <asp:TextBox ID="ftxtplannedstartDate" runat="server" class="dateclass" 
        Width="88px"></asp:TextBox> 
      </FooterTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Planned End Date"> 
      <ItemTemplate> 
       <asp:Label ID="lblPlannedEnd0" runat="server" 
        Text='<%# Bind("plannedendDate") %>'></asp:Label> 
      </ItemTemplate> 
       <EditItemTemplate> 
       <asp:TextBox ID="txtplannedendDate" runat="server" 
         Text='<%# Bind("plannedendDate") %>' class="dateclass" Width="88px"></asp:TextBox> 
      </EditItemTemplate> 
      <FooterTemplate> 
       <asp:TextBox ID="ftxtplannedendDate" runat="server" class="dateclass" 
        Width="88px"></asp:TextBox> 
      </FooterTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Actual Start Date"> 
      <ItemTemplate> 
       <asp:Label ID="lblActualStart" runat="server" 
        Text='<%# Bind("actualstartDate") %>'></asp:Label> 
      </ItemTemplate> 
       <EditItemTemplate> 
       <asp:TextBox ID="txtactualstartDate" runat="server" 
         Text='<%# Bind("actualstartDate") %>' class="dateclass" Width="88px"></asp:TextBox> 
      </EditItemTemplate> 
      <FooterTemplate> 
       <asp:TextBox ID="ftxtactualstartDate" runat="server" class="dateclass" 
        Width="88px"></asp:TextBox> 
      </FooterTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Actual End Date"> 
      <ItemTemplate> 
       <asp:Label ID="lblActualEnd" runat="server" 
        Text='<%# Bind("actualendDate") %>'></asp:Label> 
      </ItemTemplate> 
       <EditItemTemplate> 
       <asp:TextBox ID="txtactualendDate" runat="server" 
         Text='<%# Bind("actualendDate") %>' class="dateclass" Width="88px"></asp:TextBox> 
      </EditItemTemplate> 
      <FooterTemplate> 
       <asp:TextBox ID="ftxtactualendDate" runat="server" class="dateclass" 
        Width="88px"></asp:TextBox> 
      </FooterTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Member Name"> 
      <ItemTemplate> 
       <asp:Label ID="lblMember" runat="server" Text='<%# Bind("membername") %>'></asp:Label> 
      </ItemTemplate> 
       <EditItemTemplate> 
        <asp:DropDownList ID="ddlmem" runat="server" DataTextField="MemberName" DataValueField="MemberID"> 
        </asp:DropDownList> 
      </EditItemTemplate> 
      <FooterTemplate> 
        <asp:DropDownList ID="fddlmem" runat="server" DataTextField="MemberName" DataValueField="MemberID"> 
        </asp:DropDownList> 
      </FooterTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Status"> 
      <ItemTemplate> 
       <asp:Label ID="lblStatus" runat="server" Text='<%# Bind("status") %>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Edit" ShowHeader="False"> 
     <EditItemTemplate> 
      <asp:LinkButton ID="lnkUpdate" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton> 
      <br /> 
      <asp:LinkButton ID="LnkCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton> 
     </EditItemTemplate>  
     <FooterTemplate> 
      <asp:LinkButton ID="lnkAddnew" runat="server" CausesValidation="true" 
       CommandName="AddNew" Text="Submit"></asp:LinkButton> 
     </FooterTemplate> 
     <ItemTemplate> 
       <asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="False" 
        CommandName="Edit" Text="Edit"></asp:LinkButton> 
     </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

該錯誤是在這條線:txtactsta.Attributes.Add("ReadOnly", "true");

對象引用沒有設置的一個實例對象

我該如何解決這個問題?

回答

1

是不是gdScheduleNew_RowDataBound得到要求行在您的GridView? txtactualstartDatetxtactualendDate只存在於編輯行中,它們不存在於非編輯行中。

試試這個:

if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Edit)