Dynamic repeater controls getting lost after post back - c#

I have created dynamic radio button/check box controls with help of a repeater. But the controls are getting lost after postback.
as an example, if i check the radion button which is i create using the repeater all the radio buttons will disappear
here is my aspx code
<asp:Repeater ID="myRepeater" runat="server" OnItemCommand="myRepeater_ItemCommand" OnItemDataBound="myRepeater_ItemDataBound">
<HeaderTemplate>
<table>
<tr class="">
<td>
</td>
<td>
Name
</td>
<td>
Address
</td>
<td>
Age
</td>
<td>
Year
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Panel ID="pnlSelect" runat="server" EnableViewState="true"></asp:Panel>
</td>
<td>
<%#Eval("Name")%>
</td>
<td>
<%#Eval("Address")%>
</td>
<td>
<%#Eval("Age")%>
</td>
<td>
<%# Eval("Year")%>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
my .cs code
myRepeater.DataSource = *DataSource*;
myRepeater.DataBind();
protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
if (** condition 01 **)
{
if (** condition 02 **)
{
RadioButton rdoBtn = new RadioButton();
rdoBtn.ID = "rbtnID";
rdoBtn.EnableViewState = true;
rdoBtn.GroupName = "GroupName";
rdoBtn.AutoPostBack = true;
rdoBtn.CheckedChanged += new System.EventHandler(this.rdoBtnChecked_Changed);
string script = "SetUniqueRadioButton('myRepeater.*GroupName',this)";
rdoBtn.Attributes.Add("onclick", script);
Panel pnlRbtnSet = e.Item.FindControl("pnlSelect") as Panel;
pnlRbtnSet.Controls.Add(rdoBtn);
}
else
{
CheckBox chkBox = new CheckBox();
chkBox.ID = "chkBxID";
chkBox.Checked = true;
chkBox.EnableViewState = true;
Panel pnlChkBoxesSet = e.Item.FindControl("pnlSelect") as Panel;
pnlChkBoxesSet.Controls.Add(chkBox);
}
}
}
}
protected void rdoBtnChecked_Changed(Object sender, EventArgs e)
{
}
i have set enableViewState=true for both the drawing panel and each control which is creating dynamically. but it does not work. please help me...

If you need to create dynamic controls you have to recreate them on every postback. So ItemDataBound is inappropriate since it's only triggered when the repeater is getting databound. Use ItemCreated instead.
protected void myRepeater_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
if (** condition 01 **)
{
if (** condition 02 **)
{
RadioButton rdoBtn = new RadioButton();
rdoBtn.ID = "rbtnID";
rdoBtn.EnableViewState = true;
rdoBtn.GroupName = "GroupName";
rdoBtn.AutoPostBack = true;
rdoBtn.CheckedChanged += new System.EventHandler(this.rdoBtnChecked_Changed);
string script = "SetUniqueRadioButton('myRepeater.*GroupName',this)";
rdoBtn.Attributes.Add("onclick", script);
Panel pnlRbtnSet = e.Item.FindControl("pnlSelect") as Panel;
pnlRbtnSet.Controls.Add(rdoBtn);
}
else
{
CheckBox chkBox = new CheckBox();
chkBox.ID = "chkBxID";
chkBox.Checked = true;
chkBox.EnableViewState = true;
Panel pnlChkBoxesSet = e.Item.FindControl("pnlSelect") as Panel;
pnlChkBoxesSet.Controls.Add(chkBox);
}
}
}
}

Related

How to set different visibility properties for repeated items

