473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

PlaceHolder and UserControls with Events

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.

Aug 20 '06 #1
0 1531

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: blue | last post by:
I have a drop-down list, a radio button list and a submit button. I'm adding these controls to a table and I'm adding the table to a Placeholder. I'm adding it to the Placeholder because I don't...
1
by: John | last post by:
Hi all, My web app uses 3 different placeholders on one page and dynamically load usercontrols into them. That's all fine but now I have two situations which puzzle me: 1. I have a sub-folder...
1
by: Angel | last post by:
I have added controls to the placeholder control. All the controls that were added have EnableViewState = true including the placeholder. One of the controls has a button that performs a postback. My...
9
by: Anders K. Jacobsen [DK] | last post by:
Hi I have this that adds some usercontrol (UCTodays.ascx) to a placeholder foreach(A a in B){ UCTodays ucline = (UCTodays )LoadControl("UCTodays.ascx");...
2
by: N. Demos | last post by:
I have a user control with code behind of which two instances are created/declared in my aspx page. The aspx page has code behind also, as I need to access methods of the usercontrols on page...
3
by: PKin via DotNetMonster.com | last post by:
Hi, I have a web page with a radioButtonList with 3 buttons (B1,B2 and B3) and a placeholder. B1 will put an .ascx file (Pl1.ascx) in the placeHolder, B2 will do the same with Pl2.ascx and B3...
1
by: Joe | last post by:
Hello All, I have a user control which is composed of a label and a dropdownlist. In my code I add the user control to a placeholder on the webform. Now I want to be able to retrieve the...
2
by: David | last post by:
I have a UserControl loaded into a PlaceHolder and I have a button there which is suppose to load another UserControl into the same PlaceHolder and hide the previous one. Why does the events of the...
1
by: m.bagattini | last post by:
Hello folks, I'm in trouble with an application using methodologies in subject. The idea is to have a single-page application. The host aspx page coordinates all visualizations, loading and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.