Asp.net gridview中的超链接
我在gridview中有一个超链接,我希望用户点击它,它会将他们引导到一个特定的页面,也会传入gridview的第一个字段(ID),或者在会话中保存它,最好是在会话中 链接只是静态文本,所以无论他们点击什么记录,我都想让他们进入同一页面,但记录ID可用 只是不知道如何将其添加到超链接的NavigateUrl 任何提示,谢谢Asp.net gridview中的超链接,asp.net,gridview,Asp.net,Gridview,我在gridview中有一个超链接,我希望用户点击它,它会将他们引导到一个特定的页面,也会传入gridview的第一个字段(ID),或者在会话中保存它,最好是在会话中 链接只是静态文本,所以无论他们点击什么记录,我都想让他们进入同一页面,但记录ID可用 只是不知道如何将其添加到超链接的NavigateUrl 任何提示,谢谢 您可以处理Row_数据绑定事件以查找超链接控件并更新NavigateUrl属性 您可以添加简单的html控制文本链接,它将产生相同的html 使用HyperLink控件,然后
使用HyperLink控件,然后为RowDataBound事件编写事件处理程序函数,如下所示:
protected void OnRowDataBound(object source, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink hyperLink = e.Row.FindControl("hyperLinkID") as HyperLink;
// example, adjust this to your needs.
hyperLink.NavigateUrl = "~/detail.aspx?id=" + DataBinder.Eval(e.Row.DataItem, "ID");
}
}
不确定您为什么使用服务器控件而不是HTML标记 有两种方法可以做到这一点 1) 如果是静态链接,只需在页面名称前加前缀,并在标记中附加id即可。 前
您可以轻松地在GridView的标记中生成URL,而无需使用代码。您需要做的是:
{0}
<asp:Hyperlink DataNavigateUrlFields="ProductId" DataNavigateUrlFormatString="details.aspx?id={0} />
和,以获取更多详细信息
protected void MyGridview_ItemDataBound(object sender, ListViewItemEventArgs e)
{
HtmlAnchor AncImage = e.Item.FindControl("AncImage") as HtmlAnchor;
AncImage.href="myPage.aspx"/id=" + DataBinder.Eval(e.Row.DataItem, "ID"); ;
//the id is the value that you want to append for redirection
}
<asp:Hyperlink DataNavigateUrlFields="ProductId" DataNavigateUrlFormatString="details.aspx?id={0} />