Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Dynamically loaded user control events not firing on first click

Reply
Thread Tools

Dynamically loaded user control events not firing on first click

 
 
Earl Teigrob
Guest
Posts: n/a
 
      02-09-2004
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)?falsebool)s);
}

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


 
Reply With Quote
 
 
 
 
nfedin
Guest
Posts: n/a
 
      02-10-2004
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" <(E-Mail Removed)> wrote in message news:<#(E-Mail Removed)>...
> 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)?falsebool)s);
> }
>
> set
> {
> ViewState["ControlLoaded"] = value;
> }
> }

 
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
dynamically adding controls with events (but events are not firing) SevDer ASP .Net 2 11-13-2007 06:33 AM
click events no longer work on dynamically loaded web user control =?Utf-8?B?Smlt?= ASP .Net 2 08-20-2007 03:32 AM
Event fails to fire first time in dynamically loaded user control( =?Utf-8?B?U3RldmUgQm9vdGg=?= ASP .Net 6 04-06-2005 07:53 PM
Firing Events of dynamically loaded User Control Raed Sawalha ASP .Net 1 10-07-2004 12:38 PM
Events not firing (First time only) in a dynamic user control. Please help. =?Utf-8?B?U2hvdXJpZQ==?= ASP .Net 1 01-21-2004 10:31 PM



Advertisments