473,407 Members | 2,629 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,407 software developers and data experts.

Dynamically loaded user control events not firing on first click

PROBLEM:
When a user control is loaded into a PlaceHolder control more than once, the
events do not fire on the first click of a control on the dynamically loaded
user control. In other words, the first time the control is dynamically
loaded, everything works fine. After that, if the control is loaded again
from the page button event handler, the user controls events fail to fire on
the first click

NOTE: I (believe I) am rebuilding all of the controls each time.

EXAMPLE:
I have created a very simple example to illistrate this.
In this example, when the button on the page is pressed once, it loads the
user control and the events of the user control work fine. If, however, the
page button is pressed again, the control is reloaded into the placeholder
but the user control events are lost on the first click of an event firing
control (button in this example). After the first click on the user control
button, the control buttons events start working again.

I am using this in a much more complex senerio and need to find out what I
am doing wrong.

Thank you for your help

Earl

USER CONTROL:
The user control consists of one button and a literal and the botton event
is:

private void Button1_Click(object sender, System.EventArgs e)
{

Literal1.Text="Hello World";
}

ASPX PAGE THAT LOADS CONTROL
The loading page consists of a placeholder control and a button with the
following property and event handlers

private void Page_Load(object sender, System.EventArgs e)
{
if (ControlLoaded)
LoadUserControl();
}
private void Button1_Click(object sender, System.EventArgs e)
{
LoadUserControl();
}

private void LoadUserControl()
{
PlaceHolder2.Controls.Clear();

PlaceHolder2.Controls.Add(Page.LoadControl("~/test/DynamicLoadUserCont.ascx"
));
ControlLoaded=true;
}

public bool ControlLoaded
{
get
{
Object s = ViewState["ControlLoaded"];
return ((s==null)?false:(bool)s);
}

set
{
ViewState["ControlLoaded"] = value;
}
}
Nov 18 '05 #1
1 8128
You problem is that you do not give you dynamically loaded control an
ID. So .Net gives your control it's own name, usually something like
_ctl2 or _ctl3. What is happening is that the name given to your
control is changing. If you explicitly define the ID, this won't
happen.

i.e. MyControl.ID = "MyControl"

The best way to understand this is to use Tracing in the web.config
and look at the control tree.

Neil

"Earl Teigrob" <ea******@hotmail.com> wrote in message news:<#N**************@TK2MSFTNGP11.phx.gbl>...
PROBLEM:
When a user control is loaded into a PlaceHolder control more than once, the
events do not fire on the first click of a control on the dynamically loaded
user control. In other words, the first time the control is dynamically
loaded, everything works fine. After that, if the control is loaded again
from the page button event handler, the user controls events fail to fire on
the first click

NOTE: I (believe I) am rebuilding all of the controls each time.

EXAMPLE:
I have created a very simple example to illistrate this.
In this example, when the button on the page is pressed once, it loads the
user control and the events of the user control work fine. If, however, the
page button is pressed again, the control is reloaded into the placeholder
but the user control events are lost on the first click of an event firing
control (button in this example). After the first click on the user control
button, the control buttons events start working again.

I am using this in a much more complex senerio and need to find out what I
am doing wrong.

Thank you for your help

Earl

USER CONTROL:
The user control consists of one button and a literal and the botton event
is:

private void Button1_Click(object sender, System.EventArgs e)
{

Literal1.Text="Hello World";
}

ASPX PAGE THAT LOADS CONTROL
The loading page consists of a placeholder control and a button with the
following property and event handlers

private void Page_Load(object sender, System.EventArgs e)
{
if (ControlLoaded)
LoadUserControl();
}
private void Button1_Click(object sender, System.EventArgs e)
{
LoadUserControl();
}

private void LoadUserControl()
{
PlaceHolder2.Controls.Clear();

PlaceHolder2.Controls.Add(Page.LoadControl("~/test/DynamicLoadUserCont.ascx"
));
ControlLoaded=true;
}

public bool ControlLoaded
{
get
{
Object s = ViewState["ControlLoaded"];
return ((s==null)?false:(bool)s);
}

set
{
ViewState["ControlLoaded"] = value;
}
}

Nov 18 '05 #2

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

Similar topics

5
by: MS Newsgroups | last post by:
Hi, I have a scenario where I am dynamically adding a control from code when a controls event is fired. The problem I have is that when the newly created control is clicked, the click event does...
8
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image...
1
by: Raed Sawalha | last post by:
I am trying to sort a DataGrid in a user control that is programatically loaded when a link button is clicked. UC1 is loaded onto Page1.aspx after clicking a link button. No problem, loads fine....
4
by: Harry | last post by:
Hello, I have a page with a RadioButtonList and a PlaceHolder control. The RadioButtonList's AutoPostBack attribute is set to TRUE and its SelectedIndexChanged event loads one of three...
6
by: Steve Booth | last post by:
I have a web form with a button and a placeholder, the button adds a user control to the placeholder (and removes any existing controls). The user control contains a single button. I have done all...
1
by: jelle.huygen | last post by:
Hello, I have a problem in ASP.NET 2.0 with the viewstate of my dynamically added user control. I have reproduced the problem with a very simple user control and a very simple page. On my...
5
by: Andrew Robinson | last post by:
I have a page that can load a number of different user controls. Each of these user controls inherits from a common base class and the controls are loaded based on application state, status, etc...
0
by: =?Utf-8?B?Smlt?= | last post by:
I have an application that was built using asp.net 1.1. The application interviews the user with a parent form that loads a series of web user controls (.ascx files)dynamically with...
1
by: Christian Resma Helle | last post by:
Hey guys, I'm working on an AJAX Enabled ASP.NET Web application. I have a TreeView web control and an PlaceHolder web control. My PlaceHolder is inside an UpdatePanel and AsyncPostBacks are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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.