中继器和自定义控制 - 动态添加的收集和保留价值 [英] Repeater and Custom Control - Dynamically adding to the collection and retaining values

查看:70
本文介绍了中继器和自定义控制 - 动态添加的收集和保留价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它已经过了这么久,因为我已经使用Web窗体,我发现自己不记得大部分的津贴。

It has been so long since I've used Web Forms I find myself not remembering most of the perks.

我有了一个按钮,一个中继器和转发器的ItemTemplate属性的用户控件是另一个用户控件。

I have a user control that has a button, a repeater and the ItemTemplate property of the repeater is another user control.

<asp:Button runat="server" ID="btnAdd" CssClass="btn" Text="Add" OnClick="btnAdd_Click"/>
<br/>
<asp:Repeater runat="server" ID="rptrRequests">
    <ItemTemplate>
        <uc1:ucRequest ID="ucNewRequest" runat="server" />
    </ItemTemplate>
</asp:Repeater>

的想法是,当添加按钮用户点击的ucRequest的新实例被加入到集合中。在code背后如下:

The idea is that when the user clicks on the Add button a new instance of the ucRequest is added to the collection. The code behind is as follows:

public partial class ucRequests : UserControl
{
    public List<ucRequest> requests
    {
        get
        {
            return (from RepeaterItem item in rptrRequests.Items 
                    select (ucRequest) (item.Controls[1])
                    ).ToList();
        }
        set
        {
            rptrRequests.DataSource = value;
            rptrRequests.DataBind();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack) return;

        requests = new List<ucRequest>();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        var reqs = requests;
        reqs.Add(new ucRequest());
        requests = reqs;
    }
}

在很多谷歌上搜索我现在想起来,我应该绑定在OnInit方法中的中继器,以便ViewState的把控制捕获数据的ucRequest控制范围内后背上的他们,但是当我尝试这样做,我将始终对直放站控制的一个实例,因为其项目集合总是空的。

After much googling I now remember that I should be binding the Repeater in the OnInit method in order for the ViewState to put the captured data of the controls within the ucRequest control on them between post backs but when I try to do that I will always have a single instance of the control on the Repeater since its Items collection is always empty.

我怎么能设法做到这一点?

How could I manage to do this?

先谢谢了。

推荐答案

您只需要在控制整个控制集合的视图状态代替IDS。

You just need control ids in view state stead of entire control collection.

<%@ Control Language="C#" AutoEventWireup="true" 
    CodeBehind="ucRequests.ascx.cs"
    Inherits="RepeaterWebApplication.ucRequests" %>
<asp:Button runat="server" ID="btnAdd" CssClass="btn" Text="Add" 
   OnClick="btnAdd_Click" />
<br /><asp:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder>

<%@ Control Language="C#" AutoEventWireup="true" 
   CodeBehind="ucRequest.ascx.cs" 
   Inherits="RepeaterWebApplication.ucRequest" %>
<asp:TextBox runat="server" ID="TextBox1"></asp:TextBox>

private List<int> _controlIds;

private List<int> ControlIds
{
    get
    {
        if (_controlIds == null)
        {
            if (ViewState["ControlIds"] != null)
                _controlIds = (List<int>) ViewState["ControlIds"];
            else
                _controlIds = new List<int>();
        }
        return _controlIds;
    }
    set { ViewState["ControlIds"] = value; }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        foreach (int id in ControlIds)
        {
            Control ctrl = Page.LoadControl("ucRequest.ascx");
            ctrl.ID = id.ToString();

            PlaceHolder1.Controls.Add(ctrl);
        }
    }
}

protected void btnAdd_Click(object sender, EventArgs e)
{
    var reqs = ControlIds;
    int id = ControlIds.Count + 1;

    reqs.Add(id);
    ControlIds = reqs;

    Control ctrl = Page.LoadControl("ucRequest.ascx");
    ctrl.ID = id.ToString();

    PlaceHolder1.Controls.Add(ctrl);
}

这篇关于中继器和自定义控制 - 动态添加的收集和保留价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