PlaceHolder and UserControls with Events

Discussion in 'ASP .Net' started by seigo, Aug 20, 2006.

  1. seigo

    seigo Guest

    Hello,

    I faced with the following problem. I have a PlaceHolder on a page and
    a few UserControls which have custom events, for instance:

    public delegate void SelectHandler(object sender, SelectEventArgs
    e);
    public event SelectHandler OnSelect;

    protected void Page_Load(object sender, EventArgs e)
    {
    DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
    SelectEventArgs ev = new
    SelectEventArgs(DropDownList1.SelectedValue);
    if (OnSelect != null)
    {
    OnSelect(this, ev);
    }
    }

    This control fire an event when user select some value from DropDown
    and click on Button.

    On page where I placed PlaceHolder I need to dynamically load those
    UserControls. I use the following code:

    protected void Button1_Click(object sender, EventArgs e)
    {
    PlaceHolder1.Controls.Clear();
    UserControls_WebUserControl control =
    (UserControls_WebUserControl)LoadControl("~/UserControls/WebUserControl.ascx");
    control.OnSelect += new
    UserControls_WebUserControl.SelectHandler(control_OnSelect);
    PlaceHolder1.Controls.Add(control);
    PlaceHolder1.DataBind();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
    PlaceHolder1.Controls.Clear();

    PlaceHolder1.Controls.Add(LoadControl("~/UserControls/WebUserControl2.ascx"));
    PlaceHolder1.DataBind();
    }

    But I got the exception when trying to load a new UserControl at second
    time:

    Failed to load viewstate. The control tree into which viewstate is
    being loaded must match the control tree that was used to save
    viewstate during the previous request. For example, when adding
    controls dynamically, the controls added during a post-back must match
    the type and position of the controls added during the initial request.

    And if I load at first UserControl where I use custom event it doesn't
    work.

    I decided to place loading UserControl with custom event in overridden
    OnInit method:

    protected override void OnInit(EventArgs e)
    {
    base.OnInit(e);
    PlaceHolder1.Controls.Clear();
    UserControls_WebUserControl control =
    (UserControls_WebUserControl)LoadControl("~/UserControls/WebUserControl.ascx");
    control.OnSelect += new
    UserControls_WebUserControl.SelectHandler(control_OnSelect);
    PlaceHolder1.Controls.Add(control);
    PlaceHolder1.DataBind();
    }

    but know I cannot load other UserControl to PlaceHolder.

    Hope the problem is clear and thanks for any ideas or help!

    Regards,
    Alex.
    seigo, Aug 20, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. blue

    Placeholder and Events

    blue, Nov 12, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    4,556
  2. Nathan Sokalski

    Order of events, databinding, and UserControls

    Nathan Sokalski, May 21, 2006, in forum: ASP .Net
    Replies:
    9
    Views:
    462
    Nathan Sokalski
    May 23, 2006
  3. David

    PlaceHolder and UserControls

    David, Sep 28, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    296
    David
    Oct 2, 2006
  4. nail

    Build control in a placeholder or like a placeholder

    nail, Sep 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    332
  5. Chris

    Events Events Events Please Help

    Chris, Aug 30, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    359
    Chris
    Aug 30, 2005
Loading...

Share This Page