开发者

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

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 开发者_Python百科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?


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.


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