从两个表中绑定中继器 [英] Bind Repeater from two tables
本文介绍了从两个表中绑定中继器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从两个表中绑定这个中继器?
注意:两个表之间的关系:来自Users表的Services表中的userId forgien键。
< ; asp:repeater runat = server ID = rpServices >
< itemtemplate >
[Userphoto] [用户名] //来自users表的这两个字段。
介绍:
[ServiceName]
in
[ServiceField]范围[ServiceCost] //来自Services表的这三个字段。
< / itemtemplate >
我想从两个表中绑定转发器的代码??
解决方案
请尝试以下代码。
1.创建转发器(你可以改变设计,我用表来显示数据)
< asp:Repeater ID = rpServices runat = 服务器 >
< HeaderTemplate >
< table < span class =code-keyword>>
< tr >
< th > Userphoto < / th >
< th > 用户名< / th >
< th > ServiceName < / th >
< th > ServiceField < / th >
< th > ServiceCost < / th >
< / tr >
< / HeaderTemplate >
< < span class =code-leadattribute> ItemTemplate >
< tr >
< td > <% #Eval( Userphoto)< span class =code-pagedirective>%> < / td >
< td < span class =code-keyword>> <% #Eval( 用户名)%> < / td >
< ; td > <% #Eval( ServiceName)%> ; < / td >
< td > <% #Eval( ServiceField )%> < / td >
< td > <% #Eval( ServiceCost)%> < / td >
< span class =code-keyword>< / tr >
< / ItemTemplate >
< FooterTemplate >
< / table >
< / FooterTemplate >
< / as p:中继器 >
2.创建一个转发器填充的方法
private void RepeaterBind()
{
string connectionString = 数据源= sandeepss-PC;初始目录= CodeFirst;用户ID = sa; Password = knowdev;
SqlConnection con = new SqlConnection(connectionString);
con.Open();
SqlCommand cmd = new SqlCommand( @ 选择u.Userphoto ,u.Username,
s.ServiceName,s.ServiceField,
s.ServiceCost FROM Users as u inner join service on s on u.userId = s.userId,con);
IDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load (dr);
rpServices.DataSource = dt;
rpServices.DataBind();
}
3.在填写转发器控件的地方调用此方法。例如,我在Page_Load
<上调用它br $> b $ b
受保护 void Page_Load( object sender,EventArgs e)
{
if ( !Page.IsPostBack)
{
RepeaterBind();
}
}
您没有绑定到两个表,您绑定到涉及这两个表的查询的结果。那么,你对那些表的查询是什么?
为什么你绑定两个表你有没有感觉到a + b = c然后在这个(a + b)结果中转到你的c方案结果将转到sql的一个数据表使用连接并执行它
问候......:)
Hey my friends, I have two tables Users & Services and my repeater has fields from the two tables.
How can I bind this repeater from the both tables ??
Note : Relation between the two tables: userId forgien key in Services table from Users table.
<asp:repeater runat="server" ID="rpServices">
<itemtemplate>
[Userphoto] [Username] // these two fields from users table.
Introduce:
[ServiceName]
in
[ServiceField] in range [ServiceCost] // these three fields from Services table.
</itemtemplate>
I want the code which bind the repeater from the two tables ??
解决方案
Please try following code.
1. create repeater (you can change design, I am using table to show data)
<asp:Repeater ID="rpServices" runat="server"> <HeaderTemplate> <table> <tr> <th>Userphoto</th> <th>Username</th> <th>ServiceName</th> <th>ServiceField</th> <th>ServiceCost</th> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td><%# Eval("Userphoto")%></td> <td><%# Eval("Username")%></td> <td><%# Eval("ServiceName")%></td> <td><%# Eval("ServiceField")%></td> <td><%# Eval("ServiceCost")%></td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater>
2. Create a method for repeater fill up
private void RepeaterBind() { string connectionString = "Data Source=sandeepss-PC;Initial Catalog=CodeFirst;User ID=sa; Password=knowdev"; SqlConnection con = new SqlConnection(connectionString); con.Open(); SqlCommand cmd = new SqlCommand(@"Select u.Userphoto,u.Username, s.ServiceName,s.ServiceField, s.ServiceCost FROM Users as u inner join Services as s on u.userId=s.userId", con); IDataReader dr = cmd.ExecuteReader(); DataTable dt = new DataTable(); dt.Load(dr); rpServices.DataSource = dt; rpServices.DataBind(); }
3. Call this method where you filling repeater control. For example I am calling it onPage_Load
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { RepeaterBind(); } }
You don't bind to two tables, you bind to the result of a query involving those two tables. So, what's your query for those tables?
why you are binding two tables have u ever feel that a+b=c then in this (a+b) result goes to c in your scenario result will be goes to the one datatable use joins of sql and do it
regards...:)
这篇关于从两个表中绑定中继器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文