473,888 Members | 1,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(o bject sender, SelectEventArgs
e);
public event SelectHandler OnSelect;

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

protected void Button1_Click(o bject 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(o bject sender, EventArgs e)
{
PlaceHolder1.Co ntrols.Clear();
UserControls_We bUserControl control =
(UserControls_W ebUserControl)L oadControl("~/UserControls/WebUserControl. ascx");
control.OnSelec t += new
UserControls_We bUserControl.Se lectHandler(con trol_OnSelect);
PlaceHolder1.Co ntrols.Add(cont rol);
PlaceHolder1.Da taBind();
}

protected void Button2_Click(o bject sender, EventArgs e)
{
PlaceHolder1.Co ntrols.Clear();

PlaceHolder1.Co ntrols.Add(Load Control("~/UserControls/WebUserControl2 .ascx"));
PlaceHolder1.Da taBind();
}

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(EventArg s e)
{
base.OnInit(e);
PlaceHolder1.Co ntrols.Clear();
UserControls_We bUserControl control =
(UserControls_W ebUserControl)L oadControl("~/UserControls/WebUserControl. ascx");
control.OnSelec t += new
UserControls_We bUserControl.Se lectHandler(con trol_OnSelect);
PlaceHolder1.Co ntrols.Add(cont rol);
PlaceHolder1.Da taBind();
}

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 1559

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

Similar topics

4
2568
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 know exactly where the table will be located on the page until runtime. Before the form control table is added to the Placeholder, I'm adding a whole bunch of tables to the Placeholder. This is a flowchart program and I have multiple action boxes...
1
2292
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 of html pages from my site and I need to show one of these pages at a time within a specific placeholder on my main page. How do I go about doing this?
1
5383
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 question is when the button is clicked and a postback is performed, the controls that were in the placeholder no longer appears on the screen! Am I doing something wrong here? Why are the controls back on the screen eventhough they have enable view...
9
2459
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"); ucline.Initializecontrol(line,alternate); Placeholder1.Controls.Add(ucline); }
2
1986
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 submit. I've read several post here and articles on the web on this topic. What little I have learned from them is that you have to pre-compile the usercontrol in order to access it in the aspx code behind. I did this, compiling the usercontrol...
3
1820
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 with Pl3.ascx. Each .ascx file has controls that can fire som events(button, dropList...). When I start the page, B1 is default and every control on Pl1.ascx work very fine. When I choose B2 or B3 the .ascx file is loaded in the placeholder but no...
1
2934
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 selected value from the drop downlist when a user clicks a button server control. Here is the code that populates the user control and placeholder: Dim UControl As AugmentedDropDownList =
2
1459
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 controls that are inside the second UserControl doesn't work the first time I try to rise them?
1
1314
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 populating different usercontrols on a status basis. To listen for changes I'm bubbling up events from the usercontrols. Each event raises up to the host aspx page, which decides what to do, what to load etc.
0
9800
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11176
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10772
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10880
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7988
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7144
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6012
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4241
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3246
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.