【问题标题】:Dynamic repeater controls getting lost after post back回发后动态中继器控件丢失
【发布时间】: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")%>&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。但它不起作用。请帮帮我...

【问题讨论】:

标签: c# asp.net


【解决方案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);
            }
        }
    }
}

【讨论】:

  • 你的意思是不使用 ItemDataBound ..?对于中继器,我有另一个要绑定的数据列表。连同该数据,我必须插入此复选框或单选按钮。我们可以同时使用 ItemDataBound 和 ItemCreated ..吗?我们可以只使用 ItemCreated 来创建动态控件吗?
  • 感谢您的帮助。它适用于 ItemDataBound 和 ItemCreated
  • 在没有回发值之后,我为控件设置了一个值。我使用 DataBinder.Eval(e.Item.DataItem, "UserID").ToString() 设置值。但回发后 DataItem 为空。很明显,因为 ItemDataBound 在回发后没有触发。有什么解决办法...?
猜你喜欢
  • 2013-07-09
  • 2013-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-14
  • 2013-08-31
  • 1970-01-01
相关资源
最近更新 更多