Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > PlaceHolder and UserControls with Events

Reply
Thread Tools

PlaceHolder and UserControls with Events

 
 
seigo
Guest
Posts: n/a
 
      08-20-2006
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PlaceHolder and UserControls David ASP .Net 2 10-02-2006 12:25 AM
Order of events, databinding, and UserControls Nathan Sokalski ASP .Net 9 05-23-2006 04:32 PM
Events Events Events Please Help Chris ASP .Net Web Controls 0 08-30-2005 08:21 PM
Build control in a placeholder or like a placeholder nail ASP .Net Building Controls 0 09-10-2004 07:57 PM
Placeholder and Events blue ASP .Net 4 11-12-2003 10:24 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57