I stuck in a situation and need some help. I've adde a WebUserControl inside Repeater control. My WebUserControl consists of basically three controls: a CheckBox, a HyperLink, and some Label control. Plz have a look:
I've some Buttons outside the Repeater control. Now I want to select individual row through CheckBox and process it on Button (outside Repeater) Click.
for (int index = 0; index < this.Repeater1.Items.Count; index++)
{
CheckBox cbx = (this.Repeater1.Items[index].FindControl("animation1").FindControl("CheckBoxid") as CheckBox);
Label label = (this.Repeater1.Items[index].FindControl("animation1").FindControl("Labelid") as Label);
}
You can loop through the Repeater items, extract the UserControl from there using FindControl method for you to get reference to the controls within the usercontrols.. I haven't tested on this since I'm not in my development environment but I think you
can do something like below at Button Click event:
foreach (RepeaterItem item in Repeater1.Items){
if (item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.Item){
UserControl uc = (UserControl)item.FindControl("animation1");
if (uc != null){
//Access the checkbox that resides within the UserControl
CheckBox cb = (CheckBox) uc.FindControl("CheckBox1");
if(cb.Checked){
//Do something
}
}
}
}
shatru
Participant
1513 Points
292 Posts
Accessing Repeater Item on Button Click
Jan 22, 2010 06:20 AM|LINK
Hi all,
I stuck in a situation and need some help. I've adde a WebUserControl inside Repeater control. My WebUserControl consists of basically three controls: a CheckBox, a HyperLink, and some Label control. Plz have a look:
<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound"> <HeaderTemplate> <table cellpadding="0" cellspacing="0" border="0" width="580"> <tr> <td width="55%" align="left"> <span class="report_heading">Name</span> </td> <td width="15%" align="left"> <span class="report_heading">No. of Views</span> </td> <td width="15%" align="left"> <span class="report_heading">Ranking</span> </td> <td width="15%" align="left"> <span class="report_heading">Relesed Date</span> </td> </tr> <tr> <td colspan="4" width="100%" style="background:url(images/skinny_dots.gif) repeat-x center;"> </td> </tr> </table> <table cellpadding="0" cellspacing="0" border="0" width="580" class="report"> </HeaderTemplate> <ItemTemplate> <tr style="background:#faf7f7;"> <td> <uc1:animation ID="animation1" runat="server" /> </td> </tr> </ItemTemplate> <AlternatingItemTemplate> <tr style="background:#fcefef;"> <td> <uc1:animation ID="animation2" runat="server" /> </td> </tr> </AlternatingItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater>I've some Buttons outside the Repeater control. Now I want to select individual row through CheckBox and process it on Button (outside Repeater) Click.
Plz help me.
Thanx in advance.
Regards
Ajatshatru
Dragons Lab
karthicks
All-Star
31376 Points
5421 Posts
Re: Accessing Repeater Item on Button Click
Jan 22, 2010 06:54 AM|LINK
hi,
refer below code
for (int index = 0; index < this.Repeater1.Items.Count; index++) { CheckBox cbx = (this.Repeater1.Items[index].FindControl("animation1").FindControl("CheckBoxid") as CheckBox); Label label = (this.Repeater1.Items[index].FindControl("animation1").FindControl("Labelid") as Label); }Karthick S
vijjendra
Participant
1753 Points
370 Posts
Re: Accessing Repeater Item on Button Click
Jan 22, 2010 06:55 AM|LINK
Hi,
Suppose you have the checkbox inside the repeater then you need to do somthing like:
<ItemTemplate>
<tr style="background:#faf7f7;">
<td ><input type="checkbox" runat="server" id="chkId" /></td>
<td>
<uc1:animation ID="animation1" runat="server" />
</td>
</tr>
</ItemTemplate>
protected void bntDelete_Command(object sender, CommandEventArgs e)
{
if (e.CommandName.Equals("Delete"))
{
int Id=0;
foreach (RepeaterItem ri in rptOrders.Items)
{
HtmlInputCheckBox chkId = (HtmlInputCheckBox)ri.FindControl("chkId");
Literal litID = (Literal)ri.FindControl("litID");
if (chkId != null)
{
if (chkId.Checked)
{
Id = Convert.ToInt32(litrID.Text);
///Do your stuff here
}
}
}
}
}
your button code somthing look like :
<asp:Button ID="btnDelete" runat="server" Text="Delete Item" OnCommand="bntDelete_Command" CommandName="Delete"/>
Vijendra Singh
vinz
All-Star
126946 Points
17922 Posts
MVP
Re: Accessing Repeater Item on Button Click
Jan 22, 2010 06:56 AM|LINK
You can loop through the Repeater items, extract the UserControl from there using FindControl method for you to get reference to the controls within the usercontrols.. I haven't tested on this since I'm not in my development environment but I think you can do something like below at Button Click event:
foreach (RepeaterItem item in Repeater1.Items){ if (item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.Item){ UserControl uc = (UserControl)item.FindControl("animation1"); if (uc != null){ //Access the checkbox that resides within the UserControl CheckBox cb = (CheckBox) uc.FindControl("CheckBox1"); if(cb.Checked){ //Do something } } } }MessageBox Controls for WebForms | Blog | Twitter | Linkedin