如何使用嵌套转发器和每个转发器与asp.net中的不同数据源绑定

问题描述:

其中

1.第一个转发器与单行数据源绑定

2.第二个转发器与多行数据源绑定。

3.第三个转发器再次与单行数据源绑定。



详细信息

假设.....

a。第一个数据源包含学校信息,如学校名称,地址,联系号码等。

b。第二个数据源包含多个学生记录,如姓名,父亲姓名,母亲姓名,班级,流,部分和滚动否

c。第三个数据源包含特定课程的考试时间表,如考试日期,日期,科目名称,类型,考试班次,考试开始时间和结束时间。

In which
1. First repeater is bind with a single row data source
2. Second repeater is bind with a multiple rows data source.
3. Third repeater is again bind with a single row data source.

in details
Suppose.....
a. First Data Source is contain School Info Like School Name,Address,contact No & etc.
b. Second Data Source contain multiple Student records such as Name ,Father Name ,Mother Name,Class,Stream,Section & Roll No
c. Third Data Source contain Exam Time Table for a particular class such as Exam Date,Day,Subject Name,Type,Exam Shift,Exam Start Time & End Time.

< asp:Repeater ID =Parent_Repeaterrunat =server

onitemdatabound =Parent_Repeater_ItemDataBound>



< HeaderTemplate> < table bgcolor =#E6AF93width =1100px>< tr>< td colspan =6align =center>

< asp:Label ID = Label28runat =serverText =Customer ReportForeColor =Black>< / asp:Label>< / td>< / tr>< / table>< / HeaderTemplate>

< ItemTemplate>



< table bgcolor =#FCFCFCwidth =1100pxborder =1bordercolor =black >



< tr>< td colspan =6align =center>

< asp:Label ID =Label30runat =serverText =Level:CssClass =txtt>< / asp:Label>

< asp:Label ID =Label29runat =serverText ='<%#Eval(promotion_name)%>'Font-Bold =trueForeColor =Black>< / asp:Label&gt ;< / td>< / tr>

< tr>< td colspan =6height =20px>< / td>< / tr>

< tr>



< td width =175px>

< asp:标签ID =Label1CssClass =txttrunat =serverText =Customer Name>< / asp:Label>

< / td>

< td width =175px>

< asp:Label ID =Label27Visible =falserunat =serverText ='<%#Eval(user_id )%>'CssClass =txtt>< / asp:标签>



< asp:标签ID =Label2runat =server Text ='<%#Eval(name)%>'CssClass =txtt>< / asp:Label>

< / td>

< td width =200px>

< asp:Label ID =Label3CssClass =txttrunat =serverText =Product Name>< / asp:标签>

< / td>

< td width =200px>

< asp:Label ID =Label4runat =serverCssClass =txttText ='<%#Eval(product_name)%&gt ;'>< / asp:标签>

< / td>

< td width =175px>

< asp:Label ID =Label5runat =serverCssClass =txttText =User Name>< / asp:Label>

< / td>

< td width =175px>

< asp:Label ID =Label6runat =serverCssClass =txttText ='<% #Eval(username)%>'>< / asp:标签>

< / td>

< / tr>

< tr>

< td width =175px>

< asp:Label ID =Label7runat =serverCssClass =txttText =加入日期>< / asp:标签>

< / td>

< td width =175px&gt ;

< asp:Label ID =Label8runat =serverCssClass =txttText ='<%#Eval(date_Of_join,{0:dd / MM / yyyy})%>'>< / asp:标签>

< / td>

< td wid th =200px>

< asp:Label ID =Label9runat =serverCssClass =txttText =Product Price>< / asp:Label>

< / td>

< td width =200px>

< asp:Label ID =Label10runat =serverCssClass =txttText ='<%#Eval(product_price)%>'>< / asp:Label>

< / td>

< td width =175px>

< asp:标签ID =Label11runat =serverCssClass =txttText =Password&gt ;< / asp:标签>

< / td>

< td width =175px>

< asp :Label ID =Label12runat =serverCssClass =txttText ='<%#Eval(password)%>'>< / asp:Label>

< / td>

< / tr>





< tr>

< td colspan =6>

< br />

< center>

&lt ; asp:Repeater ID =Child_Repeaterrunat =server>

< HeaderTemplate>< table width =1050pxbgco lor =#F2D7C9border =1>

< tr>

< td width =150px>

< asp:Label ID =Label13CssClass =txttrunat =serverText =Level>< / asp:Label>

< / td>

< td width =150px>

< asp:Label ID =Label14CssClass =txttrunat =serverText =Customer Name >< / asp:标签>

< / td>

< td width =150px>

< asp:标签ID =Label15CssClass =txttrunat =serverText =加入日期>< / asp:标签>

