2017-10-05 111 views
0

我有一個客戶用戶控件AddressControl.ascx,它包含在中繼器控件中。當我的頁面第一次加載時,一切似乎都正常,我的轉發器正在爲Page_Load事件中填充的虛擬地址創建兩個項目。但是,當我點擊添加地址按鈕時,會添加第3個轉發器項目,但現在所有其他轉發器項目都是空的(即屏幕上的所有字段都變空)。中繼器內的ASP.NET自定義用戶控件:動態添加項目

在調試時,我注意到在第一次加載時,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(); 
    } 
} 

回答

0

你將需要實現視圖狀態軌跡如果你想讓控件在回發後保留這些值

public Address address 
    { 
     get 
     { 
      object o = ViewState["address"]; 
      return o == null ? null : (Address)o; 
     } 
     set { 
      ViewState["address"] = value; 
     } 
    } 
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我的地址對象是使用ViewState中實現的,它不會因爲上述步驟2中的幫助下,它會引用上一個PostBack中的舊對象。

相關問題