簡體   English   中英

如何在Gridview的頁腳模板中設置標簽的值?

[英]How do I set the value of a label in a Gridview's footer template?

我在gridview上有一些標簽,試圖將文本設置為頁面加載。 我修復了“對象引用錯誤”,但是我還沒有弄清楚如何動態設置標簽文本。 這是我嘗試過的:

protected string AutoDate, Website;

protected void Page_Load(object sender, EventArgs e)
{
    BindData();
    pnlMainGrid.Visible = true;
    DateTime dtMyDate = DateTime.Now;
    AutoDate = Convert.ToString(dtMyDate);
    Website = "MySite";
    Label lblCreateDate = ((Label)gvMainView.FooterRow.FindControl("lblCreateDate"));
    lblCreateDate.Text = AutoDate;
    Label lblWebsite = ((Label)gvMainView.FooterRow.FindControl("lblWebsite"));
    lblWebsite.Text = Website;
}

已編輯

我不再遇到“對象引用”錯誤,但是仍然無法動態設置標簽文本。

正確的方法是將網格與數據綁定后,在Prerender事件中更改標簽文本,如下所示:

protected void Page_PreRender(object sender, EventArgs e)
{
      Label lblCreateDate = ((Label)gvMainView.FooterRow.FindControl("lblCreateDate")).Text;
    lblCreateDate.Text = AutoDate;
    Label lblWebsite = ((Label)gvMainView.FooterRow.FindControl("lblWebsite")).Text;
    lblWebsite.Text = Website;
}

prerender是您應始終在將網格項目呈現給用戶之前操​​縱網格項目的位置,否則必須重新加載。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM