转发器内的ASP.NET自定义用户控件:动态添加项目

时间:2017-10-05 22:18:03

标签: c# asp.net user-controls repeater

我有一个客户用户控件AddressControl.ascx,它包含在转发器控件中。当我的页面首次加载时,一切似乎都正常工作,我的转发器正在为Page_Load事件中填充的虚拟地址创建两个项目。但是,当我点击添加地址按钮时,会添加第3个转发器项目,但现在我的所有其他转发器项目都是空的(即屏幕上的所有字段都变空)。

在调试时,我注意到在第一次加载时,AddressControl的Page_Load事件具有地址对象。但是,当我们点击添加地址按钮后来到Page_Load时,地址对象为空。

*注意:“添加地址”按钮的目标是显示一组空控件,以允许用户输入新地址。

非常感谢任何帮助。

AddressControl.ascx

public partial class AddressControl : System.Web.UI.UserControl
    {
        public Address address { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (address != null)
            {
                txtStreet1.Text = address.Address1;
                txtStreet2.Text = address.Address2;
                txtCity.Text = address.City;
                cboRegion.SelectedIndex = 2;
                txtPostalCode.Text = address.PostalCode;
            }
        }
    }

Default.aspx的

<asp:Repeater ID="AddrRepeater" runat="server" OnItemDataBound="AddrRepeater_ItemDataBound">
    <ItemTemplate>
        <uc1:AddressControl runat="server" id="ctlAddress" />
    </ItemTemplate>
</asp:Repeater>
<asp:Button ID="btnAdd" runat="server" text="Add Address" OnClick="btnAdd_Click" />

Default.aspx.cs

public partial class Default : System.Web.UI.Page
{
    public List<Address> Addresses
    {
        get
        {
            var addresses = HttpContext.Current.Session["addressList"] as List<Address>;
            if (addresses == null)
            {
                addresses = new List<Address>();
                HttpContext.Current.Session["addressList"] = addresses;
            }
            return addresses;
        }
        set
        {
            HttpContext.Current.Session["addressList"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Addresses = new List<Address>()
            {
                new Address { Address1 = "123 Main St", Address2 = "", City = "New York", State = "NY", PostalCode = "10001" },
                new Address { Address1 = "555 Fake St", Address2 = "", City = "New York", State = "CA", PostalCode = "91367" }
            };

            AddrRepeater.DataSource = Addresses;
            AddrRepeater.DataBind();
        }
    }

    protected void AddrRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            var address = e.Item.DataItem as Address;

            var addrCtl = e.Item.FindControl("ctlAddress") as AddressControl;
            addrCtl.address = address;
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Address newAddress = new Address();
        Addresses.Add(newAddress);

        AddrRepeater.DataSource = Addresses;
        AddrRepeater.DataBind();
    }
}

2 个答案:

答案 0 :(得分:0)

如果您希望控件在回发后保留值

,则需要实现视图状态跟踪
    message backendServiceReq ={};
    string reqType = "application/soap+xml";
    string soapDefinition;
    soapDefinition, _ = (string) namespaceMap["1.2"];
    messages:setXmlPayload(backendServiceReq, soapRequest);
    messages:setHeader(backendServiceReq, "Content-Type", reqType);
    if (soapAction != "null") {
        messages:setHeader(backendServiceReq, "SOAPAction", soapAction);
    } 
   message response = httpConnector.post(url, backendServiceReq);

答案 1 :(得分:0)

我明白了。

关于Page Event Life Cycle的全部内容。如您所见,我试图访问Address对象并填充PageLoad事件中的文本框字段。 这是错误的。

以下是事件发生的顺序:

1. Default.aspx.cs - Page_Load
2. AddressControl.ascx.cs - Page_Load // Address object is still null here because it hasn't been assigned a value from the Default.aspx.cs's ItemDataBound event.
3. Default.aspx.cs - ItemDataBound // Address assigned here
4. AddressControl.ascx.cs - OnPreRender // Here the Address object is NOT null.

如果我的AddressControl.ascx.cs中的Address对象是使用ViewState实现的,那么事件没有用,因为在上面的步骤2中,它将引用前一个PostBack中的旧对象。