我正在尝试设置驻留在事件中的标签文本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>
在上面的代码中,'d'是gridview中标签的id。在突出显示的行上,我收到错误,OBJECT REFERENCE NOT SET TO INSTANCE OF AN OBJECT.
据我所知,没有创建新标签。然后如何分配在此事件中计算的值到gridview的标签?