Addding usercontrol dynmaically with postback capability

Discussion in 'ASP .Net' started by news.microsoft.com, Apr 26, 2010.

  1. Hello,

    I am trying to add an asp.net user control dynamically with the postback
    capability.

    Adding a user control seems straightfroward but making postback work is a
    problem. When I say postback, I mean postback handled inside the
    usercontrol.
    e.g. let's say your user control has a text and a button. You save the text
    property in viewstate varible so that it is preserved during postback. You
    assign the new property value from textbox to viewstate variable after
    postback.

    Problem comes when controls are created dynamically.

    Any help will be appreciated to make this work.

    regards,
    Pravin
     
    news.microsoft.com, Apr 26, 2010
    #1
    1. Advertising

  2. What event do you dynamically add the user control? The main issue, the user
    control must maintain the same id from postback to postback. If you add it
    too late, it actually gets added after the postback event so none of the
    click event gets fired. You have to add the control early. What I usually do
    is add the control in the OnLoad or OnPreLoad event. Then if something
    changes where I have to swap the control, I do it in the OnPreRender event
    after wiping out the previously added control.

    Another key issue, the id. Controlling the ID of the dynamically added
    control will help you maintain consistancy in the postback events.

    Hope this helps,
    Mark Fitzpatrick

    "news.microsoft.com" <> wrote in message
    news:...
    > Hello,
    >
    > I am trying to add an asp.net user control dynamically with the postback
    > capability.
    >
    > Adding a user control seems straightfroward but making postback work is a
    > problem. When I say postback, I mean postback handled inside the
    > usercontrol.
    > e.g. let's say your user control has a text and a button. You save the
    > text property in viewstate varible so that it is preserved during
    > postback. You assign the new property value from textbox to viewstate
    > variable after postback.
    >
    > Problem comes when controls are created dynamically.
    >
    > Any help will be appreciated to make this work.
    >
    > regards,
    > Pravin
    >
     
    Mark Fitzpatrick, Apr 26, 2010
    #2
    1. Advertising

  3. news.microsoft.com

    Cubaman Guest

    On Apr 26, 5:52 pm, "news.microsoft.com" <> wrote:
    > Hello,
    >
    > I am trying to add an asp.net user control dynamically with the postback
    > capability.
    >

    Take a look at this user control, it might help you:
    http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

    Best regards

    > Adding a user control seems straightfroward but making postback work is a
    > problem. When I say postback, I mean postback handled inside the
    > usercontrol.
    > e.g. let's say your user control has a text and a button. You save the text
    > property in viewstate varible so that it is preserved during postback.  You
    > assign the new property value from textbox to viewstate variable after
    > postback.
    >
    > Problem comes when controls are created dynamically.
    >
    > Any help will be appreciated to make this work.
    >
    > regards,
    > Pravin
     
    Cubaman, Apr 27, 2010
    #3
  4. "news.microsoft.com" <> wrote in message
    news:...
    > Hello,
    >
    > I am trying to add an asp.net user control dynamically with the postback
    > capability.
    >
    > Adding a user control seems straightfroward but making postback work is a
    > problem. When I say postback, I mean postback handled inside the
    > usercontrol.
    > e.g. let's say your user control has a text and a button. You save the
    > text property in viewstate varible so that it is preserved during
    > postback. You assign the new property value from textbox to viewstate
    > variable after postback.
    >
    > Problem comes when controls are created dynamically.
    >
    > Any help will be appreciated to make this work.


    Mark has hit this on the head. In order to work with viewstate, you have to
    have the control on the page prior to when viewstate is applied back, which
    means before the Load event. Take a look at the page lifecycle events and
    you will understand why adding the control on load completely messes up your
    system.

    --
    Peace and Grace,
    Greg

    Twitter: @gbworld
    Blog: http://gregorybeamer.spaces.live.com

    ************************************************
    | Think outside the box! |
    ************************************************
     
    Gregory A. Beamer, Apr 28, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Novice
    Replies:
    5
    Views:
    511
  2. Eirik Eldorsen
    Replies:
    5
    Views:
    435
    Eirik Eldorsen
    Nov 15, 2004
  3. Gregory

    <DIV> addding text from code behind.

    Gregory, Oct 10, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    15,751
    Gregory
    Oct 10, 2005
  4. Sal D'Ambra

    Help With Addding Attribute to Button In Data Repeater

    Sal D'Ambra, Jan 1, 2004, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    333
    Alessandro Zifiglio
    Jan 2, 2004
  5. Mark van Bree

    Postback problem with usercontrol in usercontrol

    Mark van Bree, May 30, 2006, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    165
Loading...

Share This Page