I Have a user custom control with a repeater that has a calendar and a button that should toggle visibility for each repeated calendar.
<asp:Repeater ID="DateRepeater" runat="server"
DataSourceID="DateSource" OnItemCommand="DateRepeater_ItemCommand" OnPreRender="DateRepeater_PreRender">
<ItemTemplate>
<tr>
<td class="auto-style8">
<asp:ImageButton ID="CalendarButtonRef" runat="server" ImageUrl="~/img/calendar.png" OnClick="ShowCalendar_Click" />
<asp:Calendar ID = "RefCalendar" Width="176" runat="server" OnSelectionChanged="Calendar_SelChanged"></asp:Calendar>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
In the codebehind I am trying to target the clicked image button on the same repeated item
protected void ShowCalendar_Click(object sender, ImageClickEventArgs e)
{
ImageButton btnSender = (ImageButton)sender;
String TragetCalendarID = btnSender.UniqueID.Remove(19) + "RefCalendar";
if (Page.IsPostBack)
{
Calendar TargetCalendar;
foreach (RepeaterItem rpt in DateRepeater.Items)
{
if (rpt.FindControl("RefCalendar").UniqueID == TragetCalendarID)
{
Trace.Write(rpt.FindControl("RefCalendar").UniqueID);
Trace.Write(rpt.FindControl("RefCalendar").GetType().ToString());
TargetCalendar = (Calendar)rpt.FindControl(TragetCalendarID);
if (TargetCalendar.Visible == false)
{
TargetCalendar.Visible = true;
}
}
else
{
TargetCalendar.Visible = false;
}
}
}
I manage to cast the target Calendar and in the trace I get the object as a calendar but when I try to get the visibility property it says that the object reference is not set to an instance of an object.
What is missing in my code?
This is how the function then works, if referring to the object by its ID with FindControl it works. I was trying to cast the object to a calendar and use it as as a variable but it doesn't work like that.
This is the working code
protected void ShowCalendar_Click(object sender, ImageClickEventArgs e)
{
ImageButton btnSender = (ImageButton)sender;
String TragetCalendarID = btnSender.UniqueID.Remove(19) + "RefCalendar";
if (Page.IsPostBack)
{
foreach (RepeaterItem rpt in DateRepeater.Items)
{
if (rpt.FindControl("RefCalendar").UniqueID == TragetCalendarID)
{
if (rpt.FindControl("RefCalendar").Visible == false)
{
rpt.FindControl("RefCalendar").Visible = true;
}
else
{
rpt.FindControl("RefCalendar").Visible = false;
}
}
}
}
}

append tr when a value is not null using item template

I am appending rows using item template
<tr>
<td>
<asp:HyperLink ID="HyperLink1" NavigateUrl="bla" CssClass="display:inherit !important;" Text='<%# someFunction(School.Name,"Name") %>' runat="server" />
</td>
</tr>
the problem with the above one is if someFunction(School.Name,"Name") returns null it appends empty tr, so i want to append tr only if someFunction(School.Name,"Name")!=null
So i tried the below code by adding a if condition but it is not compiling !!
It says school.name is not defined
<% if(someFunction(School.Name,"Name")!=null){
<tr>
<td>
<asp:HyperLink ID="HyperLink1" NavigateUrl="bla" CssClass="display:inherit !important;" Text='<%# someFunction(School.Name,"Name") %>' runat="server" />
</td>
</tr>
} %>
You could take care of it from the code behind.
<td id="tdAppend" runat="server"> Content </td>
if(School.Name == null)
tdAppend.Visible = false;
UPDATED ANSWER:
currentItemIndex = -1;
listView.ItemDataBound += listView_ItemDataBound;
CheckQueryString();
//Hide or show the table header based on id
HtmlControl thControl = listView.FindControl("thAppend") as HtmlControl;
if (thControl != null)
{
if (obj.ID == 12)
{
thControl.Visible = true;
}
else
{
thControl.Visible = false;
for (int i = 0; i < listView.Items.Count; i++)
{
HtmlControl tdControl = listView.Items[i].FindControl("tdSchoolName") as HtmlControl;
if (tdControl != null)
{
tdControl.Visible = false;
}
}
}
}
For convenience:
void listView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
currentItemIndex++;
ListViewDataItem dataitem = (ListViewDataItem)e.Item;
HtmlTableRow trControl = (HtmlTableRow)e.Item.FindControl("MainTableRow");
if (IsOdd(currentItemIndex))
trControl.BgColor = "DarkGray";
}
}
private bool IsOdd(int value)
{
return value % 2 != 0;
}
It's a pretty straight forward practice.. Handling the ListView.OnItemDataBinding event.
Markup
<%# Import Namespace="MyNamespace" %>
<asp:ListView ID="lview" runat="server" OnItemDataBound="lview_ItemDataBound" ItemPlaceholderID="Placeholder1">
<ItemTemplate>
<tr>
<td><%# ((School)Container.DataItem).Name %></td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table>
<div id="Placeholder1" runat="server"></div>
</table>
</LayoutTemplate>
</asp:ListView>
Code-Behind
namespace MyNamespace
{
public class Default : Page
{
IEnumerable<School> dataItems {get;set;}
protected void Page_Load(object sender, EventArgs e)
{
dataItems = new School[] { new School() { Name = "School 1" }, new School() { Name = "School 2" }, new School() { Name = null }, new School() { Name = "School 3" } }.AsEnumerable();
lview.DataSource = dataItems;
lview.DataBind();
}
protected void lview_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ListView listview = (ListView)sender;
ListViewItem row = e.Item;
School dataItem = (School)e.Item.DataItem;
if (dataItem.Name == null)
{
row.Visible = false;
}
}
}
public class School
{
public string Name { get; set; }
}
}
personally, I don't usually use tables.. it's a fairly odd-looking layout template.

