dynamically adding user controls

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default dynamically adding user controls

    I have a web form that has a button called "Add Blank Row".
    Every time this button is pressed a new "blank row" user
    control should be added to the web form. This is done
    dynamically in the button's event handler by calling
    Page.LoadControl(). Everything works ok except that the
    web form can't seem to remember the user controls that are
    added.

    The moment a postback occurs, any user controls that exist
    disappear.

    With some googling, I found a page which said "you must
    load dynamically created controls on EVERY postback".

    Is this true?And if so, could someone please explain why
    and how to work around it?

    (I assume it would require keeping track of the user
    controls as they are added but I can't think of a
    efficient way to go about doing this)


    Cheers, Dune
    Dune Guest

  2. Similar Questions and Discussions

    1. Adding controls dynamically to a datagrid
      Hi, We have a requirement, where based on the UI type selected in a dropdown, a control should get added to the column of a grid dynamically. For...
    2. Dynamically Adding Controls
      I am trying to dynamically add controls to my page, but am having trouble with controls such as buttons. I have been able to add simple controls...
    3. Dynamically adding controls to a data repeater
      I would like to create a composite control based on a data repeater how do I dynamically add and bind child controls to it? I imagine this is not a...
    4. Dynamically adding controls to page SOURCE file
      Hi, I have a class, derived from "Control" which I can include on a page using: <tag:MyControl attr1=value></tag:MyControl> My questions are:...
    5. Event not firing. Adding controls dynamically to UserControl
      I am adding controls to the UserControl dynamically and then loading the UserControl Dynamically.But I am facing problem with firing of click event...
  3. #2

    Default Re: dynamically adding user controls

    "Dune" <mleow@paradise.net.nz> wrote in message
    news:024201c383e7$e651a420$a001280a@phx.gbl...
    > I have a web form that has a button called "Add Blank Row".
    > Every time this button is pressed a new "blank row" user
    > control should be added to the web form. This is done
    > dynamically in the button's event handler by calling
    > Page.LoadControl(). Everything works ok except that the
    > web form can't seem to remember the user controls that are
    > added.
    >
    > The moment a postback occurs, any user controls that exist
    > disappear.
    >
    > With some googling, I found a page which said "you must
    > load dynamically created controls on EVERY postback".
    >
    > Is this true?And if so, could someone please explain why
    > and how to work around it?
    This is true, and not just for user controls. It's true for all controls.

    Each request, whether an initial request (GET) or a postback (POST) creates
    a new instance of your page class. This instance has no relationship to the
    instance created on the previous request, nor is there a relationship
    between the instance created for the initial request and the one created on
    PostBack. In particular, controls you added on the initial request will not
    exist in the instance created on PostBack - unless you create them.

    Now, if ViewState is enabled, once you create the controls and add them to
    their parent's Controls collection, the controls will load their own
    ViewState. However, this requires that the controls be created in the same
    order each time.
    > (I assume it would require keeping track of the user
    > controls as they are added but I can't think of a
    > efficient way to go about doing this)
    Actually, ViewState is a good way to do this. On the initial request, when
    you decide how many rows you'll have the first time around, set
    ViewState["TableRows"] to the number of rows. On PostBack, you can read
    ViewState["TableRows"] to find out how many rows you'll need to create. In
    the Click event handler for your "New Row" button, you can add the new row
    and increment ViewState["TableRows"]. On the next PostBack, you'll read
    ViewState["TableRows"] to find out how many rows you'll need to create...
    --
    John Saunders
    Internet Engineer
    [email]john.saunders@surfcontrol.com[/email]


    John Saunders Guest

  4. #3

    Default Re: dynamically adding user controls

    thanks :)

    got it all working now

    >-----Original Message-----
    >"Dune" <mleow@paradise.net.nz> wrote in message
    >news:024201c383e7$e651a420$a001280a@phx.gbl...
    >> I have a web form that has a button called "Add Blank
    Row".
    >> Every time this button is pressed a new "blank row" user
    >> control should be added to the web form. This is done
    >> dynamically in the button's event handler by calling
    >> Page.LoadControl(). Everything works ok except that the
    >> web form can't seem to remember the user controls that
    are
    >> added.
    >>
    >> The moment a postback occurs, any user controls that
    exist
    >> disappear.
    >>
    >> With some googling, I found a page which said "you must
    >> load dynamically created controls on EVERY postback".
    >>
    >> Is this true?And if so, could someone please explain why
    >> and how to work around it?
    >
    >This is true, and not just for user controls. It's true
    for all controls.
    >
    >Each request, whether an initial request (GET) or a
    postback (POST) creates
    >a new instance of your page class. This instance has no
    relationship to the
    >instance created on the previous request, nor is there a
    relationship
    >between the instance created for the initial request and
    the one created on
    >PostBack. In particular, controls you added on the
    initial request will not
    >exist in the instance created on PostBack - unless you
    create them.
    >
    >Now, if ViewState is enabled, once you create the
    controls and add them to
    >their parent's Controls collection, the controls will
    load their own
    >ViewState. However, this requires that the controls be
    created in the same
    >order each time.
    >
    >> (I assume it would require keeping track of the user
    >> controls as they are added but I can't think of a
    >> efficient way to go about doing this)
    >
    >Actually, ViewState is a good way to do this. On the
    initial request, when
    >you decide how many rows you'll have the first time
    around, set
    >ViewState["TableRows"] to the number of rows. On
    PostBack, you can read
    >ViewState["TableRows"] to find out how many rows you'll
    need to create. In
    >the Click event handler for your "New Row" button, you
    can add the new row
    >and increment ViewState["TableRows"]. On the next
    PostBack, you'll read
    >ViewState["TableRows"] to find out how many rows you'll
    need to create...
    >--
    >John Saunders
    >Internet Engineer
    >john.saunders@surfcontrol.com
    >
    >
    >.
    >
    Dune Guest

  5. #4

    Default Re: dynamically adding user controls

    Hi John,

    I've couple of questions with respect to adding
    WebControls to a page.

    I've written a WebControl that encapsulates an client-side
    ActiveX control. Now, I want to add multiple instances of
    this control to my page.

    If I statically bind it with


    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
    AutoEventWireup="false"
    Inherits="ControlLifeCycle.WebForm1" %>
    <%@ Register TagPrefix="dial"
    Namespace="BroadVu.Util.Widgets" Assembly="Dial" %>
    <HTML>
    <HEAD>
    <title>WebForm1</title>
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post"
    runat="server">
    <Dial:dial id="addedDial"
    runat="server"></Dial:dial>
    </form>
    </body>
    </HTML>

    it works fine. The output HTML is rendered properly
    within the <form> tag.

    Issue #1
    --------
    I'm using Page.RegisterClientScript &
    Page.RegisterStartupScript methods inside my WebControl to
    emit some JavaScript. They are NOT getting emitted.
    Though if I have a custom Render(Page page) method, the
    scripts get emitted!

    Issue #2
    --------
    If I add control this way:

    Dial dial = new Dial(xml);
    this.Controls.Add(dial);

    the HTML rendered through Render(HtmlWriter) outputs
    OUTSIDE the <html> tag!


    <HTML>
    </HTML>
    <table id="K000004" border="1" style="border:1px solid
    #C9E7FA;">
    <!--HTML content written by the control out goes here! -->
    </table>


    Any help in this regard would be deeply appreciated.
    Please help.


    Regards,


    Vyas





    >-----Original Message-----
    >"Dune" <mleow@paradise.net.nz> wrote in message
    >news:024201c383e7$e651a420$a001280a@phx.gbl...
    >> I have a web form that has a button called "Add Blank
    Row".
    >> Every time this button is pressed a new "blank row" user
    >> control should be added to the web form. This is done
    >> dynamically in the button's event handler by calling
    >> Page.LoadControl(). Everything works ok except that the
    >> web form can't seem to remember the user controls that
    are
    >> added.
    >>
    >> The moment a postback occurs, any user controls that
    exist
    >> disappear.
    >>
    >> With some googling, I found a page which said "you must
    >> load dynamically created controls on EVERY postback".
    >>
    >> Is this true?And if so, could someone please explain why
    >> and how to work around it?
    >
    >This is true, and not just for user controls. It's true
    for all controls.
    >
    >Each request, whether an initial request (GET) or a
    postback (POST) creates
    >a new instance of your page class. This instance has no
    relationship to the
    >instance created on the previous request, nor is there a
    relationship
    >between the instance created for the initial request and
    the one created on
    >PostBack. In particular, controls you added on the
    initial request will not
    >exist in the instance created on PostBack - unless you
    create them.
    >
    >Now, if ViewState is enabled, once you create the
    controls and add them to
    >their parent's Controls collection, the controls will
    load their own
    >ViewState. However, this requires that the controls be
    created in the same
    >order each time.
    >
    >> (I assume it would require keeping track of the user
    >> controls as they are added but I can't think of a
    >> efficient way to go about doing this)
    >
    >Actually, ViewState is a good way to do this. On the
    initial request, when
    >you decide how many rows you'll have the first time
    around, set
    >ViewState["TableRows"] to the number of rows. On
    PostBack, you can read
    >ViewState["TableRows"] to find out how many rows you'll
    need to create. In
    >the Click event handler for your "New Row" button, you
    can add the new row
    >and increment ViewState["TableRows"]. On the next
    PostBack, you'll read
    >ViewState["TableRows"] to find out how many rows you'll
    need to create...
    >--
    >John Saunders
    >Internet Engineer
    >john.saunders@surfcontrol.com
    >
    >
    >.
    >
    Vyas Bharghava Guest

  6. #5

    Default Re: dynamically adding user controls

    "Vyas Bharghava" <vyas_b@yahoo.com> wrote in message
    news:19ed901c38770$9a31d5b0$a601280a@phx.gbl...
    > Hi John,
    >
    ....
    > Issue #1
    > --------
    > I'm using Page.RegisterClientScript &
    > Page.RegisterStartupScript methods inside my WebControl to
    > emit some JavaScript. They are NOT getting emitted.
    > Though if I have a custom Render(Page page) method, the
    > scripts get emitted!
    I don't know of any method with the signature "void Render(Page)". What is
    this method?

    And,in your "protected override void Render(HtmlTextWriter writer)" method,
    are you calling base.Render(writer)"?

    > Issue #2
    > --------
    > If I add control this way:
    >
    > Dial dial = new Dial(xml);
    > this.Controls.Add(dial);
    >
    > the HTML rendered through Render(HtmlWriter) outputs
    > OUTSIDE the <html> tag!
    In the above, what was "this"?
    > <HTML>
    > </HTML>
    > <table id="K000004" border="1" style="border:1px solid
    > #C9E7FA;">
    > <!--HTML content written by the control out goes here! -->
    > </table>

    There's something very strange about the way that rendering is working in
    your case. Please make sure that all of your overloads are calling their
    base class versions. If there's still a problem after that, please let us
    know more about what events your code is running in.
    --
    John Saunders
    Internet Engineer
    [email]john.saunders@surfcontrol.com[/email]


    John Saunders Guest

  7. #6

    Default Re: dynamically adding user controls

    > Issue #1
    > --------
    > I'm using Page.RegisterClientScript &
    > Page.RegisterStartupScript methods inside my WebControl to
    > emit some JavaScript. They are NOT getting emitted.
    > Though if I have a custom Render(Page page) method, the
    > scripts get emitted!
    >>I don't know of any method with the signature "void Render
    >>(Page)". What is
    >>this method?
    This is a method written by me.
    If I pass the page object from the ASPX page in the onload event:

    Dial dial = new Dial();
    dial.Render(this);

    Inside this Render I access the underlying response stream thus:

    public void Render(Page page)
    {
    page.Response.Write("Hi there!");
    }
    >>And,in your "protected override void Render
    >>(HtmlTextWriter writer)" method,
    >>are you calling base.Render(writer)"?
    I wasn't... I'll try this.

    > Issue #2
    > --------
    > If I add control this way:
    >
    > Dial dial = new Dial(xml);
    > this.Controls.Add(dial);
    >
    > the HTML rendered through Render(HtmlWriter) outputs
    > OUTSIDE the <html> tag!
    >>In the above, what was "this"?
    It's the page object. I'm adding the control to the controls collection
    of the page.

    Thanks.

    Vyas

    *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    Vyas Bharghava Guest

  8. #7

    Default Re: dynamically adding user controls

    "Vyas Bharghava" <vyas_b@yahoo.com> wrote in message
    news:%239Hjg2BiDHA.884@TK2MSFTNGP10.phx.gbl...
    > > Issue #1
    > > --------
    > > I'm using Page.RegisterClientScript &
    > > Page.RegisterStartupScript methods inside my WebControl to
    > > emit some JavaScript. They are NOT getting emitted.
    > > Though if I have a custom Render(Page page) method, the
    > > scripts get emitted!
    >
    > >>I don't know of any method with the signature "void Render
    > >>(Page)". What is
    > >>this method?
    >
    > This is a method written by me.
    > If I pass the page object from the ASPX page in the onload event:
    >
    > Dial dial = new Dial();
    > dial.Render(this);
    >
    > Inside this Render I access the underlying response stream thus:
    >
    > public void Render(Page page)
    > {
    > page.Response.Write("Hi there!");
    > }
    >
    > >>And,in your "protected override void Render
    > >>(HtmlTextWriter writer)" method,
    > >>are you calling base.Render(writer)"?
    >
    > I wasn't... I'll try this.
    >
    >
    > > Issue #2
    > > --------
    > > If I add control this way:
    > >
    > > Dial dial = new Dial(xml);
    > > this.Controls.Add(dial);
    > >
    > > the HTML rendered through Render(HtmlWriter) outputs
    > > OUTSIDE the <html> tag!
    >
    > >>In the above, what was "this"?
    >
    > It's the page object. I'm adding the control to the controls collection
    > of the page.
    This is your problem. The Page object is not the HtmlForm. If you look at
    Page.Controls, you'll find it contains an HtmlForm control. You need to add
    your controls to the HtmlForm's Controls collection.
    --
    John Saunders
    Internet Engineer
    [email]john.saunders@surfcontrol.com[/email]


    John Saunders Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139