日期:2014-05-18  浏览次数:20481 次

获取或设置GridView中的Label值
<asp:GridView   ID= "GridView1 "   runat= "server "   AllowPaging= "True "   AutoGenerateColumns= "False "   DataSourceID= "SqlDataSource1 "   PageSize= "20 ">
          <Columns>
              <asp:TemplateField   HeaderText= "开始日期 "   SortExpression= "ksrq ">
                      <ItemTemplate>
                              <asp:Label   ID= "Label4 "   runat= "server "   Text= ' <%#   Bind( "ksrq ", "yyyy-MM-dd ")   %> '> </asp:Label>
                      </ItemTemplate>
              </asp:TemplateField>

请问我在后台代码中如何得到Label4的值或改变它的值。
好像不能直接用Label4.Text   =   "XXX "
        protected   void   GridView1_RowDataBound(object   sender,   GridViewRowEventArgs   e)
        {
                //Label   lb   =   (Label)e.Row.FindControl( "Label4 ");
                Label   lb   =   (Label)GridView1.FooterRow.FindControl( "Label4 ");
                lb.Text   =   "aaa ";
        }这要写总是不行,提示错误:“未将对象引用设置到对象的实例。”

------解决方案--------------------
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lb = (Label)GridView1.FooterRow.FindControl( "Label4 ");
lb.Text = "aaa ";
}
}