How to find dropdown selected value inside repeater in dropdown selected index change event?

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<tr>
<td><%#subtypes.FindByPk(Convert.ToInt32(Eval("SubitemID"))).title%></td>
<td><%#Eval("quantity")%></td>
<td><%#ThanaRecord.FindByPk(Convert.ToInt32(Eval("Thanaid"))).title%></td>
<td><%#Eval("created_at")%></td>
<td> <% if (Employee.GetCurrentEmployee().role == "Admin") { %>
EDIT
DELETE
<% } %>
<%if (Employee.GetCurrentEmployee().role == "SuperVisor")
{ %>
<asp:DropDownList ID="DropDownList1" runat="server" Width="120px" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" CssClass="form-control">
<asp:ListItem Text="Status" Value="0">Status</asp:ListItem>
<asp:ListItem Text="Accept" Value="1">Accept</asp:ListItem>
<asp:ListItem Text="Reject" Value="2">Reject</asp:ListItem>
</asp:DropDownList>
<%--<asp:textbox runat="server" id="textTest"></asp:textbox>--%>
</td>
<%} %>
</tr>
</ItemTemplate>
</asp:Repeater>
Code:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//DropDownList DropDownList1 = (DropDownList)sender;
//string SelectedValue = DropDownList1.SelectedValue;
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DropDownList ddldrop = (DropDownList)e.Item.FindControl("DropDownList1");
int value =Convert.ToInt32( ddldrop.SelectedValue);
Supervisor sup = new Supervisor();
if (value ==1 ) {
sup.Status = "Accept";
sup.Save();
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//Supervisor sup = new Supervisor();
//sup.Status = "Accept";
//sup.Save();
}
You just have to cast the sender argument in the event-handler to DropDownList. It is always the source of the event. So no need to use FindControl:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList) sender;
string selectedValue = ddl.SelectedValue;
// ...
}

DropdownList SelectedIndexChanged inside Repeater fires up while under UpdatePanel and click another Dropdownlist outside repeater

