Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

User Control events in C#

Original Message
Name: mini_otis
Date: March 23, 2005 at 18:08:40 Pacific
Subject: User Control events in C#
OS: Windows XP
CPU/Ram: AMD 64 3500
Comment:
I am having major trouble with C# and programatically loading user controls with public event handling on the controls. It's more extensively described here: http://pastecode.net/?action=viewpost&tag=956 If someone can tell me what I am doing wrong, or even if what I am trying is simply not possible (something I am finding a lot at the moment but a lot is apparently being fixed for ASP.NET 2.0)

Report Offensive Message For Removal


Response Number 1
Name: SN
Date: March 24, 2005 at 06:45:21 Pacific
Subject: User Control events in C#
Reply: (edit)
That took a while to go through, and I'm still not certain I understand what you're trying to accomplish, but I'll give it a go anyways.

First let me restate your question in more general terms: You are adding user controls at runtime into a placeholder, and the events for those user controls aren't being fired on postback. Is this the basic idea?

This is why the designers of ASP.Net made the page load method execute before events...To give you a chance to reload controls so their events will fire. The problem is that the browser is sending your page events for controls that don't exist (you added them at runtime the last time the code ran, now they're gone), so obviously their events won't be fired.

What you need to do is essentially recreate (in the page load event) the state of the page to exactly how it was last time you outputted it to the browser. This way the user controls exist, and ASP.NET will fire their events appropriately. In your case, this would involve reloading the user control you loaded last time. Make sure it gets the same ID, name, etc.

You seem to have been pretty close to figuring this out on your own, sorry if you already knew this and I missed the question. If so, try to rephrase in more generic terms and I'll try again.

-SN


Report Offensive Follow Up For Removal

Response Number 2
Name: mini_otis
Date: March 24, 2005 at 16:16:48 Pacific
Subject: User Control events in C#
Reply: (edit)
I have loaded the user controls back in ok on postback - that's not my problem. My problem is making sure the right user control is being loaded in. Because the events are fired after page load my session variables are not updated so I don't know which user control is supposed to be loaded next. If I then load the requested user control after the postback event handling has done the resulting user control does not have its events (bubbled to the base page) handled because any event handling specified on controls generated after Page_Load seems to be completely ignored!

What I am doing now is essentially bringing the event handling of the previous form in to the Page_Load myself by processing the Request.Form variables before they are supposed to be in my own little way (I run them through a switch case!). That way I get the right user control and it has all the right event handling.

Someone suggested something similar but it didn't work and I improvised from there.


Report Offensive Follow Up For Removal

Response Number 3
Name: SN
Date: March 24, 2005 at 16:26:11 Pacific
Subject: User Control events in C#
Reply: (edit)
"I have loaded the user controls back in ok on postback - that's not my problem."
I don't mean loading the new user control (the one the user selects) in on postback, I mean the one you had in the page the last time around. So you'll actually load two user controls into the placeholder, the previous one, and the one the user wants to see next.

I don't see anywhere in the page load event where you are loading the control...You need to call a function that checks to see which user control you had in the placeholder the last time around (presumably by checking a session variable of some kind) and loads it in. Then its events will fire.

"any event handling specified on controls generated after Page_Load seems to be completely ignored!"

Exactly...That's what I'm talking about. You have to reload them in page_load for their events to fire. You shouldn't have to do any funky processing of the request.form variables yourself.

Good luck,
-SN


Report Offensive Follow Up For Removal




Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: User Control events in C#

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




DSHUB24 Connection Problems

need help with dsl and dial up

novel 3.12

help mandriva install last straw!

Icon Scaling in Explorer Bar


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC