如何将数据表显示为结果列表? [英] How to display datatable as a result list?
本文介绍了如何将数据表显示为结果列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个DataTable但是动态的,每次行数可能不同。我想将行显示为结果列表,如下例所示:
// DataTable
ID |公司|网站
1 | Dgroup | www.dgroup.com
2 |刘| www.liu.com
我想要的结果是这样的:
< h3 > Dgroup < / h3 >
< a href = www.dgroup.com > 访问网站< / a >
< br / >
< h3 > LIU < / h3 >
< a href = www。 liu.com > 访问网站< / a >
我该怎么办?这样做?
解决方案
你可以使用转发器控制
< span class =code-keyword>< asp:repeater id = Repeater1 runat = server < span class =code-attribute> xmlns:asp = #unknown >
< < span class =code-leadattribute> itemtemplate >
< h3 >
<% #Eval( 公司)%> < / h3 >
< a href = < ;% #Eval( 网站)%> < span class =code-attribute> target = _ blank > 访问网站< / a >
< / itemtemplate >
< / asp:repeater >
受保护 void Page_Load( object sender,EventArgs e)
{
if (!IsPostBack)
{
Repeater1。 DataSource = GetTable();
Repeater1.DataBind();
}
}
string conn =Data Source = .; Initial Catalog = FriendsBookDB;用户ID = sa;密码= win7;
SqlConnection con = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand(select top 3 EmpId,employee from ppb,con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
con.Open();
da.Fill(dt);
hyp1.NavigateUrl =http://+ dt.Rows [0] [1];
我的dt contains(Datatable):
1 www.facebook.com
2 www.twitter.com
3 www.google.com
I have a DataTable but a dynamic one, each time the number of rows may be different. I want to display the rows as a result list, as the example below:
//DataTable
ID | Company | Website
1 | Dgroup | www.dgroup.com
2 | LIU | www.liu.com
The result I want is like this:
<h3>Dgroup</h3>
<a href="www.dgroup.com">visit website</a>
<br/>
<h3>LIU</h3>
<a href="www.liu.com">visit website</a>
How can I do this?
解决方案
you can use repeater control
<asp:repeater id="Repeater1" runat="server" xmlns:asp="#unknown"> <itemtemplate> <h3> <%# Eval("Company")%></h3> <a href="<%# Eval("Website")%>" target="_blank">visit website</a> </itemtemplate> </asp:repeater>
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Repeater1.DataSource = GetTable(); Repeater1.DataBind(); } }
string conn = "Data Source=.;Initial Catalog=FriendsBookDB;User Id=sa;Password=win7"; SqlConnection con = new SqlConnection(conn); SqlCommand cmd = new SqlCommand("select top 3 EmpId,employee from ppb",con); SqlDataAdapter da = new SqlDataAdapter(cmd); con.Open(); da.Fill(dt); hyp1.NavigateUrl = "http://" + dt.Rows[0][1];
My dt contains (Datatable):
1 www.facebook.com 2 www.twitter.com 3 www.google.com
这篇关于如何将数据表显示为结果列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文