I have a 2 Dropdownlist outside a repeater (1st ddl will trigger and refresh the content of the repeater, 2nd ddl refresh its list after an item inside the repeater were executed) and a dropdownlist control inside the repeater.i don't have a problem with the 1st dropdownlist but the 2nd dropdownlist when triggered via selectedindexchanged, it also fires up the selectedindexchanged of the dropdownlist inside the repeater. I put up an UpdatePanel on the repeater and on the 2nd dropdownlist but still the error occurs.
How can i trace the step by step execution and also why the selectedindexchanged handler fires up?
Here is the code snippet:
CODE BEHIND:
public partial class NewStyleItem : System.Web.UI.Page
{
protected StyleTemplateManager styleTempMngr = new StyleTemplateManager();
protected StyleFabricationManager styleFabMngr = new StyleFabricationManager();
protected List<ComponentReferenceView> ReferenceLookUpList
{
get
{
return (List<ComponentReferenceView>)ViewState["ReferenceLookUpView"];
}
set
{
ViewState["ReferenceLookUpView"] = value;
}
}
protected List<ComponentReferenceView> ReferenceViewList
{
get
{
return (List<ComponentReferenceView>)ViewState["CompReferenceView"];
}
set
{
ViewState["CompReferenceView"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack == false)
{
LoadBrandList();
ddlGarmentList.Enabled = false;
//ReferenceLookUpList = new List<ComponentReferenceView>();
}
else
{
}
//rptStyleComponents.DataSource = ReferenceLookUpList;
//rptStyleComponents.DataBind();
}
protected void LoadBrandList()
{
BrandManager brandMngr = new BrandManager();
ddlBrandList.Items.Clear();
ddlBrandList.Items.Add(new ListItem { Text = "Select brand", Value = "" });
foreach (Brand item in brandMngr.Brands())
{
ddlBrandList.Items.Add(new ListItem
{
Text = item.BrandDescription,
Value = item.BrandCode
});
}
}
protected void LoadComponentInfo(string BrandCode)
{
if (styleTempMngr.FindTemplateByBrandCode(BrandCode) == true)
{
ReferenceLookUpList = (from StyleComponent i in styleFabMngr.ComponentList
join StyleFabReference x in styleTempMngr.ReferenceList on
i.SectionCode equals x.SectionCode
select new ComponentReferenceView
{
InOrder = x.SortOrder,
IsNeeded = x.IsRequired,
SCode = x.SeriesCode,
SecCode = x.SectionCode,
SecName = i.SectionName,
EntryValue = x.DataCharacter
}).OrderBy(p => p.InOrder).ToList();
ReferenceLookUpList.Where(row => row.SecCode == "BCODE").ToList()
.ForEach(refItem => refItem.EntryValue = ddlBrandList.SelectedValue);
var DistinctList = (from StyleFabReference x in styleTempMngr.ReferenceList.OrderBy(p => p.SortOrder).ToList()
where x.SectionCode != "SPCHAR" && x.SectionCode != "BCODE"
select x.SectionCode).Distinct().ToList();
ReferenceViewList = (from StyleComponent i in styleFabMngr.ComponentList
join x in DistinctList on
i.SectionCode equals x
select new ComponentReferenceView
{
SecCode = x,
SecName = i.SectionName
}).OrderBy(p => p.InOrder).ToList();
}
else
ReferenceViewList = new List<ComponentReferenceView>();
rptStyleComponents.DataSource = ReferenceViewList;
rptStyleComponents.DataBind();
}
protected void LoadGarments(string ApparelCode)
{
if (string.IsNullOrEmpty(ddlBrandList.SelectedValue) == true)
{
ddlGarmentList.SelectedIndex = 0;
ddlGarmentList.Enabled = false;
}
else
{
ddlGarmentList.Enabled = true;
GarmentManager gmMngr = new GarmentManager();
List<Garment> garmentList = new List<Garment>();
garmentList = gmMngr.GetGarmentsByBrand(ddlBrandList.SelectedValue, ApparelCode);
ddlGarmentList.Items.Clear();
ddlGarmentList.Items.Add(new ListItem { Text = "Select garment", Value = "" });
foreach (Garment item in garmentList)
{
ddlGarmentList.Items.Add(new ListItem { Text = item.GarmentDescription, Value = item.GarmentCode });
}
}
}
protected void ddlBrandList_SelectedIndexChanged(object sender, EventArgs e)
{
txtNewStyleNo.Text = string.Empty;
if (string.IsNullOrEmpty(ddlBrandList.SelectedValue) == false)
LoadComponentInfo(ddlBrandList.SelectedValue);
}
[Serializable]
public class ComponentReferenceView
{
public string SCode
{
get;
set;
}
public string SecCode
{
get;
set;
}
public string SecName
{
get;
set;
}
public int InOrder
{
get;
set;
}
public bool IsNeeded
{
get;
set;
}
public string EntryValue
{
get;
set;
}
}
protected void rptStyleComponents_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
}
protected void rptStyleComponents_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DropDownList ddlControl = (DropDownList)e.Item.FindControl("ddlDataSelection");
if (ddlControl != null)
{
ddlControl.AppendDataBoundItems = true;
ddlControl.Items.Add(new ListItem { Text = "Select " + ReferenceViewList[e.Item.ItemIndex].SecName.ToLowerInvariant(), Value = "" });
var itemlist = (from x in styleFabMngr.ComponentRowItems(ReferenceViewList[e.Item.ItemIndex].SecCode).AsEnumerable()
select new ListItem
{
Text = x.Field<string>("col_name"),
Value = x.Field<string>("col_value")
});
ddlControl.Items.AddRange(itemlist.ToArray<ListItem>());
ddlControl.EnableViewState = false;
ddlControl.AutoPostBack = true;
ddlControl.SelectedIndexChanged += new EventHandler(DropDownListSelectedIndexChanged);
}
}
}
protected virtual void DropDownListSelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
RepeaterItem item = (RepeaterItem)ddl.NamingContainer;
ReferenceViewList[item.ItemIndex].EntryValue = ddl.SelectedValue;
ConstructStyleNo();
var Apparelfound = ReferenceViewList.Where(x => x.SecCode == "APCODE" &&
string.IsNullOrEmpty(x.EntryValue) == false).FirstOrDefault();
if (Apparelfound != null)
{
ddlGarmentList.Enabled = true;
LoadGarments(Apparelfound.EntryValue);
}
else
{
ddlGarmentList.Enabled = false;
}
}
private void ConstructStyleNo()
{
foreach (ComponentReferenceView item in ReferenceViewList)
{
ReferenceLookUpList.Where(row => row.SecCode == item.SecCode).ToList()
.ForEach(refItem => refItem.EntryValue = item.EntryValue);
}
string sResult = string.Empty;
var result = (from x in ReferenceLookUpList.OrderBy(y => y.InOrder)
select x.EntryValue);
txtNewStyleNo.Text = string.Empty;
if (result != null)
txtNewStyleNo.Text = String.Join("", result) + "0001";
}
private void ConstructStyleDescription()
{
txtStyleDescription.Text = ddlBrandList.SelectedItem.Text + "-" + ddlGarmentList.SelectedItem.Text;
}
protected void ddlGarmentList_SelectedIndexChanged(object sender, EventArgs e)
{
txtStyleDescription.Text = string.Empty;
if (string.IsNullOrEmpty(ddlGarmentList.SelectedValue) == false)
ConstructStyleDescription();
}
}
ASPX:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:UpdatePanel ID="upnlNewFabrication" runat="server">
<ContentTemplate>
<h1>
New Style Item
</h1>
<br />
<%--<usrCtrl:style_generation ID="stylegen_frm" runat="server">
</usrCtrl:style_generation>--%>
<table>
<tr>
<td>
Brand :
</td>
<td>
<asp:DropDownList ID="ddlBrandList" runat="server"
onselectedindexchanged="ddlBrandList_SelectedIndexChanged"
AutoPostBack="True">
</asp:DropDownList>
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td>
Generated Style# :
</td>
<td>
<asp:TextBox ID="txtNewStyleNo" runat="server" ReadOnly="true"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Style Description :
</td>
<td>
<asp:TextBox ID="txtStyleDescription" runat="server" ReadOnly="true"></asp:TextBox>
</td>
</tr>
<asp:Repeater ID="rptStyleComponents" runat="server"
onitemdatabound="rptStyleComponents_ItemDataBound"
onitemcreated="rptStyleComponents_ItemCreated"
EnableViewState="True">
<ItemTemplate>
<asp:UpdatePanel ID="upnlComponent" runat="server">
<ContentTemplate>
<tr>
<td>
<asp:Label ID="lblSectionName" runat="server" Text='<%# Eval("SecName")%>' ></asp:Label>
</td>
<td>
<asp:DropDownList ID="ddlDataSelection" runat="server">
</asp:DropDownList>
</td>
</tr>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="ddlDataSelection" />
</Triggers>
</asp:UpdatePanel>
</ItemTemplate>
</asp:Repeater>
</table>
<br />
<table>
<tr>
<td colspan="2">Description Items</td>
</tr>
<tr>
<td colspan="4"></td>
</tr>
<tr>
<td>Garment Type:</td>
<td>
<asp:UpdatePanel ID="upGarment" runat="server" >
<ContentTemplate>
<asp:DropDownList ID="ddlGarmentList" runat="server"
AppendDataBoundItems="True" AutoPostBack="True"
onselectedindexchanged="ddlGarmentList_SelectedIndexChanged">
<asp:ListItem Text = "Select garment" Value = ""></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="sdsGarment" runat="server"
ConnectionString="<%$ ConnectionStrings:IRMSConnectionString %>"
SelectCommand="Select * from Garments"></asp:SqlDataSource>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="ddlGarmentList" />
</Triggers>
</asp:UpdatePanel>
</td>
</tr>
<tr>
<td>Cost Price:</td>
<td>
<asp:TextBox ID="txtCostPrice" runat="server"></asp:TextBox></td>
<td>SRP :</td>
<td><asp:TextBox ID="txtSRP" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Quantity :</td>
<td><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
<td>Week :</td>
<td><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>

