Asp.net repeater control with checkboxlist - c#

Binding data to checkbox list inside a repeater controls ItemDataBound event.
Here is Aspx code:
<asp:Repeater ID="rptrSelectedRcds" runat="server">
<ItemTemplate>
<div id="groupsDiv" runat="server">
<div class="hidden" id="divGroupSelector" runat="server">
<asp:CheckBoxList runat="server" ID="chkLstGroups"
RepeatDirection="Vertical">
</asp:CheckBoxList>
</div>
<asp:Button ID="btnConfirmGroups" runat="server" Text="Confirm" />
</div>
</ItemTemplate>
</asp:Repeater>
Code used inside ItemDataBound to bind data to repeater control:
protected void rptrSelectedRcds_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
Int32 iProviderID = Convert.ToInt32(((Label)e.Item.FindControl("lblIndividualID")).Text);
HtmlGenericControl selectGroupsDiv = (HtmlGenericControl)e.Item.FindControl("divGroupSelector");
if (divGroupSelector != null)
{
CheckBoxList chkLstGroups = (CheckBoxList)e.Item.FindControl("chkLstGroups");
chkLstGroups.DataSource = GetStateGroupsByProvider(stateValue, iProviderID);
//This code line gives different values for each record
chkLstGroups.DataTextField = "Name";
chkLstGroups.DataValueField = "pkID";
chkLstGroups.DataBind();
}
}
I can see in firebug the ids for chkLstGroups are different, but values bind to it are same.I want different values for each different checkbox id generated as from method - GetStateGroupsByProvider
Please help me out..!
Thanks.!

Related

get selected value by drop down list in a repeater by CommandArgument of link button

I have a repeater that in it has one dropdown list and one linkbutton.
I want to get the selected value of the dropdown list by CommandArgument in linkbutton, but it just knows default value of dropdown list.
My code :
<asp:Repeater runat="server" ID="Repeater1" OnItemDataBound="Page_Load2"OnItemCommand="list_ItemCommand" >
<ItemTemplate>
<asp:DropDownList ID="dlPricelist" CssClass="width100darsad dropdownlist" runat="server" AutoPostBack="true" ViewStateMode="Enabled" >
</asp:DropDownList>
<asp:LinkButton ID="btnAddToCart" runat="server" class="btn btn-success btnAddtoCardSinglepage" CommandArgument='<%#Eval("id") %>' CommandName="addtocard">اضافه به سبد خرید</asp:LinkButton>
<asp:Label ID="xxxxxx" runat="server" Text="Label"></asp:Label>
</ItemTemplate>
</asp:Repeater>
Code behind:
protected void Page_Load2(object sender, RepeaterItemEventArgs e)
{
if (!IsPostBack)
{
string id = Request.QueryString["id"].ToString();
DataSet dsselectcategory = BLLTour.left3join(id.Trim().ToString());
var dlPricelist = (DropDownList)e.Item.FindControl("dlPricelist");
dlPricelist.DataSource = dsselectcategory.Tables[0];
dlPricelist.DataTextField = "pricelistPrice";
dlPricelist.DataValueField = "priceid";
dlPricelist.DataBind();
}
}
protected void list_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "addtocard")
{
foreach (RepeaterItem dataItem in Repeater1.Items)
{
Label xxxxxx = (Label)e.Item.FindControl("xxxxxx");
LinkButton btnAddToCart = (LinkButton)e.Item.FindControl("btnAddToCart");
xxxxxx.Text = ((DropDownList)dataItem.FindControl("dlPricelist")).SelectedItem.Text; //No error
}
}
}
I don't know how I should fix it.
You are using very old technology to show data that's not appropriate.
But if you are serious to use it, you must use FindControll method in ItemTemplate of your repeater control. You should find dropdownlist first, and then cast it to a object to be able to use it's value.

How to bind repeater in loop in c#

