我有一个Gridview,允许用户编辑和删除记录。我正在使用.net 3.5使用C#。
当我只有一条记录时,我的编辑命令工作正常,但当我的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");
:
对象引用没有设置为对象的实例
我该如何解决这个问题?
答案 0 :(得分:1)
在gridview中每个行都不会调用gdScheduleNew_RowDataBound
吗? txtactualstartDate
和txtactualendDate
仅存在于编辑行上,在非编辑行中不存在。
试试这个:
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Edit)