[英]ASP:NET Binding dropdownlist inside of the repeater
What i want to do is binding and dropdown list inside of the repeater but I didn't make it. 我想要做的是转发器内部的绑定和下拉列表,但我没有成功。 I'am getting following error at the marked line: System.NullReferenceException. 我在标记的行中遇到以下错误:System.NullReferenceException。 Any help would be appriciated. 任何帮助都会得到满足。
Back-end: 后端:
protected void shoeRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
FixbayDBDataContext db = new FixbayDBDataContext();
var colors = from c in db.ColorTbls select new { ColorID = c.ColorID, ColorName = c.ColorName, };
DropDownList ddl = (DropDownList)e.Item.FindControl("colorList1");
ddl.DataSource = colors; //ERROR LINE
ddl.DataTextField = "ColorName";
ddl.DataValueField = "ColorName";
}
Front-End: 前端:
<ajaxToolkit:TabPanel ID="TabPanel5" runat="server">
<HeaderTemplate>Show Shoes</HeaderTemplate>
<ContentTemplate runat="server">
<asp:Repeater ID="shoeRepeater" OnItemCreated="shoeRepeater_ItemDataBound" runat="server">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<table border="1" style="border-color:#ff9900; width:400px; font-weight:bold; font-family:'Oswald', Arial, sans-serif;">
<tr>
<td rowspan="6" style="width:150px; height:150px;">
<image src="shoeImages/<%#DataBinder.Eval(Container.DataItem,"ImagePath") %>"></image>
</td>
</tr>
<tr>
<td>
<%#DataBinder.Eval(Container.DataItem,"BrandName") %> <%#DataBinder.Eval(Container.DataItem,"ModelName") %>
</td>
</tr>
<tr>
<td>
Price: $<%#DataBinder.Eval(Container.DataItem,"Price") %>
</td>
</tr>
<tr>
<td>
Size: <%#DataBinder.Eval(Container.DataItem,"Size") %>
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="colorList1" onchange="get(this)" runat="server">
</asp:DropDownList>
Color: <%#DataBinder.Eval(Container.DataItem,"PrimaryColor") %> - <%#DataBinder.Eval(Container.DataItem,"SecondaryColor") %>
</td>
</tr>
<tr>
<td>
Quantity: <%#DataBinder.Eval(Container.DataItem,"Quantity") %>
</td>
</tr>
</table>
</ItemTemplate>
<FooterTemplate></FooterTemplate>
</asp:Repeater>
</ContentTemplate>
</ajaxToolkit:TabPanel>
You have subscribed to the wrong event: 您订阅了错误的活动:
OnItemCreated="shoeRepeater_ItemDataBound"
This should be 这应该是
OnItemDataBound="shoeRepeater_ItemDataBound"
Here is the solution 这是解决方案
protected void shoeRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
FixbayDBDataContext db = new FixbayDBDataContext();
var colors = from c in db.ColorTbls select new { ColorID = c.ColorID, ColorName = c.ColorName, };
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DropDownList ddl = (DropDownList)e.Item.FindControl("colorList1");
ddl.DataSource = colors;//Or any other datasource.
ddl.DataTextField = "ColorName";
ddl.DataValueField = "ColorName";
ddl.DataBind();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.