I have used repeater in asp.net
<div class="slider-inner">
<div id="daslider" runat="server" class="da-slider">
<asp:Repeater ID="rptSlider" runat="server">
<ItemTemplate>
<asp:Panel ID="sld" runat="server" class="da-slide">
<h2><asp:Literal ID="lblTitle" runat="server"></asp:Literal></h2>
<p>
<asp:Literal ID="lblDescription" runat="server"></asp:Literal>
</p>
<div class="da-img">
<iframe id="framevid" runat="server" visible="false" width="530" height="300" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
<asp:Image ID="sldrimg" runat="server" CssClass="img-responsive"/>
</div>
</asp:Panel>
</ItemTemplate>
<FooterTemplate>
<asp:Panel ID="btnlinks" runat="server" class="da-arrows">
<span class="da-arrows-prev"></span>
<span class="da-arrows-next"></span>
</asp:Panel>
</FooterTemplate>
</asp:Repeater>
</div>
</div>
In CS File I want to bind them programically using loop as below lines of code
private void GetMainAppSettings()
{
MainSetting Item = context.FetchMainAppSettings();
SliderContext contextSlider = new SliderContext();
Slider SW = new Slider();
string PageName = "Home Page";
IEnumerable<_14Muslims.Domain.Entity.Slider> pType = contextSlider.SliderFetchAllEnabled(PageName);
foreach (Slider item in pType)
{
lblTitle.Text = item.SliderTitle;
lblDescription.Text = item.SliderDescription;
framevid.Attributes.Add("src", item.SliderImage);
sldr.Attributes.Add("src", item.SliderImage);
daslider.Style.Add("background-image", WebUtility.UrlSchemeAuthority() + #"/FileStore/AppSettingsSiteLogos/" + item.BackgroundImage);
}
}
Note that GetMainAppSettings() is called on page_load event
Please Help me !!!
There are two separate things that you need to do:
Set the source of the repeater
Tell the repeater what to do for each item in the source.
To achieve the first, you just need to set the DataSource property of the repeater to the collection of items you need displayed, and execute a DataBind call:
private void GetMainAppSettings()
{
MainSetting Item = context.FetchMainAppSettings();
SliderContext contextSlider = new SliderContext();
Slider SW = new Slider();
string PageName = "Home Page";
IEnumerable<_14Muslims.Domain.Entity.Slider> pType = contextSlider.SliderFetchAllEnabled(PageName);
rptSlider.DataSource(pType);
rptSlider.DataBind();
}
When this is done, the repeater will loop through each item, process it and, display whatever is needed. To customize this process, the repeated provides an ItemDataBound event where you can set how the template should look for a specific item:
protected void rptSlider_ItemDataBound(object sender, RepeaterItemEventArgs e) {
// This event is raised for the header, the footer, separators, and items.
// Execute the following logic for Items and Alternating Items.
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
//get the item from the event arguments
var item = (Slider)e.Item.DataItem;
//get the controls
var lblTitle = (Label)e.Item.FindControl("lblTitle");
var lblDescription= (Label)e.Item.FindControl("lblDescription");
var framevid= (HtmlGenericControl)e.Item.FindControl("framevid");
var sldr= (HtmlGenericControl)e.Item.FindControl("sldr");
//set the values
lblTitle.Text = item.SliderTitle;
lblDescription.Text = item.SliderDescription;
framevid.Attributes.Add("src", item.SliderImage);
sldr.Attributes.Add("src", item.SliderImage);
}
}
This will execute once for each item in the data source, and you have complete control over what goes where and how. The looping is done implicitly for you by the repeater itself.
No need to loop the data in your code behind, you can directly assign the DataSource and Repeater control will take care of rest.
In Code behind, you can programatically set the DataSource like this:-
rptSlider.DataSource = pType;
rptSlider.DataBind();
In your repeater control, you can put the Data Binder code nuggets to assign particular properties to control like this:-
<h2><asp:Literal ID="lblTitle" runat="server" Text='<%# SliderTitle%>'></asp:Literal></h2>
and so on..for other controls.
Why use loop for bind repeater? You can directly assign your object "pType" to repeater data source. Like
IEnumerable<_14Muslims.Domain.Entity.Slider> pType = contextSlider.SliderFetchAllEnabled(PageName);
rptSlider.DataSource=pType;
rptSlider.DataBind();
After you can access all your field in repeater on .aspx page.
More Details see below article:
http://www.c-sharpcorner.com/UploadFile/5089e0/how-to-use-repeater-control-in-Asp-Net/

