【发布时间】:2016-02-11 18:43:12
【问题描述】:
我在中继器的帮助下创建了动态单选按钮/复选框控件。但是回发后控件会丢失。 例如,如果我检查我使用中继器创建的单选按钮,所有单选按钮都会消失
这是我的 aspx 代码
<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>
我的 .cs 代码
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)
{
}
我已经为绘图面板和动态创建的每个控件设置了 enableViewState=true。但它不起作用。请帮帮我...
【问题讨论】:
-
这个链接可以帮到你这个链接可以帮到你codeproject.com/Articles/502251/…
-
@SruthiSuresh:我试过了。但对我不起作用