1

I have a Gridview that allows user to edit and delete the records. I am using .net 3.5 using C#.

When I have just one record my edit command is working fine but problem comes when there are more that 1 record in my gridview.

In this case while binding the controls it it is unable to find the controls the second time and thus throws the error.

Here is my code:

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");
        }
    }
}

markup code is:

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

The error is on this line: txtactsta.Attributes.Add("ReadOnly", "true");:

object reference no set to an instance of object

How can I fix this?

4

1 回答 1

1

不会为您的 gridview 中的每一gdScheduleNew_RowDataBound调用?并且只存在于编辑行上,它们不存在于非编辑行上。txtactualstartDatetxtactualendDate

尝试这个:

if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Edit)
于 2012-07-23T15:15:44.983 回答