< / td>

< td width =150px>

< asp:Label ID =Label16CssClass =txttrunat =serverText =Product Name&gt ;< / asp:标签>

< / td>

< td width =150px>

< asp :Label ID =Label17CssClass =txttrunat =serverText =Product Price>< / asp:Label>

< / td>

< td width =150px>

< asp:Label ID =Label18CssCla ss =txttrunat =serverText =User Name>< / asp:Label>

< / td>

< td width =150px>

< asp:Label ID =Label19CssClass =txttrunat =serverText =Password>< / asp:Label>

< / td>

< / tr>

< / table>< / HeaderTemplate>

< ItemTemplate>

< table width =1050pxbgcolor =whiteborder =1>

< tr>

< td width =150px>

< asp:标签ID =Label20CssClass =txttrunat =serverText ='<%#Eval( promotion_name)%>'>< / asp:标签>

< / td>

< td width =150px>

< asp:Label ID =Label21CssClass =txttrunat =serverText ='<%#Eval(name)%>'>< / asp:Label&gt ;

< / td>

< td width =150px>

< asp:Label ID =Label22 CssClass =txttrunat =serverText ='<%#Eval(date_Of_join,{0:dd / MM / yyyy }))%>'>< / asp:标签>

< / td>

< td width =150px>

< asp:Label ID =Label23CssClass =txttrunat =serverText ='<%#Eval(product_name)%>'>< / asp:Label&gt ;

< / td>

< td width =150px>

< asp:Label ID =Label24 CssClass =txttrunat =serverText ='<%#Eval(product_price)%>'>< / asp:Label>

< / td>

< td width =150px>

< asp:Label ID =Label25CssClass =txttrunat =serverText ='< %#Eval(用户名)%>'>< / asp:标签>

< / td>

< td width =150px >

< asp:Label ID =Label26CssClass =txttrunat =serverText ='<%#Eval(password)%>'>< ; / asp:标签>

< / td>

< / tr>

< / table>

< / ItemTemplate>

< / asp:Repeater>

< / center>

< br />

< / td>

< / tr>

< / table>

< br />

< / ItemTemplate>





< / asp :中继器>











在C#中,

con.Open();

SqlDataAdapter adp3 = new SqlDataAdapter(select * from table1,con);

adp3.Fill(ds);

Parent_Repeater.DataSource = ds;

Parent_Repeater.DataBind();





将所有子转发器绑定在父转发器项目数据绑定中,



protected void Parent_Repeater_ItemDataBound(object sender,RepeaterItemEventArgs e)

{

尝试

{

if(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)

{



Label lbl_userid =(Label)e.Item.FindControl(Label27 );

id = int.Parse(lbl_userid.Text);



Repeater Chi_Repeat =(Repeater)e.Item.FindControl( Child_Repeater);

con.Open();

数据集ds1;

SqlDataAdapter adp1 = new SqlDataAdapter(select * from table2 ,con);

adp1.Fill(ds1);

Chi_Repeat.DataSource = ds1;

Chi_Repeat.DataBind(); nd() ;

}



}

catch(例外情况)

{

Response.Write(ex.ToString());

}

}
<asp:Repeater ID="Parent_Repeater" runat="server"
onitemdatabound="Parent_Repeater_ItemDataBound">

<HeaderTemplate><table bgcolor="#E6AF93" width="1100px"><tr><td colspan="6" align="center">
<asp:Label ID="Label28" runat="server" Text="Customer Report" ForeColor="Black"></asp:Label></td></tr></table></HeaderTemplate>
<ItemTemplate>

<table bgcolor="#FCFCFC" width="1100px" border="1" bordercolor="black" >

<tr><td colspan="6" align="center">
<asp:Label ID="Label30" runat="server" Text="Level: " CssClass="txtt"></asp:Label>
<asp:Label ID="Label29" runat="server" Text='<%#Eval ("promotion_name") %>' Font-Bold="true" ForeColor="Black"></asp:Label></td></tr>
<tr><td colspan="6" height="20px"></td></tr>
<tr>

<td width="175px">
<asp:Label ID="Label1" CssClass="txtt" runat="server" Text="Customer Name"></asp:Label>
</td>
<td width="175px">
<asp:Label ID="Label27" Visible="false" runat="server" Text='<%#Eval ("user_id") %>' CssClass="txtt"></asp:Label>

