472,952 Members | 2,155 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,952 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 8067
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.