How bind Checklistbox inside repeater control having datasource as LINQ - asp.net

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 = Repeater1.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#

Related

How to find ItemIndex of outer repeater when user click radio button in the inner repeater

I have a nested repeater and need to findout the ItemIndex of the outer repeater when user click the radio button in the inner repeater.
I tried using rb.parent.parent and also rb.NamingContainer.NamingContainer but I got casting error.
I also searched different sites regarding similar problems but they were not a solution for mine.
Any comment is highly appreciated.
protected void rptrSubscriptionGroups_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Repeater rptrSubscriptions = (Repeater)e.Item.FindControl("rptrSubscriptions");
if (rptrSubscriptions != null)
{
RepeaterItem item = e.Item;
if (e.Item.ItemType == (ListItemType.Item) || e.Item.ItemType == (ListItemType.AlternatingItem))
{
DataTable SubscriptionTbl = SubscriptionsBind(SelectedLanguageID, SelectedPlatformID, SelectedCurrencyID, SelectedGroupID, 1);
rptrSubscriptions.DataSource = SubscriptionTbl;
rptrSubscriptions.DataBind();
}
}
}
protected void HighlightSubscription(object source, EventArgs e)
{
RadioButton rb = source as RadioButton;
int CurrentrepeaterItemIndex = ((RepeaterItem)rb.NamingContainer).ItemIndex;
// save current row index in a hiddden field
hiddenSelectedSubscriptionRowIndex.Value = CurrentrepeaterItemIndex.ToString();
int ParentRepeaterItemIndex = ((RepeaterItem)rb.NamingContainer.NamingContainer).ItemIndex;
hiddenSelectedSubscriptionGroupRowIndex.Value = ParentRepeaterItemIndex.ToString();
}
<asp:Repeater ID="rptrSubscriptionGroups" runat="server" OnItemDataBound="rptrSubscriptionGroups_ItemDataBound" OnItemCommand="rptrSubscriptionGroups_ItemCommand">
<ItemTemplate>
<asp:Repeater ID="rptrSubscriptions" runat="server" OnItemDataBound="rptrSubscriptions_ItemDataBound" OnItemCommand="rptrSubscriptions_ItemCommand">
<ItemTemplate>
<asp:RadioButton ID="rbSubscription" runat="server" OnCheckedChanged="HighlightSubscription" AutoPostBack="true"/>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
You said that you have tried with Parent.Parent but I think that you can do it with it :
int CurrentrepeaterItemIndex = ((RepeaterItem)rb.Parent.Parent.Parent).ItemIndex;
The First Parent is going one level up to the ItemTemplate :
<ItemTemplate>
<asp:RadioButton ID="rbSubscription" runat="server" OnCheckedChanged="HighlightSubscription" AutoPostBack="true"/>
The Second Parent is going again one level up to the inner Repeater :
<asp:Repeater ID="rptrSubscriptions" runat="server" OnItemDataBound="rptrSubscriptions_ItemDataBound" OnItemCommand="rptrSubscriptions_ItemCommand">
The Third Parent is going to the outer Repeater item and you can get the item index.
<asp:Repeater ID="rptrSubscriptionGroups" runat="server" OnItemDataBound="rptrSubscriptionGroups_ItemDataBound" OnItemCommand="rptrSubscriptionGroups_ItemCommand">
<ItemTemplate>

Nested repeater and datasource

