获取Gridview中TemplateFields的值

3

我在获取模板字段值时遇到了问题; Gridview在ContentPlaceHolder1中;

我试图在GridView1_RowCreated事件中获取值

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

但是这段代码返回了 null 或空值。
这是我的列,列索引为 0。 注意:我使用 SqlDataSource 填充 GridView。在浏览器中,我可以看到行内容,但无法从 codebehind 访问。
<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>

我认为您应该尝试使用RowDataBound。 - deostroll
1个回答

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>

Codebehind

   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;
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接