Can't find my textbox inside a repeater

I am making a menu for a cafe. It shows the food and rinks items, the price and then I want a textbox, where people can write how much of each they want to buy, shown in a textbox. Then I want each textbox to have a unique ID based on the menu items ID.
This is my repeater:
<asp:Repeater ID="ParentRepeater" runat="server" OnItemDataBound="ParentRepeater_ItemDataBound">
<ItemTemplate>
<h2>
<%#DataBinder.Eval(Container.DataItem, "typenavn") %></h2>
<asp:HiddenField ID="HiddenField1" Value='<%# Eval("id") %>' runat="server" />
<asp:Repeater ID="ChildRepeater" runat="server">
<ItemTemplate>
<table>
<tr>
<td style="width: 400px">
<%#DataBinder.Eval(Container.DataItem, "productName") %>
</td>
<td style="width: 400px">
<%#DataBinder.Eval(Container.DataItem, "pris") %>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
And this is my code behind:
protected void ParentRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
RepeaterItem item = e.Item;
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
Repeater ChildRepeater = (Repeater)item.FindControl("ChildRepeater");
HiddenField hide = e.Item.FindControl("HiddenField1") as HiddenField;
int id = Convert.ToInt32(hide.Value);
var query = from es in gr.products
where es.typeID == id
select es;
List<product> list = new List<product>();
foreach (product pro in query)
{
list.Add(pro);
}
ChildRepeater.DataSource = list;
ChildRepeater.DataBind();
int h = 0;
foreach (RepeaterItem item1 in ChildRepeater.Items)
{
if (item1.ItemType == ListItemType.Item || item1.ItemType == ListItemType.AlternatingItem)
{
TextBox txt = (TextBox)item1.FindControl("TextBox1") as TextBox;
HiddenField hf = (HiddenField)item1.FindControl("HiddenField2") as HiddenField;
for (int i = 0; i < list.Count; i++)
{
txt.ID = "HB" + list[h].id.ToString();
hf.Value = list[h].id.ToString();
h++;
break;
}
}
}
}
}
Anybody got any ideas about how to find the textbox??
You have to search for the TextBox in the RepeaterItem. So you either handle the inner Repeater's ItemDataBound event or you simply iterate all RepeaterItems:
foreach(RepeaterItem item in ChildRepeater.Items){
if(item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem){
var txt = (TextBox)item.FindControl("TextBox1");
}
}

Categories

Resources