C# RowDataBound对象未设置为对象的实例
我有一个运行良好的gridview,但在单击“编辑”时会出现“对象未设置为对象实例”的错误 我认为这是因为我的gridview中的标签在编辑模式下是空的,这就是问题的原因,但我不知道如何解决这个问题C# RowDataBound对象未设置为对象的实例,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个运行良好的gridview,但在单击“编辑”时会出现“对象未设置为对象实例”的错误 我认为这是因为我的gridview中的标签在编辑模式下是空的,这就是问题的原因,但我不知道如何解决这个问题 <asp:BoundField DataField="Received" HeaderText="Received" SortExpression="Received" ReadOnly="true">
<asp:BoundField DataField="Received" HeaderText="Received" SortExpression="Received"
ReadOnly="true">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:TemplateField HeaderText="Complete" SortExpression="Complete">
<ItemTemplate>
<asp:Label ID="lblComplete" runat="server" Text='<%# Bind("Complete") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:BoundField DataField="TransTime" HeaderText="Trans. Time" SortExpression="TransTime"
ReadOnly="true">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="lbClose" runat="server" CausesValidation="False" CommandName="CloseClicked" Text ="Close"
OnClick="CloseClick_Click">Close</asp:LinkButton>
<asp:LinkButton ID="lbEdit" runat="server" CausesValidation="False" CommandName="EditRow" Text =""
OnClick="Edit_Click" CommandArgument='<%# Eval("TicketId")%>'>Edit</asp:LinkButton>
<asp:LinkButton ID="lbDelete" runat="server" CausesValidation="False" CommandName="DeleteRow" Text =""
OnClick="Delete_Click">Delete </asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lbUpdate" runat="server" CausesValidation="True" CommandName="UpdateRow"
ForeColor="White" Text="Update" CommandArgument='<%# Eval("TicketId")%>'></asp:LinkButton>
<asp:LinkButton ID="lbCancel" runat="server" CausesValidation="False" CommandName="CancelUpdate"
ForeColor="White" CommandArgument='<%# Eval("TicketId")%>' Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
/>
</asp:GridView>
我在lbClose.Enabled中得到错误
protected void gvData_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Visible = false;
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lbClose = (LinkButton)e.Row.Cells[5].FindControl("lbClose");
LinkButton lbEdit = (LinkButton)e.Row.Cells[5].FindControl("lbEdit");
LinkButton lbDelete = (LinkButton)e.Row.Cells[5].FindControl("lbDelete");
var lblTrans = (Label)e.Row.FindControl("lblTrans");
var lblComplete = (Label)e.Row.FindControl("lblComplete");
if (e.Row.Cells[3].Text == "")
{
lbClose.Enabled = true; //Error Here
lbEdit.Enabled = true;
lbDelete.Enabled = true;
}
else
{
lbClose.Enabled = false;
}
}
}
我认为这是因为我的gridview中的标签在编辑模式下为空,这就是导致问题的原因
好的,您的意思是,当您在此行中输入EditMode
时,问题就出现了。您是对的,这些控件在EditMode
中不存在,因为它们是ItemTemplate
的一部分。所以只需执行以下操作:
LinkButton lbClose = (LinkButton)e.Row.Cells[5].FindControl("lbClose");
if (lbClose == null) { return; }
如果找不到控件,则知道行的状态,因此下面的语句无关紧要
我认为这是因为我的gridview中的标签在编辑模式下为空,这就是导致问题的原因
好的,您的意思是,当您在此行中输入EditMode
时,问题就出现了。您是对的,这些控件在EditMode
中不存在,因为它们是ItemTemplate
的一部分。所以只需执行以下操作:
LinkButton lbClose = (LinkButton)e.Row.Cells[5].FindControl("lbClose");
if (lbClose == null) { return; }
如果找不到控件,则知道行的状态,因此下面的语句无关紧要。这些控件在EditMode中不存在,因为它们是ItemTemplate的一部分。只需更改条件
if (e.Row.RowType == DataControlRowType.DataRow & gvData.EditIndex != e.Row.RowIndex)
这些控件在EditMode中不存在,因为它们是ItemTemplate的一部分。只需更改条件
if (e.Row.RowType == DataControlRowType.DataRow & gvData.EditIndex != e.Row.RowIndex)
跟踪输出应该准确地告诉您哪一行是问题所在。我猜可能是您的某个FindControl语句返回null。跟踪输出应该准确地告诉您哪一行是问题所在。我猜可能是您的某个FindControl语句返回null。您能共享gridview和sqldatasource!能否共享gridview和sqldatasource的完整aspx页面!