Solved

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

Posted on 2004-08-11
7
333 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:fpasquier
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Ok, finally, it seems to work. Something was maybe wrong, but now it's ok. Thanks a lot.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now