Solved

How to handle an event in controls and keep it up to date.

Posted on 2004-08-11
7
336 Views
Last Modified: 2010-04-15

I have an ASCX control (called A) that is used by an ASPX page . The aspx page has an event (called E) listener and all the appropriated functions to handle this event coming from another ASCX page (called B).

Once the event E has been completed, a property of the control A is set and I have enough information to "render" the control A which is made by a set of dynamic controls. My problem is that I do not know how to add event handlers to these dynamic controls. I tried the following code (in control A):

override protected void OnPreRender(EventArgs e)
          {
               GenerateAllMyControls(myProperty);
               for(int i=0; i<this.FindControl("my3345").Controls.Count; i++)
               {
                    if(this.FindControl("my3345").Controls[i] is LinkButton)
                    {
                         LinkButton lb = (LinkButton)this.FindControl("my3345").Controls[i];
                         lb.Command+= new System.Web.UI.WebControls.CommandEventHandler(this.linkbuttonclick);
                    }
               }
          }

The main problem is that the requested property (myProperty) is not set when the Page_Load of this control A is called because all "postback" events are treated afterwards. So when can I generate these controls and give they their respective CommandEventHandler ?

I know the question is a bit tricky because out of context... But please help!!!
0
Comment
Question by:fpasquier
  • 4
  • 3
7 Comments
 
LVL 20

Accepted Solution

by:
TheAvenger earned 500 total points
ID: 11772892
OK, let's try to think a little... The order of event execution in ASP page was like:

Initialize
Load view state
Process postback data
Load
Send postback change notifications
Handle postback events
Prerender
Save state
Render
Dispose
Unload

So, in order for your DYNAMIC controls to be able to handle events then need to be RECREATED at least before the step "Handle postback events" (maybe even before Send postback change notifications). However the property myProperty is ready for you only after the postback event is handled and maybe this property is different between postbacks.

The most important thing here is that you have to RECREATE THE DYNAMIC CONTROLS FROM THE PREVIOUS FORM LOAD in order to handle the events from them. So my suggestion is the following: once you render the controls (so about the line  GenerateAllMyControls(myProperty);) save the value of myProperty to the ViewState of the page like this:

this.Page.ViewState["myProperty"] = myProperty;

Then in the Load event of the custom control, read the property:

myProperty = (myPropertyType)this.Page.ViewState["myProperty"];

and recreate all controls from the previous form load. Note: you should do this in the Load event of the control, so that it takes place before the step "Handle postback events" as discussed.

Possible problems:
- You need to have the ViewState of the page enabled. This is the default but you may have changed it.
- You need to be careful when creating the controls for the new page load. Maybe there are some controls that were already created because they existed on the previous load and maybe there are controls that you don't need any more. So be careful to create/remove the right controls.

I hope I was at least a little bit clear what I mean. Have luck!
0
 

Author Comment

by:fpasquier
ID: 11773792
This is very interesting. The only problem is that I can not get them back in the Load event to "offer" them an event handler, because they do not seem to be accessible via their father ID.

I modified the PreRender fonction in the following way
override protected void OnPreRender(EventArgs e)
            {
                  if(myProperty!=null)
                  {
                        GenerateAllMyControls(myProperty);
                  }

                  ViewState["myProperty"] = myProperty;
                  base.OnPreRender(e);
            }

And I added the following line in the Page_Load:
         if(Page.IsPostBack)
                  {

                        myProperty= (string)ViewState["myProperty"];
                        
                        //this.FindControl("my3345").Controls.Clear();
                        GenerateAllMyControls(myProperty);
                        for(int i=0; i<this.FindControl("my3345").Controls.Count; i++)
                        {
                              if(this.FindControl("my3345").Controls[i] is LinkButton)
                              {
                                    LinkButton lb = (LinkButton)this.FindControl("my3345").Controls[i];
                                    lb.Command+= new System.Web.UI.WebControls.CommandEventHandler(this.linkbuttonclick);
                              }
                        }
                        
                  }
If I comment on the line that is "slash-slashed", I get my controls TWICE, with some differences (due the property change)
If I enable the "slash-slashed" line, Then I get an error saying the the control can not be found.
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 11773855
You should move the part of assigning events in the OnPreRender. They have to be assigned BEFORE the page is sent to the user, not after it comes back.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:fpasquier
ID: 11774199
I fully agree with your last comment, but in this case, we are exactly in the same situation I was at the beginning: the event does not work because it is "too late" to connect it to a dynamic control.
The controls that are created in the Page_Load are  "too old" (do not correspond to the actual myProperty)
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 11774355
You have 2 executions of the page:

1. You do nothing in the Page_Load; create the controls in PreRender and attach events to it
2. You recreate the controls from 1 in the Page_Load; the events are fired; remove unneeded controls from step 1 and create new needed ones in PreRender and attach new events to it

I don't see where it's too late or somethig...
0
 

Author Comment

by:fpasquier
ID: 11774709
I am sorry but in my case the events are not fired.
If would like to see, my code is here
http://www.fabnet.ch/test/contextCategories.ascx.cs
0
 

Author Comment

by:fpasquier
ID: 11809530
Ok, finally, it seems to work. Something was maybe wrong, but now it's ok. Thanks a lot.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question