简体   繁体   English

回发后无法还原动态转发器控件的值

[英]Can not restore dynamic repeater controls values after a post back

i use ItemCreated to create my dynamic controls. 我使用ItemCreated创建我的动态控件。 but after a post back since ItemDataBound not triggering, i cant restore my controls values with e.Item.DataItem . 但是在回发之后,由于ItemDataBound没有触发,我无法使用e.Item.DataItem恢复控件值。 DataItem is null after the post back. 回发后, DataItem为null。 how can i restore my values..? 我如何恢复我的价值观..? my code is like below 我的代码如下

    protected void rptV_ItemCreated(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            if (** condition 01 **)
            {
                if (** condition 01 **)
                {
                    RadioButton rdoBtn = new RadioButton();
                    rdoBtn.ID = "rbtnID";
                    rdoBtn.EnableViewState = true;
                    rdoBtn.GroupName = "GroupName";
                    rdoBtn.AutoPostBack = true;
                    rdoBtn.Attributes.Add("value", (e.Item.DataItem != null) ? DataBinder.Eval(e.Item.DataItem, "UserID").ToString() : "");
                    rdoBtn.CheckedChanged += new System.EventHandler(this.rdoBtnChecked_Changed);
                    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;
                    chkBox.Attributes.Add("value", (e.Item.DataItem != null) ? DataBinder.Eval(e.Item.DataItem, "UserID").ToString() : "");
                    Panel pnlChkBoxesSet = e.Item.FindControl("pnlSelect") as Panel;
                    pnlChkBoxesSet.Controls.Add(chkBox);
                }
            }
        }
    }

please help me if someone can 请帮助我,如果有人可以

In most scenarios, dynamic controls can be substituted by static controls. 在大多数情况下,动态控件可以由静态控件代替。 By that, you avoid a lot of effort and trouble related to the dynamic creation of controls. 这样,您就避免了与动态创建控件相关的大量工作和麻烦。 That said, in your case you could add a MultiView to the ItemTemplate and show the View that is appropriate for the record: 也就是说,在您的情况下,您可以将MultiView添加到ItemTemplate并显示适合该记录的View

<asp:Repeater ID="rpt" runat="server" OnItemDataBound="rpt_ItemDataBound">
    <ItemTemplate>
        <asp:MultiView ID="multiView" runat="server">
            <asp:View runat="server">
                <asp:RadioButton ID="radioBtn" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Text") %>' Checked='<%# DataBinder.Eval(Container.DataItem, "Checked") %>' />
            </asp:View>
            <asp:View runat="server">
                <asp:CheckBox ID="checkBox" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Text") %>' Checked='<%# DataBinder.Eval(Container.DataItem, "Checked") %>' />
            </asp:View>
        </asp:MultiView>
    </ItemTemplate>
</asp:Repeater>

In the samples, I use the following class for the data items: 在示例中,我对数据项使用以下类:

public class Item
{
    public int Index { get; set; }
    public string Text { get; set; }
    public bool Checked { get; set; }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        var data = Enumerable.Range(1, 6)
            .Select(x => new Item() { Index = x, 
                                      Text = "Item " + x.ToString(), 
                                      Checked = (x % 2) == 0 });
        rpt.DataSource = data;
        rpt.DataBind();
    }
}

In the ItemDataBound event, you set the active view according to your needs: ItemDataBound事件中,根据需要设置活动视图:

protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item ||
        e.Item.ItemType == ListItemType.AlternatingItem)
    {
        var multiView = (MultiView)e.Item.FindControl("multiView");
        multiView.ActiveViewIndex = ((Item)e.Item.DataItem).Index % 2;
    }
}

In a PostBack you can access the static controls of the Repeater items: PostBack您可以访问Repeater项目的静态控件:

protected void btn_Click(object sender, EventArgs e)
{
    var lst = new List<string>();
    foreach (RepeaterItem item in rpt.Items)
    {
        if (item.ItemType == ListItemType.Item ||
            item.ItemType == ListItemType.AlternatingItem)
        {
            var multiView = (MultiView)item.FindControl("multiView");
            if (multiView.ActiveViewIndex == 0)
            {
                var radioBtn = (RadioButton)item.FindControl("radioBtn");
                lst.Add("Radio button is " + (radioBtn.Checked ? "" : "not ") + "checked.");
            }
            else
            {
                var checkBox = (CheckBox)item.FindControl("checkBox");
                lst.Add("Check box is " + (checkBox.Checked ? "" : "not ") + "checked.");
            }
        }
    }
}

From my experience, dynamic controls should be avoided as long as possible because sooner or later they cause a lot of trouble. 根据我的经验,应尽可能避免使用动态控件,因为它们迟早会带来很多麻烦。 I hope this answer gives you an idea on how to achieve this in your scenario. 我希望这个答案能给您一个有关如何在您的方案中实现此目标的想法。

找到了解决方案..可以将viewstatee.Item.ItemIndexID一起使用

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

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