在Gridview中获取TemplateFields的值

时间:2010-06-23 08:47:31

标签: c# asp.net

我在获取模板字段的值时遇到问题; Gridview位于ContentPlaceHolder1;

我正试图在GridView1_RowCreated事件中获得价值

int RowIndex = GridView1.Rows.Count - 1;
GridView1.Rows[RowIndex].Cells[0].Text = " " + AltKatLinkler;

但是这段代码返回null或空。

我的列,列索引为0。 注意:我使用SqlDataSource填充GridView。没有问题我可以在浏览器中看到行内容但我无法从代码隐藏中访问。

<asp:templatefield headertext="Haberler" sortexpression="KategoriID" xmlns:asp="#unknown">
    <ItemTemplate>
       < a href='<%# "KategoriGoster.aspx?KategoriID=" + Eval("KategoriID")%>'>
       <%# Eval("KategoriAd")%>
       <%# Eval("Açıklama")%>
    </ItemTemplate>
</asp:TemplateField>

1 个答案:

答案 0 :(得分:2)

看另一种方法

<asp:templatefield headertext="Haberler" sortexpression="KategoriID" xmlns:asp="#unknown">
    <ItemTemplate>
       < a href='<%# "KategoriGoster.aspx?KategoriID=" + Eval("KategoriID")%>'>
       <asp:Label ID="lbKategori" runat="server" Text='<%# Eval("KategoriAd").ToString() %>'></asp:Label>
       <asp:Label ID="lbAçıklama" runat="server" Text='<%# Eval("Açıklama").ToString() %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

代码隐藏

   protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            var lbKategori = e.Row.FindControl("lbKategori") as Label;
            var lbAçıklama = e.Row.FindControl("lbAçıklama") as Label;
        }
    }