ajax CollapsiblePanelExtender inside datalist

I am creating a data list that will have multiple person records displayed inside a collapsible panel and iside each dataItem user can edit the record etc.
Following is my code
<%# Register TagName="CreateEditPerson" Src="~/ascx/create_edit_person.ascx" TagPrefix="WD" %>
<asp:DataList ID="dlPerson" runat="server" OnItemDataBound="dlPerson_OnItemDataBound">
<ItemTemplate>
<div class="personRow">
<div class="personRowHeader" id="divEditPerson">
<div class="lastNameColumn">
<asp:Label ID="lblLastName" runat="server"></asp:Label </div>
<div class="firstNameColumn">
<asp:Label ID="lblFirstName" runat="server"></asp:Label>
</div>
<div class="editColumn">
<asp:Image ID="imgExpandImage" runat="server" Class="divSearchLabel" meta:resourcekey="imgExpandImage" />
<asp:Label ID="lblEditPerson" runat="server" meta:resourcekey="lblEditPerson"></asp:Label>
</div>
<div style="clear: both">
</div>
</div>
<ajaxToolkit:CollapsiblePanelExtender ID="cpPerson" runat="server" meta:resourcekey="cpPerson"
Collapsed="true" TargetControlID="pnlpersonEdit" CollapseControlID="divEditPerson"
ExpandControlID="divEditPerson" TextLabelID="lblEditPerson" CollapsedSize="0"
ScrollContents="false" SuppressPostBack="True" ImageControlID="imgExpandImage">
</ajaxToolkit:CollapsiblePanelExtender>
<asp:Panel ID="pnlpersonEdit" runat="server">
<div class="product_technical_details">
<WD:CreateEditPerson ID="createEditPerson1" runat="server" />
</div>
</asp:Panel>
</div>
</ItemTemplate>
</asp:DataList>
My Problem is,
1. When I click on 1st dataitem panel, All the panels expands. Do i need the dynamic id of the TargetControlID ?
I solved it as follows:
on ItemDataBound event of the datalist, I just set dynamic ID of the collapsible panel, target panel and collaped/expand control; and set the "TargetControlID", "CollapseControlId" and "ExpandControlId"
I am giving the code snippet here:
protected void dlPerson_OnItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//Find the controls in the Item Template
Panel pnlPersonEdit = e.Item.FindControlRecursive("pnlPersonEdit") as Panel;
CollapsiblePanelExtender cpPersonEdit =
e.Item.FindControlRecursive("cpPersonEdit") as CollapsiblePanelExtender;
//This panel was a div previously "divEditPerson" I have changed it to panel
Panel pnlEditPersonHead = e.Item.FindControlRecursive("pnlEditPersonHead") as Panel;
//Some object from db
PersonObject personObject = e.Item.DataItem as PersonObject;
//Set Id of the collapsed/expanded panel from unique key
pnlEditPersonHead.ID += personObject.CrmddressId.ToString();
//Set Id of the target panel from unique key
pnlPersonEdit.ID += personObject.CrmddressId.ToString();
//simillarly set Id of the collapsible panel extender
cpPersonEdit.ID += personObject.CrmddressId.ToString();
//Now set the TargetControlID, CollapseControlID and ExpandControlID
cpPersonEdit.TargetControlID = pnlPersonEdit.ID;
cpPersonEdit.CollapseControlID = pnlEditPersonHead.ID;
cpPersonEdit.ExpandControlID = pnlEditPersonHead.ID;
}
}

Change Css class on a Panel thats located inside a Asp:repeater

