简体   繁体   English

动态转发器控制回发后迷路

[英]Dynamic repeater controls getting lost after post back

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 这是我的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>

my .cs code 我的.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)
    {

    }

i have set enableViewState=true for both the drawing panel and each control which is creating dynamically. 我为绘图面板和每个动态创建的控件都设置了enableViewState = true。 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. 所以ItemDataBound是不合适的,因为它仅在转发器获取数据绑定时才触发。 Use ItemCreated instead. 请改用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);
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM