How bind Checklistbox inside repeater control having datasource as LINQ
i my website i am using Repeater control which further contain checkboxlist control.
Now my problem is that i have successfully bind "Parameter Type" in repeater control but when i am binding checkbox values, it does't appears in display
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<h4>
<%#Container.DataItem%></h4>
<asp:CheckBoxList ID="chkParList" runat="server" RepeatDirection="Horizontal"
DataTextField = >
</asp:CheckBoxList>
<br /><br />
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:Repeater>
In *.cs file following are my code
IMonitoringDataInfo objMonitoringDataInfo = new ChannelFactory<IMonitoringDataInfo>("MonitoringDataInfo").CreateChannel();
Collection<ParameterDetailDTO> clParameterDetailDTO = objMonitoringDataInfo.GetAllParameters(idList, out errorCode);
var parameters = (from resx in clParameterDetailDTO
select resx.ParameterType).Distinct();
Repeater1.DataSource = parameters.ToList();
Repeater1.DataBind();
counter = Repeate开发者_如何转开发r1.Items.Count;
while (i < counter - 1)
{
foreach (var parType in parameters)
{
var items = from resx in clParameterDetailDTO
where resx.ParameterType.ToLower().Contains(parType.ToLower())
select new { resx.ParameterName, resx.ParameterID };
((CheckBoxList)(Repeater1.Items[i].FindControl("chkParList"))).DataSource = items;
((CheckBoxList)(Repeater1.Items[i].FindControl("chkParList"))).DataTextField = "ParameterName";
((CheckBoxList)(Repeater1.Items[i].FindControl("chkParList"))).DataValueField = "ParameterID";
((CheckBoxList)(Repeater1.Items[i].FindControl("chkParList"))).DataBind();
}
i++;
}
I am using LINQ as datasource
Please help?
add OnItemDataBound event to Repeater :
<asp:Repeater ID="Repeater1" OnItemDataBound="Repeater1_ItemDataBound" runat="server">
then in code behind do something like this :
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
var parameters = (from resx in clParameterDetailDTO
select resx.ParameterType).Distinct();
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
CheckBoxList chkParList = (e.Item.FindControl("chkParList") as CheckBoxList);
chkParList.DataSource = parameters.ToList();
chkParList.DataTextField = "ParameterName";
chkParList.DataValueField = "ParameterID";
chkParList.DataBind();
}
}
Try putting your CheckBoxList binding inside the ItemDataBound event of the repeater.
See:
Repeater.ItemDataBound Event
Using OnItemDataBound with Repeater in ASP.NET and C#
精彩评论