Hello and thanks for taking your time to read this.
I'm trying to change the CSS class of a panel thats located inside a Repeater when I select a RadioButton.
<div>
<asp:RadioButtonList OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged" AutoPostBack="true" ID="RadioButtonList1" RepeatDirection="Horizontal" runat="server">
<asp:ListItem Selected="True">Show Gallery</asp:ListItem>
<asp:ListItem>Show List</asp:ListItem>
</asp:RadioButtonList>
</div>
<div class="RpOutterFrame" runat="server" id="RpOutterFrame">
<asp:Repeater runat="server" ID="RP">
<ItemTemplate>
<panel class="ShowDiv" runat="server" id="RpInnerFrame">
<img runat="server" style="width: 80px;" id="ModelImg" class="ModelImg" src='<%# string.Format("~/Content/Img/ModelImg/{0}", Eval("Image")) %>' />
<br />
<%# Eval("Model") %>
</panel>
</ItemTemplate>
</asp:Repeater>
</div>
My C#:
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (RadioButtonList1.Items[0].Selected == true)
{
RpOutterFrame.Attributes["class"] = "RpOutterFrame";
Panel panel = (Panel)this.FindControl("RpInnerFrame");
panel.CssClass = "ShowDiv2";
}
}
As you can see the Panel already has the class ShowDiv and then I would like it to change the class to ShowDiv2 when I select/click the Radiobutton.
Anyone who can help me figuar what I'm doing wrong or fix the code?
A Repeater's purpose is to repeat something. So normally it contains multiple elements. Therefore the RepeaterItem is the NamingContainer which must contain unqiue ID's and where you can find your controls via FindControl(ID).
So this does not work since this is the Page which is not the NamingContainer of the Panel:
Panel panel = (Panel)this.FindControl("RpInnerFrame");
panel.CssClass = "ShowDiv2";
You have to loop all items:
foreach(RepeaterItem item in RP.Items)
{
Panel panel = (Panel)item.FindControl("RpInnerFrame");
panel.CssClass = "ShowDiv2";
}
Apart from that you should use the ASP:Panel instead of Panel.
So change
<panel class="ShowDiv" runat="server" id="RpInnerFrame">
// ...
</panel>
to
<ASP:Panel CssClass="ShowDiv" runat="server" id="RpInnerFrame">
// ...
</ASP:Panel>

need to change class in repeater elements

i have a repeater:
<ul>
<asp:Repeater ID="Repeater4" runat="server" DataSourceID="SqlDataSource5" onitemcommand="Repeater4_ItemCommand">
<ItemTemplate>
<li class="sports_menu1">
<asp:LinkButton ID="LinkButton1" runat="server" class="selected onclick="sideMenuSports1_Click">
<%#Eval("iconURL")%>
</asp:LinkButton></li>
</ItemTemplate>
</asp:Repeater>
</ul>
and i want to handle all the buttons in the same function:
protected void sideMenuSports1_Click(object sender, EventArgs e)
{
changeDataSources(); /*-----> this function supose to derict to URL(in a div on the same page) acording the id of the item of the repeater */
removeAllClasses(); /*this supose to remove all class named selected from the element in the <asp:button> element */
((LinkButton)sender).Attributes.Add("class", "selected");
}
to sumerise, what i need is:
i need to remove the class "selected" in all the repeater <asp:button> elements, then add it only to the element the user clicked
each button clicked redirect to other URL ,how i pass the button the id of the repeater element?
Had the same problem, solved it like this:
.aspx part:
<asp:Repeater ID="itemsRepeater" runat="server" OnItemCommand="itemsRepeater_ItemCommand">
<ItemTemplate>
<li>
<asp:LinkButton id="location_button" CommandArgument='<%# Eval("Key") %>' Text='<%# Eval("Value") %>' runat="server"></asp:LinkButton>
</li>
</ItemTemplate>
</asp:Repeater>
code-behind part:
protected void itemsRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
// for the sample purpose, setting the value here - replace it with your own
int locationId = 2;
// looping through all the repeater items (buttons)
foreach (RepeaterItem i in ((Repeater)source).Items)
{
// finding the repeater items having ID set to "location_button"
LinkButton btn = (LinkButton)i.FindControl("location_button");
// custom class added when location matches button's CommandArgument
if (btn.CommandArgument == locationId.ToString())
btn.CssClass = "button_main active";
else
btn.CssClass = "button_main";
}
}

Categories

Resources