访问GridView ItemTemplate控件ASP.NET

时间:2015-10-26 09:04:08

标签: c# asp.net gridview

我在ASPX页面上使用ASP.NET GridView:

<asp:GridView ID="GrdLimitAndUtilization" runat="server" AutoGenerateColumns="False" GridLines="None"
                    Width="99%" meta:resourcekey="GrdAccountListResource1" OnRowDataBound="GrdLimitAndUtilization_RowDataBound"
                    ShowHeader="True" rowstyle-cssclass="rowHover" ClientIDMode="Static" CssClass="gridView">
                    <Columns>
<asp:TemplateField HeaderText="Excess" meta:resourcekey="Excess">
                            <HeaderStyle HorizontalAlign="Left" Width="120px" CssClass="gridheader" />
                            <HeaderTemplate> <asp:Label ID="col5a" Text="Excess" runat="server"></asp:Label></HeaderTemplate>
                            <ItemTemplate>
                                <asp:HyperLink ID="lnkExcess" runat="server" value='' Text='<%# Bind("Excess") %>'
                                    meta:resourcekey="HyperLink1Resource1"></asp:HyperLink>
                                <asp:Label ID="Excess_Currency" runat="server" Text='<%# Bind("Currency") %>'></asp:Label>
                            </ItemTemplate>
                            <ItemStyle Width="120px" HorizontalAlign="Left" CssClass="customerProductItemTemp gridviewLeftMargin" />
                        </asp:TemplateField>
                    </Columns>
</asp:GridView>

我知道我可以通过以下代码访问GridView列:

GrdLimitAndUtilization.Rows[0].Cells[0].ForeColor = System.Drawing.Color.Green;

但是,如何才能访问ID =“lnkExcess”的HyperLink控件的值/文本?

感谢。

1 个答案:

答案 0 :(得分:2)

您可以使用RowDataBound这样的事件: -

protected void GrdLimitAndUtilization_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        HyperLink lnkExcess = (HyperLink)e.Row.FindControl("lnkExcess");
        //Access hyperlink's properties here.
     }
}