Dynamically adding Controls to WebUserControl

  1. #1

    Dynamically adding Controls to WebUserControl

    Hello,
    I created a Web User Control (called 'CTL_Worklist'), in which I statically add an ext:Panel.
    No I try to add a LinkButton to the content of that ext:Panel.
    I call Panel.UpdateContent() and the LinkButton is corrctly displayed in the Panel.
    I also added a DirectEvent with a Url to another aspx site to the LinkButton.
    So far so good.

    But now I try to click on that LinkButton (called 'Btn1') and I get the following strange Error:
    'The control with ID 'CTL_Worklist_Btn1' not found'

    The stacktrace is:
    [HttpException (0x80004005): The control with ID 'CTL_Worklist_Btn1' not found]
       Ext.Net.ResourceManager.RaisePostBackEvent(String eventArgument) +2102
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +176
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

    Am I missing something or what am I doing wrong here?
    Thanks a lot for your help!
    regards,
    bright
    Last edited by geoffrey.mcgill; Oct 17, 2011 at 3:34 PM. Reason: please use [CODE] tags
  2. #2
    Hi,

    You are dynamically adding a Control on one request, but that Control is not automatically available in future requests, unless re-added to the Controls Collection of the Page.
    Geoffrey McGill
    Founder
  3. #3
    Hi,
    thanks for the very quick response!
    But how do I add a control to the controls collection of a Page? Because when I do
    Page.Controls.Add(linkButton)
    it disappears from the Panel :(

    What I do exactly is:
    LOGGER.Debug("Adding Worklist Entries");
    
    			Ext.Net.LinkButton linkButton = new Ext.Net.LinkButton();
    			linkButton.Text = "Lieferschein 0815 zu Bearbeiten";
    			linkButton.ID = "Btn1";
    			linkButton.DirectEvents.Click.Url = "~/Sites/Workflow/Lieferschein.aspx";
    
    			WorklistPanel.ContentContainer.Controls.Add(linkButton);
    			WorklistPanel.UpdateContent();
    Thanks a lot!
    regards,
    Simon
  4. #4
    Hi,

    Using this...

    WorklistPanel.ContentContainer.Controls.Add(linkButton);
    ... actually does add the Control to the Pages .Controls Collection. It's handled under-the-covers for you.
    Geoffrey McGill
    Founder
  5. #5
    Ok, that is fine. But somehow I still get this error, that it cannot find the control which I added. I also looked in the ResourceManager string on the site's source, but the button I added is not contained in it.

    Maybe it has something to do with the fact that I add this LinkButton during a DirectEvent Ajax call?
    Thank you very much,
    bright
  6. #6
    Hi,

    I figured out a few more things:
    It seems that if I add a Control through an Ajax Event in CodeBehind, it is not permanently added to the page. Which meens pressing reload of the browser makes it disappear again. Is there a possibility to not letting the browser forget the controls on reload?
    Can I add the Control permanently to the page? Which means it will also be included in the ResourceManager and in the ViewState of the page?

    Thank you very much,
    bright
  7. #7
    Quote Originally Posted by bright View Post
    Hi,

    I figured out a few more things:
    It seems that if I add a Control through an Ajax Event in CodeBehind, it is not permanently added to the page. Which meens pressing reload of the browser makes it disappear again. Is there a possibility to not letting the browser forget the controls on reload?
    Can I add the Control permanently to the page? Which means it will also be included in the ResourceManager and in the ViewState of the page?

    Thank you very much,
    bright
    Hello

    I am facing the same issue. Conceptually adding/changing controls dynamically is key to my proposed solution. Any answer to this would allow me to progress.

    Thanks in advance
    Darren
  8. #8
    I got it to work for me.
    What I do is when I add a control to the page dynamically, I place marker, identifiying the control, in the session. When the page_load is executed on the page I look for this marker and if it is found, I add the control again to the page.

    Does this make it clear somehow?
    bright
  9. #9
    Quote Originally Posted by bright View Post
    I got it to work for me.
    What I do is when I add a control to the page dynamically, I place marker, identifiying the control, in the session. When the page_load is executed on the page I look for this marker and if it is found, I add the control again to the page.

    Does this make it clear somehow?
    bright
    Hi Bright

    Thanks for the reply. After seeing your answer and then digging around a bit further I decided to re-assess my approach. By this I mean using razor as my view engine and avoid the aspx lifecycle thing that is going on. In saying that, I'm still struggling a little with razor and doing things on the client instead, but I think its the correct way forward.

    Thanks again
    Darren

Similar Threads

  1. Replies: 2
    Last Post: Dec 15, 2011, 11:58 AM
  2. Replies: 1
    Last Post: Aug 17, 2010, 10:07 PM
  3. Replies: 2
    Last Post: Nov 04, 2009, 3:36 PM
  4. WebUserControl in Topbar
    By reto.ruemmeli in forum 1.x Help
    Replies: 0
    Last Post: Mar 16, 2009, 12:34 PM
  5. [CLOSED] Dynamically adding controls to a tav
    By riccardosarti in forum 1.x Help
    Replies: 3
    Last Post: Sep 24, 2008, 9:49 AM

Posting Permissions