<asp:Label ID="Label2" runat="server" Text='<%#Eval ("name") %>' CssClass="txtt"></asp:Label>
</td>
<td width="200px">
<asp:Label ID="Label3" CssClass="txtt" runat="server" Text="Product Name"></asp:Label>
</td>
<td width="200px">
<asp:Label ID="Label4" runat="server" CssClass="txtt" Text='<%#Eval ("product_name") %>'></asp:Label>
</td>
<td width="175px">
<asp:Label ID="Label5" runat="server" CssClass="txtt" Text="User Name"></asp:Label>
</td>
<td width="175px">
<asp:Label ID="Label6" runat="server" CssClass="txtt" Text='<%#Eval ("username") %>'></asp:Label>
</td>
</tr>
<tr>
<td width="175px">
<asp:Label ID="Label7" runat="server" CssClass="txtt" Text="Date of Join"></asp:Label>
</td>
<td width="175px">
<asp:Label ID="Label8" runat="server" CssClass="txtt" Text='<%#Eval ("date_Of_join","{0:dd/MM/yyyy}") %>'></asp:Label>
</td>
<td width="200px">
<asp:Label ID="Label9" runat="server" CssClass="txtt" Text="Product Price"></asp:Label>
</td>
<td width="200px">
<asp:Label ID="Label10" runat="server" CssClass="txtt" Text='<%#Eval ("product_price") %>'></asp:Label>
</td>
<td width="175px">
<asp:Label ID="Label11" runat="server" CssClass="txtt" Text="Password"></asp:Label>
</td>
<td width="175px">
<asp:Label ID="Label12" runat="server" CssClass="txtt" Text='<%#Eval ("password") %>'></asp:Label>
</td>
</tr>


<tr>
<td colspan="6">
<br />
<center>
<asp:Repeater ID="Child_Repeater" runat="server">
<HeaderTemplate><table width="1050px" bgcolor="#F2D7C9" border="1">
<tr>
<td width="150px">
<asp:Label ID="Label13" CssClass="txtt" runat="server" Text="Level"></asp:Label>
</td>
<td width="150px">
<asp:Label ID="Label14" CssClass="txtt" runat="server" Text="Customer Name"></asp:Label>
</td>
<td width="150px">
<asp:Label ID="Label15" CssClass="txtt" runat="server" Text="Date of Join"></asp:Label>
</td>
<td width="150px">
<asp:Label ID="Label16" CssClass="txtt" runat="server" Text="Product Name"></asp:Label>
</td>
<td width="150px">
<asp:Label ID="Label17" CssClass="txtt" runat="server" Text="Product Price"></asp:Label>
</td>
<td width="150px">
<asp:Label ID="Label18" CssClass="txtt" runat="server" Text="User Name"></asp:Label>
</td>
<td width="150px">
<asp:Label ID="Label19" CssClass="txtt" runat="server" Text="Password"></asp:Label>
</td>
</tr>
</table></HeaderTemplate>
<ItemTemplate>
<table width="1050px" bgcolor="white" border="1">
<tr>
<td width="150px">
<asp:Label ID="Label20" CssClass="txtt" runat="server" Text='<%#Eval ("promotion_name") %>'></asp:Label>
</td>
<td width="150px">
<asp:Label ID="Label21" CssClass="txtt" runat="server" Text='<%#Eval ("name") %>'></asp:Label>
</td>
<td width="150px">
<asp:Label ID="Label22" CssClass="txtt" runat="server" Text='<%#Eval ("date_Of_join","{0:dd/MM/yyyy}") %>'></asp:Label>
</td>
<td width="150px">
<asp:Label ID="Label23" CssClass="txtt" runat="server" Text='<%#Eval ("product_name") %>'></asp:Label>
</td>
<td width="150px">
<asp:Label ID="Label24" CssClass="txtt" runat="server" Text='<%#Eval ("product_price") %>'></asp:Label>
</td>
<td width="150px">
<asp:Label ID="Label25" CssClass="txtt" runat="server" Text='<%#Eval ("username") %>'></asp:Label>
</td>
<td width="150px">
<asp:Label ID="Label26" CssClass="txtt" runat="server" Text='<%#Eval ("password") %>'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
</center>
<br />
</td>
</tr>
</table>
<br />
</ItemTemplate>


</asp:Repeater>





In C#,
con.Open();
SqlDataAdapter adp3=new SqlDataAdapter("select * from table1",con);
adp3.Fill(ds);
Parent_Repeater.DataSource = ds;
Parent_Repeater.DataBind();


Bind all child Repeater in Parent Repeater item Databound,

protected void Parent_Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
try
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{

Label lbl_userid = (Label)e.Item.FindControl("Label27");
id = int.Parse(lbl_userid.Text);

Repeater Chi_Repeat = (Repeater)e.Item.FindControl("Child_Repeater");
con.Open();
dataset ds1;
SqlDataAdapter adp1=new SqlDataAdapter("select * from table2",con);
adp1.Fill(ds1);
Chi_Repeat.DataSource = ds1;
Chi_Repeat.DataBind();nd();
}

}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}