I have a repeater nested in an other repeater.
My question is: is it possible to generate other ItemTemplate components with datasource.
If yes, how can I do it ?
For example:
ItemTemplate1 of parentRepeater
A
B
C
ItemTemplate2 of parentRepeater
D
E
F
It means that ItemTemplate of childRepeater is changed for each ItemTemplate of parentRepeater.
<asp:Repeater runat="server" ID="repeater1"
onitemdatabound="Repeater1_ItemDataBound" >
<ItemTemplate>
<!--Outer repeater -->
<asp:repeater id="repeater2" runat="server">
<itemtemplate>
<!--Inner repeater repeater -->
</itemtemplate>
</asp:repeater>
</ItemTemplate>
</asp:Repeater>
`
Here is Back end code
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//it return current row (think in terms of nested loops)
DataRowView drv = (DataRowView)e.Item.DataItem;
// get value of that row
int postID = Convert.ToInt32(drv["PostID"]);
//do what ever you want to do
Repeater2.DataSource = //some data like you did in outer repeater
Repeater2.DataBind();
}
}
If your goal is to have them alternate back and forth between two templates, then use AlternateItemTemplate.
<asp:Repeater runat="server">
<ItemTemplate>
This came from ItemTemplate.
</ItemTemplate>
<AlternateItemTemplate>
This came from AlternateItemTemplate.
</AlternateItemTemplate>
</asp:Repeater>
This is commonly used when you want to vary the background color of each row in a table.

how to bind a value of datasource to a checkbox in datalist with Eval()

I have a datalist in my asp.net page. I bind a datasource to it in codebehind
and I have a checkbox in this datalist.
var n = from gi in DataContext.Context.GalleryImages
join g in DataContext.Context.Galleries
on gi.GalleryID equals g.GalleryID
where g.UserID == UserID && gi.GalleryID==GalleryID
select new
{
GalleryID = g.GalleryID,
ImageDescription = gi.ImageDescription,
GalleryName = g.GalleryName,
ImageFileName = gi.ImageFileName,
IsAlbumImage = gi.IsAlbumImage,
ImageID=gi.ImageID
};
dlGalleryList.DataSource = n;
dlGalleryList.DataBind();
When the "IsAlbumImage " is true the checkbox should be checked.
How can I bind this property to the checkbox?
It should be bind like:
<ItemTemplate>
<asp:CheckBox id="MyCheckBox" runat="server" Checked='<%#Eval("IsAlbumImage") %>' />
</ItemTemplate>
Actually you have to ways to bind checkbox in a datalist
1- (recommended) Binding it directly from the ASP code using the Bind or Eval
<ItemTemplate>
<asp:CheckBox id="MyCheckBox" runat="server" Checked='<%#Eval("IsAlbumImage") %>' />
</ItemTemplate>
2- Binding it on the ItemDataBound Event
First you will add the event handler to your datalist control, and adds the Boolean value to a datakey to be used in itemdatabound event
<asp:DataList ID = "DataList1" OnItemDataBound="DataListItemEventHandler" DataKeys = "IsAlbumImage"/>
Then you add the C# code that bind this
protected void DataListItemEventHandler(object sender, DataListItemEventArgs e)
{
CheckBox checkbx = new CheckBox();
checkbx = (CheckBox)e.Item.FindControl("MyCheckBox");
checkbx.Checked = (bool) DataList1.DataKeys(e.Item.ItemIndex)("IsAlbumImage");
}
Like this:
<asp:CheckBox
ID="check"
runat="server"
Checked='<%# Eval("column_name").ToString().Equals("1") %>'
/>

Asp.Net ListView's Items property is always empty

Good day
I have a user control that contains a ListView which is being used to display tabs.
I am databinding the ListView and on the OnItemDataBound event adding the tab elements.
However the Items property never contains anything and is always empty (on PostBack and on ItemDataBound).
Markup:
<asp:ListView ID="SaleTabsListView" runat="server" OnItemDataBound="SaleTabsListView_OnItemDataBound" ItemPlaceholderID="SaleTabsPlaceHolder" EnableViewState="true">
<LayoutTemplate>
<div class="tabs">
<asp:PlaceHolder runat="server" ID="SaleTabsPlaceHolder" />
</div>
</LayoutTemplate>
<ItemTemplate>
<asp:PlaceHolder runat="server" ID="ItemPlaceHolder" />
</ItemTemplate>
</asp:ListView>
Code behind:
protected void SaleTabsListView_OnItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
var saleTab = ((ListViewDataItem)e.Item).DataItem as SalesTab;
if (saleTab != null)
{
var placeHolder = e.Item.FindControl("ItemPlaceHolder") as PlaceHolder;
var htmlAnchor = new HtmlAnchor();
htmlAnchor.Attributes.Add("id", saleTab.Id);
htmlAnchor.Attributes.Add("class", saleTab.IsCurrent ? currentTabClass : string.Empty);
var emptyLabel = new Label();
var label = new Label {Text = saleTab.Title};
label.Attributes.Add("id", saleTab.Id + "Label");
label.Attributes.Add("class", saleTab.IsComplete ? completeClass : incompleteClass);
emptyLabel.Controls.Add(label);
htmlAnchor.Controls.Add(emptyLabel);
placeHolder.Controls.Add(htmlAnchor);
}
}
}
So my question is: is the binding I'm doing somehow screwing with the Items property?
Thanks

Setting the Selectedindex/selectedvalue of a Dropdownlist in a repeater not working

I have a repeater with a DropDownList in it. I set the datasource of this list in the itembound event en set the selectedindex. When I debug the selectedindex is set, but when the page is done loading for all the item the default item is selected.
This is my code:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
var ddl = (DropDownList)e.Item.FindControl("DataFeedItems");
ddl.DataSource = FilterDropDownData();
ddl.DataTextField = "ColumnName";
ddl.DataValueField = "ColumnName";
ddl.DataBind();
ddl.SelectedValue = "Select";
ddl.SelectedIndex = 28;
}
protected DataTable FilterDropDownData()
{
var importedFeedColums = I make the table here;
DataRow newRow = importedFeedColums.NewRow();
newRow[0] = "Selecteren";
importedFeedColums.Rows.Add(newRow);
return importedFeedColums;
}
I Als tried to using the Databound even of the dropdown list, but this didnt work either:
protected void DataFeedItems_DataBound(object sender, System.EventArgs e)
{
var ddl = (DropDownList) (sender);
ddl.SelectedValue = "Selecteren";
ddl.Items[28].Selected = true;
}
<asp:Repeater ID="Repeater1" runat="server"
onitemdatabound="Repeater1_ItemDataBound">
<ItemTemplate>
<li>
<label><%# DataBinder.Eval(Container.DataItem, "ColumnName") %></label>
<asp:DropDownList ID="DataFeedItems" ClientIDMode="Static" runat="server" DataSource='<%# FilterDropDownData() %>'
DataTextField="ColumnName" DataValueField="ColumnName" OnDataBound="DataFeedItems_DataBound" >
</asp:DropDownList>
<input id="Hidden1" runat="server" clientidmode="Static" type="hidden" value='<%# DataBinder.Eval(Container.DataItem, "ColumnName") %>' />
</li>
</ItemTemplate>
</asp:Repeater>
When I post the form I can get the selectedvalue and text of each DropDownList. What am I doing wrong
What does FilterDropDownData() return?
Have you tried this?
ddl.Items.FindByValue("Selecteren").Selected = true;
or
ddl.Items.FindByText("Selecteren").Selected = true;
This approach will fail since a drop-down can not have multiple items selected. And setting the "Selected = true" will do exactly the same.
Only way you can set an item as selected is by using the SelectedIndex property of DropDownList.
Ex:ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue("Selecteren"));

Resources