绑定链接的GridView [英] Bind Links To GridView
本文介绍了绑定链接的GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何呈现的WebForms的GridView列链接?
How can I render links in columns of WebForms GridView?
我有以下的code
SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.UsersContacts WHERE UserId='vika'", con);
con.Open();
var list1 = new List<string>();
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var node = reader[1];
list1.Add(node.ToString());
}
}
con.Close();
GridView1.DataSource = list1;
GridView1.DataBind();
和我想要做的事,如 list1.Add(&LT; A HREF ='#'&gt;中+ node.ToString()+&LT; A&gt;中);
并使其在GridView的我一个链接。
and I want to do something such as list1.Add("<a href='#'>"+node.ToString()+"<a>");
and make it a link in my GridView.
推荐答案
我宁愿做这在GridView的模板定义,而不是通过链接从数据源。你的GridView的定义可以有的asp:HyperLinkField字段
或 ASP:超链接
字段,并根据需要将数据绑定
I would rather do this at the gridview template definition instead of passing the link from the datasource. Your gridview definition can have the asp:HyperLinkField
or asp:HyperLink
field and bind the data as required.
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:HyperLinkField
HeaderText="View Details"
DataNavigateUrlFields="node"
DataNavigateUrlFormatString="~/TargetPage.aspx?Id={0}"
DataTextField="node"
/>
</Columns>
</asp:GridView>
或者
<asp:HyperLink runat="server" NavigateUrl='<%# Eval("node", "~/TargetPage.aspx?Id={0}") %>' Text="View Details" />
这篇关于绑定链接的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文