-1

我正在尝试设置驻留在事件中的标签文本gridviewRowDataBound相同的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的标签?

4

1 回答 1

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>

您的列标签在哪里,放入列标签并尝试

于 2013-03-06T07:11:52.293 回答