问题描述
我有一个简单的下拉和中继器控制page.on页面提交Repeater控件绑定到datasource1其中有3列。
I have a page with a simple dropdown and repeater control in page.on submit repeater control is bound to datasource1 which has 3 columns.
现在我的要求是,如果我选择选项2,应该结合datasource2其中包含4列。
Now my requirement is if i select option2 , it should bind to datasource2 which contains 4 columns.
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<HeaderTemplate>
<tr>
<th align="left">Header1</th>
<th align="left">Header3</th>
<th align="left">Header2</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="Header1" runat="server" /></td>
<td>
<asp:Label ID="Header3" runat="server" /></td>
<td>
<asp:Label ID="Header2" runat="server" /></td>
</tr>
</ItemTemplate>
是否有可能为同一个中继器动态绑定到不同的数据源?我怎么可以指定在运行时头部模板和项目模板?可这种情况下只有一个中继器控制和多个异构数据源来实现?
Is it possible for the same repeater to dynamically bind to heterogeneous data sources?How can i specify the header template and item templates at run- time? Can this scenario be implemented with just one repeater control and multiple heterogeneous data sources?
推荐答案
在下面感觉code会帮助你。
Feels below code will help you .
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<HeaderTemplate>
<tr class="">
<asp:Repeater ID="Header1" runat="server">
<ItemTemplate>
<th align="left"><%# Container.DataItem %>
</th>
</ItemTemplate>
</asp:Repeater>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class="">
<asp:Repeater ID="Item1" runat="server">
<ItemTemplate>
<td><%# Container.DataItem %>
</td>
</ItemTemplate>
</asp:Repeater>
</tr>
</ItemTemplate>
</asp:Repeater>
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
Repeater headerRepeater = e.Item.FindControl("Header1") as Repeater;
headerRepeater.DataSource = dt.Columns;
headerRepeater.DataBind();
}
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater columnRepeater = e.Item.FindControl("Item1") as Repeater;
var row = e.Item.DataItem as System.Data.DataRowView;
columnRepeater.DataSource = row.Row.ItemArray;
columnRepeater.DataBind();
}
}
或使用2个不同的用户control.First用户控件包含repeater1,第二包含repeater2.Then动态背后
or in other way using 2 different User control.First user control contain repeater1, second contain repeater2.Then dynamic add these repeaters to your page, at code behind
这篇关于如何动态绑定asp.net中继器控制数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!