How to get property of a dynamically loaded user control

Last post 07-15-2008 11:46 AM by daniel.saidi. 3 replies.

Sort Posts:

  • How to get property of a dynamically loaded user control

    07-14-2008, 2:32 AM
    • Member
      4 point Member
    • yukito
    • Member since 08-01-2007, 1:49 AM
    • Posts 21

    I have a user control (uc1.ascx) with a property (p), and its user interface includes a textbox. The value of "p" depends on what user types in the textbox.

    I dynamically load uc1 in my page_load event (2 copies):

    1    for (int i = 0; i <= 1; i++)
    2    {
    3     Control uc1 = LoadControl("uc1.ascx");
    4     Table1.Rows[i].Cells[0].Controls.Add(uc1);
    5    }
    
     
    After the page_load event, I can see 2 copies of my user control on web form.

    User now types in the textbox some text, and the property (p) is set.

    Question is: how can I get the values of the properties for the 2 user controls respectively?

     

  • Re: How to get property of a dynamically loaded user control

    07-14-2008, 8:09 AM
    Answer

    When you create your Control, assign it a unique ID, like

    uc1.ID = i.ToString();

    And then when you want to access it, use the function FindControl and give it the id to get the control.

    Hope this helps. 

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    My Blog
    If you get the answer to your question, please mark it as the answer.
  • Re: How to get property of a dynamically loaded user control

    07-14-2008, 7:41 PM
    • Member
      4 point Member
    • yukito
    • Member since 08-01-2007, 1:49 AM
    • Posts 21

    ralph.varjabedian:

    When you create your Control, assign it a unique ID, like

    uc1.ID = i.ToString();

    And then when you want to access it, use the function FindControl and give it the id to get the control.

    Hope this helps. 

    Thank you ralph. I modified my code to this:

     Page_Load:
    for (int i = 0; i <= 1; i++)
    {
     Control uc1 = LoadControl("uc.ascx");
     uc1.ID = "id" + i.ToString();
     Table1.Rows[i].Cells[0].Controls.Add(uc1);
    }

    button_Click:
    {
     uc uc0 = Table1.FindControl("id0") as uc;
     Response.Write(uc0.p.ToString());
     uc uc1 = Table1.FindControl("id1") as uc;
     Response.Write(uc1.p.ToString());
    }

    Working well.

     

  • Re: How to get property of a dynamically loaded user control

    07-15-2008, 11:46 AM
    • Member
      4 point Member
    • daniel.saidi
    • Member since 07-15-2008, 11:38 AM
    • Posts 2

    All dynamically loaded controls are unloaded from the page whenever a postback occurs. They will thus not remain on the page if you perform a PostBack. This is quite cumbersome if you are using a lot of dynamically loaded controls in a "dynamic way", where controls can be loaded/unloaded in any order depending on what actions the user performs.

    After each postback, you have to manually reload the controls to the page and make sure to do so at the right time in the page life cycle and with the correct ID. If you do anything in the incorrect order, they will not work properly. For instance, events will not fire, nested controls will not load etc.

    This, still rather managable, issue can however become quite a problem for complex web applications where dynamically loaded controls are essential to keep down byte size and avoid a lot of unnecessary functionality. It may become quite tricky to keep track of which controls to reload, which to unload etc. if the user's actions determine how they are loaded/unloaded.

    A very simple and handy solution to this issue is described in this blog:

    http://daniel-saidi.blogspot.com/2008/07/aspnet-dynamically-loaded-usercontrol.html

    It features a slimmed, downloadable class which, hopefully, will take care of this issue for you. When you attach a control, you can cast it to the class and then access the class properties as you would do with any class instance.

    The blog is removed since a while back. I have created a small class that takes care of this issue and will upload it together with the old text asap.

Page 1 of 1 (4 items)