C# 对象引用未设置为标签的对象实例

C# 对象引用未设置为标签的对象实例,c#,gridview,nullreferenceexception,C#,Gridview,Nullreferenceexception,我试图在gridview的rowdabund事件上设置驻留在gridview中的标签文本。其代码如下: protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { int day = Convert.ToInt32(DropDownList3.SelectedValu

我试图在
gridview
rowdabund
事件上设置驻留在
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();
    }
}
GRIDVIEW结构:

<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。在突出显示的行中,我得到一个错误,即
对象引用未设置为对象的实例。
据我所知,新标签未被创建。那么如何将此事件中计算的值分配给gridview的标签?


 <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标记,然后尝试调试代码并检查e.Row.FindControl(“d”)的结果。我猜这是空的。几乎所有的
NullReferenceException
情况都是相同的。请参阅“”以获得一些提示。我知道它为空,但是我不知道如何解决它…你能建议我吗?文档说明如果指定的控件不存在,将返回
null
。显示gridview标记直到不工作。问题是…你能建议我在gridview中显示天值的其他方法吗?我将其标记为答案,但由于我是stackoverflow的新手,所以无法投票。这需要15个声誉。再次感谢。