How to dynamically create a control in a user control?

Discussion in 'ASP .Net' started by Quentin Huo, Sep 8, 2004.

  1. Quentin Huo

    Quentin Huo Guest

    Hi:

    I have a user control and I want to dynamically create a Label control and
    TextBox control when the button "add more author name" is clicked.

    I did like this:

    1. there is a "PlaceHolder" control "ph" in the user control;

    2. create a Label control and TextBox control when in the "add" button
    event, like:
    Label lblAuthor = new Label();
    lblAuthor.Text="Author";


    TextBox tbAuthor = new TextBox();

    tbAuthor.ID="...";



    3. add the controls to the PlaceHolder control "ph":

    ph.Controls.Add(lblAuthor);
    ph.Controls.Add(tbAuthor);


    But it doesn't work, the value that is typed into the new-created TextBox
    control cannot be kept by form(the form tag is in the parent page that
    includes the user control). When click another button like "save", the value
    will be losted. Where is the problem? How to fix it?

    Thanks

    Q.
     
    Quentin Huo, Sep 8, 2004
    #1
    1. Advertising

  2. you need to recreate control hierarchy on each postback before ViewState is
    retained for dynamically added controls

    HTH

    Konrad

    "Quentin Huo" <> wrote in message
    news:...
    > Hi:
    >
    > I have a user control and I want to dynamically create a Label control and
    > TextBox control when the button "add more author name" is clicked.
    >
    > I did like this:
    >
    > 1. there is a "PlaceHolder" control "ph" in the user control;
    >
    > 2. create a Label control and TextBox control when in the "add" button
    > event, like:
    > Label lblAuthor = new Label();
    > lblAuthor.Text="Author";
    >
    >
    > TextBox tbAuthor = new TextBox();
    >
    > tbAuthor.ID="...";
    >
    >
    >
    > 3. add the controls to the PlaceHolder control "ph":
    >
    > ph.Controls.Add(lblAuthor);
    > ph.Controls.Add(tbAuthor);
    >
    >
    > But it doesn't work, the value that is typed into the new-created TextBox
    > control cannot be kept by form(the form tag is in the parent page that
    > includes the user control). When click another button like "save", the

    value
    > will be losted. Where is the problem? How to fix it?
    >
    > Thanks
    >
    > Q.
    >
    >
     
    Konrad Rotuski, Sep 8, 2004
    #2
    1. Advertising

  3. Quentin,

    The reason it doesn't work is that the server-side event that adds the
    new controls to the place holder gets fired late in the event chain.
    There is nothing you can do about that. I don't think it would help
    for me to go deep into this, so I will just give you an alternative
    solution. I suggest that you use a datagrid rather than a place
    holder. It is fairly easy to add new rows to the datagrid rather than
    have to overcome the problem you have with the place holder. Let me
    point you to a few articles that discuss dynamically adding rows to a
    datagrid. The point to remember with adding new rows to a datagrid is
    that you add a new row to the data source of the grid and then rebind
    and that takes care of it.

    http://www.dotnetjunkies.com/Tutorial/C0DD0D9A-8907-4117-BBF2-4C407AA83574.dcik
    http://www.123aspx.com/ResPrint.aspx?res=359



    sayed

    "Quentin Huo" <> wrote in message news:<>...
    > Hi:
    >
    > I have a user control and I want to dynamically create a Label control and
    > TextBox control when the button "add more author name" is clicked.
    >
    > I did like this:
    >
    > 1. there is a "PlaceHolder" control "ph" in the user control;
    >
    > 2. create a Label control and TextBox control when in the "add" button
    > event, like:
    > Label lblAuthor = new Label();
    > lblAuthor.Text="Author";
    >
    >
    > TextBox tbAuthor = new TextBox();
    >
    > tbAuthor.ID="...";
    >
    >
    >
    > 3. add the controls to the PlaceHolder control "ph":
    >
    > ph.Controls.Add(lblAuthor);
    > ph.Controls.Add(tbAuthor);
    >
    >
    > But it doesn't work, the value that is typed into the new-created TextBox
    > control cannot be kept by form(the form tag is in the parent page that
    > includes the user control). When click another button like "save", the value
    > will be losted. Where is the problem? How to fix it?
    >
    > Thanks
    >
    > Q.
     
    Sayed Hashimi, Sep 8, 2004
    #3
    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. David J. Berman

    How do I dynamically create user controls?

    David J. Berman, Jun 25, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    4,413
    David J. Berman
    Jun 30, 2003
  2. =?Utf-8?B?UmV6YSBOYWJp?=

    Load User Control Dynamically, Cast object dynamically

    =?Utf-8?B?UmV6YSBOYWJp?=, Mar 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    6,733
    Matt Berther
    Mar 5, 2005
  3. =?Utf-8?B?UmV6YSBOYWJp?=

    Load User Control Dynamically, Cast object dynamically

    =?Utf-8?B?UmV6YSBOYWJp?=, Mar 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    6,365
    Scott Allen
    Mar 4, 2005
  4. Travis Pruitt

    Dynamically Adding User Control with Child User Control

    Travis Pruitt, Jan 13, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    189
    Travis Pruitt
    Jan 13, 2005
  5. David Lozzi

    Create User Controls dynamically

    David Lozzi, Oct 13, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    149
    Steven Cheng[MSFT]
    Oct 19, 2005
Loading...

Share This Page