评论insérerunebalise html <a> dans le titre de la colonne d&#39;un GridView en asp.net

时间:2017-05-11 09:48:36

标签: c# asp.net gridview

I am working for the first time in asp.net/webforms for my internship. I have to display a GridView fill with information coming from class. I already create my GridView dynamically with BoundField. However I want the HeaderText of each column to be a link toward the configuration page of the object represented by the column. But I can't find how to insert this link dynamically... Maybe I should try with Javascript, but how ?

The GridView code in .aspx :

<asp:GridView ID="GridView_Objet" runat="server" AutoGenerateColumns="false">
   <Columns>
   </Columns>
</asp:GridView>

The GridView code in .cs :

foreach (Objet objet in m_objets)
{
    dataColumn = new DataColumn(objet.getNom(), typeof(string));
    dataTable.Columns.Add(dataColumn);

    boundField = new BoundField();
    boundField.HeaderText = string.Format("<a href='/dc6_consignes_decapage.aspx'>" + objet.getNom() + "</a>");
    boundField.DataField = objet.getNom();
    GridView_Consigne.Columns.Add(boundField);
}

Here the actual GridView

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您可以像这样访问GridView中的标题,并将控件添加到单元格。

HyperLink hl = new HyperLink();
hl.NavigateUrl = "/Page2.aspx";
hl.Text = "Link to Page 2";

GridView1.HeaderRow.Cells[0].Controls.Add(hl);

这需要在每个页面加载时完成,其中包括PostBack,因为它们是动态添加的控件。