我正在尝试设置位于gridview
RowDataBound
gridview
事件protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int day = Convert.ToInt32(DropDownList3.SelectedValue);
int days = System.DateTime.DaysInMonth(2013,day);
Label lab = (Label)e.Row.FindControl("d");
lab.Text = days.ToString();
}
}
上的标签文字。相同的代码如下:
<asp:GridView ID="GridView2" runat="server" OnRowDataBound="GridView2_RowDataBound" />
<asp:BoundField DataField="name" HeaderText="Name" SortExpression="n" />
<asp:BoundField DataField="dept" HeaderText="Department" SortExpression="d" />
<asp:BoundField DataField="code" HeaderText="Employee Code" SortExpression="c" />
<asp:TemplateField HeaderText="Total days" >
<itemtemplate>
<asp:Label ID="d" runat="server" Text="" />
</itemtemplate>
</asp:TemplateField>
</asp:GridView>
GRIDVIEW STRUCTURE:
OBJECT REFERENCE NOT SET TO INSTANCE OF AN OBJECT.
在上面的代码中,'d'是gridview中标签的id。在突出显示的行中我收到错误{{1}}据我所知,新标签没有被创建。然后如何将此事件中计算的值分配给gridview的标签?
答案 0 :(得分:1)
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView2_RowDataBound">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="n" />
<asp:BoundField DataField="ID" HeaderText="Department" />
<asp:BoundField DataField="Seats" HeaderText="Employee Code" />
<asp:TemplateField HeaderText="Total days">
<ItemTemplate>
<asp:Label ID="d" runat="server" Text=""></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
您的columns标记在哪里,放在columns标记中并尝试