[英]Binding a Dropdown inside repeater
I'm binding a DropDownList
inside a repeater and below is my mark up 我在一个转发器中绑定了一个
DropDownList
,下面是我的标记
<asp:Repeater ID="Repeater2" runat="server"
onitemdatabound="Repeater2_ItemDataBound">
<HeaderTemplate>
<asp:DropDownList ID="DropDownList8" runat="server">
<asp:ListItem>Select</asp:ListItem>
<asp:ListItem>Left</asp:ListItem>
<asp:ListItem>Right</asp:ListItem>
<asp:ListItem>SubString</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList7" runat="server">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="Button" />
</HeaderTemplate>
<FooterTemplate>
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:Repeater>
protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
try
{
if (e.Item.ItemType == ListItemType.Header)
{
DataTable dt = new DataTable();
dt = Common.LoadExample();
DropDownList ddl = (DropDownList)e.Item.FindControl("DropDownList2");
ddl.DataSource = dt;
ddl.DataTextField = "Name";
ddl.DataValueField = "Name";
}
}
catch (Exception ex)
{
throw;
}
}
but I'm unable to fill the dropdown list as the breakpoint is skipping e.Item.ItemType == ListItemType.Header
here. 但由于断点在此处跳过
e.Item.ItemType == ListItemType.Header
,因此无法填充下拉列表。 How can I solve this? 我该如何解决?
For header row binding, move your code from ItemDataBound
to ItemCreated
对于标题行绑定,将代码从
ItemDataBound
移至ItemCreated
-- updated answer -- -更新的答案-
This will ONLY work once you add ItemTemplate
without item template, setting blank datasource will not bind the grid in expected way. 仅当您添加不带项目模板的
ItemTemplate
这才起作用,设置空白数据源将不会以预期的方式绑定网格。
Please add item template and bind some dummy data 请添加项目模板并绑定一些虚拟数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.