动态转发器控制在回发后丢失

时间:2016-02-11 10:27:35

标签: c# asp.net

我在转发器的帮助下创建了动态单选按钮/复选框控件。但是在回发后控件正在迷失。 例如,如果我检查我使用转发器创建的radion按钮,所有单选按钮都将消失

这是我的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")%>&nbsp;
                    </td>
                    <td>
                        <%# Eval("Year")%>&nbsp;
                    </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。但它不起作用。请帮帮我......

1 个答案:

答案 0 :(得分:1)

如果需要创建动态控件,则必须在每次回发时重新创建它们。所以ItemDataBound是不合适的,因为它仅在转发器获得数据绑定时触发。请改用ItemCreated

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);
            }
        }
    }
}