Dynamic Events

Last post 02-03-2009 8:23 PM by ambic. 2 replies.

Sort Posts:

  • Dynamic Events

    02-01-2009, 9:08 PM
    • Member
      32 point Member
    • ambic
    • Member since 04-28-2005, 9:38 PM
    • Posts 19

    I have a control that I have written that dynamically builds a few LinkButtons that send back an onclick event.  I wire these up during the OnInit and toggle the visibility to make them accessible by the users.   I originally attempted to create the objects on the fly, but on the reload of the control the event call would vanish. 

     I now have to add a new piece of functionality to my control.  Its basically a "Search" control, that will bring back a list of items.  Each item needs a button to the side saying "Select this item" and on selecting it needs to pass back a value that I can react to.  I cannot create the event handler in the onInit section because I have no idea how many I will need, so I will need to create it afterwards.  But these events don't do anything because on postback the controls they were associated with wont exist.


    Any suggestions?

  • Re: Dynamic Events

    02-01-2009, 9:38 PM
    • Member
      32 point Member
    • ambic
    • Member since 04-28-2005, 9:38 PM
    • Posts 19

    Just a bit more info about the type of thing I'm doing:  This is from the microsoft ASP.Net books online

    "Explicit Binding for Dynamic Controls

    If you create controls by declaring them in markup, you can bind events to methods using an attribute (for example, onclick) or in Visual Basic, by using the Handles keyword. If you create controls dynamically (in code), you cannot use either of these methods, because the compiler does not have a reference to the control at compilation time.

    In that case, you must use explicit event binding.

    ...

    Button b = new Button;
    b.Text = "Click";
    b.Click += new System.EventHandler(ButtonClick);
    Placeholder1.Controls.Add(b);

    "

    I experimented with the code suggested by Microsoft.  (Ignoring the fact that it doesnt compile - lol) I have a search button on my control.  THe OnClick event is set during the OnInit stage.

    protected override void OnInit(EventArgs e)
    {
       // ... Other code removed ... //
    SearchExecute = new Button();
    SearchExecute.Text =
    "Search";
    SearchExecute.Click +=
    new EventHandler(btnSearch_Click);
    SearchActionExecute.Controls.Add(SearchExecute);  // This is a table cell created earlier in the routine, removed for brevity //

    }

    private void btnSearch_Click(object sender, EventArgs e)
    {
    doSearch(1);
    RewirePageLinks();
    b =
    new Button();
    b.Text =
    "Click";
    b.Click +=
    new System.EventHandler(ButtonClick);
    Controls.Add(b);
    }

    The new Button B (declared to the class) appears fine. But on clicking reposts the whole page, and the event handler ButtonClick is never called.  I need to be able to dynamically add about 20 buttons on average, but could have to do quite a few more, and predeclaring them all in the OnInit is messy.

    -----

     

    [EDIT: Work Around:  I have a workaround.  I am currently setting PostBackUrl to another page which handles parameters set inside the URL at the point of construction.  This works okay but doesn't really solve the issue.  I am posting this here in case it helps someone else]

     

     

  • Re: Dynamic Events

    02-03-2009, 8:23 PM
    Answer
    • Member
      32 point Member
    • ambic
    • Member since 04-28-2005, 9:38 PM
    • Posts 19

    This took a lot of achieving. I read internet pages for more than a day before coming up with the following:

     

    Contrary to what a LOT of forums say, where you must recreate all controls on the OnInit override, you dont have to.  What you must do is this:

    1. Set OnInit to do the following:

    protected override void OnInit(EventArgs e)
    {
      
    base.OnInit(e);
       Page.RegisterRequiresControlState(
    this);
       ...  Your other on init code
    }

    This forces the Control State memory bank to be turned on.

    2.  Go to the routine where you are creating the controls, and specify the ID of the buttons.  If you do not, you will not be able to complete stage 3.

    Button btn = new Button();
    btn.ID =
    "btnProg" + prog.ProgId.ToString();
    btn.Click +=
    new EventHandler(SelectProgramme_click);
    tcInfo.Controls.Add(btn);
    hplProgrammeLinks.Add(btn);

    3. Create/Edit the Page Load to reconstruct your controls which will no longer exist.  They must have the SAME IDs, so I call the same routine that generated them in the first place.  The page_load does work because it occurs in the page lifecycle BEFORE the events are fired.  If the IDs are inconsistent your events will vanish.  This will cause an unintended side effect where your routine for generation is called OnLoad and then again with the page events.  If you do need data reads etc, put them into properties in a seperate routine, and have the object generation called seperately.

    if (Page.IsPostBack) && (!String.IsNullOrEmpty(SearchString.Text))
    {
       doSearch(1);
    }

     

Page 1 of 1 (3 items)