【发布时间】:2011-08-31 18:42:57
【问题描述】:
我在 gridview 中有一个超链接,我希望用户单击它,它将他们引导到特定页面,并且还传入 gridview 的第一个字段(ID)或将其保存在会话中,最好是在会话中。
该链接只是静态文本,因此无论他们点击什么记录,我都希望将他们带到同一页面,但该记录 ID 可用。
只是不确定如何将其添加到超链接的 NavigateUrl。
感谢任何提示,谢谢
【问题讨论】:
我在 gridview 中有一个超链接,我希望用户单击它,它将他们引导到特定页面,并且还传入 gridview 的第一个字段(ID)或将其保存在会话中,最好是在会话中。
该链接只是静态文本,因此无论他们点击什么记录,我都希望将他们带到同一页面,但该记录 ID 可用。
只是不确定如何将其添加到超链接的 NavigateUrl。
感谢任何提示,谢谢
【问题讨论】:
您可以轻松地在 GridView 的标记中生成 URL,而无需借助代码。你需要做的是:
{0} 改为。例如
<asp:Hyperlink DataNavigateUrlFields="ProductId" DataNavigateUrlFormatString="details.aspx?id={0} />
在运行时呈现控件时,您会发现对于每一行,{0} 都替换为 ProductId 列的值。
请参阅String.Format 和DataNavigateUrlFormatString 了解更多详情。
【讨论】:
【讨论】:
使用 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 即可。 例如
<a href='myPage.aspx<%#Eval("YourID")%>'><strong>Click me to navigate</strong></a>
2) 给 a 标签一些 id 并让它运行在服务器上处理数据绑定事件并将值绑定到它。
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
}
【讨论】: