在动态创建的网格视图中创建链接
问题描述:
大家好,
我创建了一个动态网格视图,其中有四个字段:-名称,性别,联系人,电子邮件.我想将名称字段作为链接,从中我可以打开一个新页面,该页面将显示人员的完整个人资料.
请帮助我.
Hi All,
I have created a dynamic grid view, i have four fields in it namely:-Name, Gender, Contact, Email. I want to make the name field as a link from which i can open a new page which will show complete profile of person.
Please help me with this.
答
已更新:
将超链接动态添加到动态添加的GridView中
Updated:
Adding Hyperlink dynamically into a dynamic added GridView
private void NewMethod()
{
DataTable dt = SomeMethodReturnsDataTable();
GridView gv = new GridView();
PlaceHolder1.Controls.Add(gv);
gv.DataSource = dt;
gv.DataBind();
foreach (GridViewRow gr in gv.Rows)
{
HyperLink hp = new HyperLink();
hp.Text = gr.Cells[0].Text;
hp.NavigateUrl = "~/Default.aspx?name=" + hp.Text;
gr.Cells[0].Controls.Add(hp);
}
}
private DataTable SomeMethodReturnsDataTable()
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = "Data Source=.; Initial Catalog=skand; Integrated Security=true";
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "Select candidatename as 'Name', gender as 'Gender', contactmobile as 'Contact Number', contactmail as 'E-Mail' from tblUserRegister";
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Close();
}
return dt;
}
-------------------------------------------------- -----------
直接将超链接添加到ASP.NET标记页中的代码
-------------------------------------------------------------
Code for adding hyperlink directly into ASP.NET tag page
<asp:gridview id="GridView1" runat="server" autogeneratecolumns="False" xmlns:asp="#unknown">
<columns>
<asp:hyperlinkfield datatextfield="name" headertext="Name" />
</columns>
</asp:gridview>
后面的代码
code behind
protected void Page_Load(object sender, EventArgs e)
{
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.Add("name");
dt.Rows.Add("Steve Jobs");
dt.Rows.Add("Bill Gates");
GridView1.DataSource = dt;
GridView1.DataBind();
foreach (GridViewRow gvr in GridView1.Rows)
{
((HyperLink)gvr.Cells[0].Controls[0]).NavigateUrl = "~/Default.aspx?name="
+ dt.Rows[gvr.RowIndex]["name"];
}
}
http://forums.asp.net/t/1060749.aspx/1 [ ^ ]
动态添加链接到datagridview [ ^ ]