Solved

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

Posted on 2004-08-11
7
337 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

840 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