How to set a column in gridview as hyperlink which is autogenerated

吃可爱长大的小学妹 提交于 2021-02-07 19:51:05

问题


I want to make the gridview.columns[0] as hyperlink. I tried so many work around mentioned in different sites. I am binding a list<> to the grid. and I need to make the first column as hyperlink and upon clicking that link, it should be redirected to a page with the corresponding item. Which event I need to use and how can I pass that value from the list?


回答1:


void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var firstCell = e.Row.Cells[0];
        firstCell.Controls.Clear();
        firstCell.Controls.Add(new HyperLink { NavigateUrl = firstCell.Text, Text = firstCell.Text });
    }
}

Be warned that if you bind data to grid only first time page loaded then your changes will disappear.




回答2:


You have to make that column as Template Column

<asp:TemplateField HeaderText="">
  <ItemTemplate>
   <asp:HyperLink ID="HyperLink1" runat="server" Text="test" NavigateUrl='<%# Eval("fieldName", "show.aspx?ID={0}") %>'></asp:HyperLink>
  </ItemTemplate>
</asp:TemplateField>


来源:https://stackoverflow.com/questions/6277653/how-to-set-a-column-in-gridview-as-hyperlink-which-is-autogenerated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!