Reputation: 369
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...
Upvotes: 0
Views: 1238
Reputation: 460288
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);
}
}
}
}
Upvotes: 1