动态添加的用户控件的事件处理

时间:2010-04-26 11:58:49

标签: asp.net

我有一个用户控件说SearchVendor.ascx,它包含4个按钮,一个gridview和另一个用户控件。 我需要动态加载控件作为模式弹出

我收到此代码

var uc = Page.LoadControl("~/blah/VendorProductSearch.ascx") as VPSearch;
uc.ShowVPSearch(true);
_tempPlaceHolder.Controls.Add(uc);

它可以正常加载控件,但第二次控件消失时点击任意按钮???

我希望动态添加的控件保持不变,直到用户点击取消按钮

有关如何实现这一点的想法吗?

谢谢&问候, Francis P。

1 个答案:

答案 0 :(得分:0)

每次回发后都需要将控件添加到页面中。我正在使用未知数量的控件进行类似的操作,并且我保留了添加数量的计数,因此我可以在回发时重新添加它们。要在取消时删除它,您需要调用Controls.Remove“myUserControlID”(传入您分配给它的ID)。

public int AddressCount
{
    get
    {
       string s = (string)ViewState["addressCount"];
       return ((s == null) ? 1 : Convert.ToInt32(s));
    }

    private set { ViewState["addressCount"] = value.ToString(); }
}    

protected void Page_Load(object sender, EventArgs e)
{
   for (int i = 0; i < AddressCount; i++) 
   {
      Control ai = LoadControl("~/controls/addressItem.ascx");
      addressContainer.Controls.Add(ai); // addressContainer is a PlaceHolder control on the page
   }
}
相关问题