Creating controls dynamically based on user input

Discussion in 'ASP .Net' started by mohaaron@gmail.com, Oct 30, 2007.

  1. Guest

    I can think of a lot of reasons why this might need to be done but as
    far as I can tell it's not possible. I've been looking for a way to
    add HtmlTableRows to a table using a button click for a while and it
    seems it's not possible because the row that gets added with each
    click won't get recreated after a post back. After all the reading it
    seems that any dynamically created controls must be created in the
    Init event to be recreated after postback. This I think is very
    unfortionate as it makes it impossible to change the display
    interactively. Can someone please verify that this really is
    impossible to do?
    , Oct 30, 2007
    #1
    1. Advertising

  2. Dave Bush Guest

    OK, I'll verify that you are on the right track. You will need to
    recreate the number of rows you dynamically created during init. But,
    once you've done that view state should be able to recreate everything
    else.

    "Any dynamically created controls must be created in Init to be
    recreated after postback." Would be better stated, "Any dynamically
    created control must be recreated in Init to have its state reset after
    postback." Viewstate assumes the control object has been created, it is
    just resetting the state of that object. Not, recreating the object AND
    setting the state.

    All you really need is a way to keep track of what you created
    dynamically so that you can recreate it during init. You could do this
    with a hidden field which you could get from
    Request.Form["hiddenFieldName"] during Init.


    -----Original Message-----
    From: [mailto:]
    Posted At: Tuesday, October 30, 2007 7:44 PM
    Posted To: microsoft.public.dotnet.framework.aspnet
    Conversation: Creating controls dynamically based on user input
    Subject: Creating controls dynamically based on user input

    I can think of a lot of reasons why this might need to be done but as
    far as I can tell it's not possible. I've been looking for a way to
    add HtmlTableRows to a table using a button click for a while and it
    seems it's not possible because the row that gets added with each
    click won't get recreated after a post back. After all the reading it
    seems that any dynamically created controls must be created in the
    Init event to be recreated after postback. This I think is very
    unfortionate as it makes it impossible to change the display
    interactively. Can someone please verify that this really is
    impossible to do?
    Dave Bush, Oct 31, 2007
    #2
    1. Advertising

  3. I don't think you are on the right track. You shouldn't need to build any
    table dynamically. Instead, you should use one of the databound controls
    that produce tables, like GridView, DataList or Repeater. You need to define
    a datasource that will contain items (rows) holding data that will go to the
    resulting table rows. When you databind the control to the datasource, it
    will build the table for you automatically. The datasource doesn't have to
    be a database table. It can be any collection object, like an array etc.
    Based on user input, modify the datasource object, re-bind the control and
    you are done.

    --
    Eliyahu Goldin,
    Software Developer
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    <> wrote in message
    news:...
    >I can think of a lot of reasons why this might need to be done but as
    > far as I can tell it's not possible. I've been looking for a way to
    > add HtmlTableRows to a table using a button click for a while and it
    > seems it's not possible because the row that gets added with each
    > click won't get recreated after a post back. After all the reading it
    > seems that any dynamically created controls must be created in the
    > Init event to be recreated after postback. This I think is very
    > unfortionate as it makes it impossible to change the display
    > interactively. Can someone please verify that this really is
    > impossible to do?
    >
    Eliyahu Goldin, Oct 31, 2007
    #3
  4. Guest

    If I shouldn't need to create a table dynamically then how would you
    create a table which will have a undetermined number of rows? Meaning
    the number of rows needs to change based on what the user needs. In my
    case I have to allow the user the ability to create a new row of
    information based on a button click. I keep trying to think of ways
    around this problem and am now thinking maybe it's possible to pre-
    create my table in the Init event with more rows then will be needed,
    then set all the rows except the first one to Visible=False. Then as
    the user needs another row I will just make it visible. This might
    work. It's a hack as it will be a problem if the user needs more rows
    then were provided.

    I'm also thinking maybe I can do this using pure html and the
    Request.Form method to custom build what I need.

    There must be some way to do this.

    On Oct 31, 1:15 am, "Eliyahu Goldin"
    <> wrote:
    > I don't think you are on the right track. You shouldn't need to build any
    > table dynamically. Instead, you should use one of the databound controls
    > that produce tables, like GridView, DataList or Repeater. You need to define
    > a datasource that will contain items (rows) holding data that will go to the
    > resulting table rows. When you databind the control to the datasource, it
    > will build the table for you automatically. The datasource doesn't have to
    > be a database table. It can be any collection object, like an array etc.
    > Based on user input, modify the datasource object, re-bind the control and
    > you are done.
    >
    > --
    > Eliyahu Goldin,
    > Software Developer
    > Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net
    >
    > <> wrote in message
    >
    > news:...
    >
    >
    >
    > >I can think of a lot of reasons why this might need to be done but as
    > > far as I can tell it's not possible. I've been looking for a way to
    > > add HtmlTableRows to a table using a button click for a while and it
    > > seems it's not possible because the row that gets added with each
    > > click won't get recreated after a post back. After all the reading it
    > > seems that any dynamically created controls must be created in the
    > > Init event to be recreated after postback. This I think is very
    > > unfortionate as it makes it impossible to change the display
    > > interactively. Can someone please verify that this really is
    > > impossible to do?- Hide quoted text -

    >
    > - Show quoted text -
    , Oct 31, 2007
    #4
  5. > If I shouldn't need to create a table dynamically then how would you
    > create a table which will have a undetermined number of rows?


    The point is that you add rows to the datasource which could be as simple as
    just an array or an ArrayList. Than you do something like myGrid.DataBind()
    and you get a table with as many rows as the number of items in your
    datasource.

    --
    Eliyahu Goldin,
    Software Developer
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net
    Eliyahu Goldin, Nov 1, 2007
    #5
    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. =?Utf-8?B?TWF0dA==?=

    Creating controls dynamically based on a dynamic control

    =?Utf-8?B?TWF0dA==?=, Feb 4, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    376
    =?Utf-8?B?TWF0dA==?=
    Feb 4, 2005
  2. Bob Rock
    Replies:
    1
    Views:
    617
    Cowboy \(Gregory A. Beamer\)
    Sep 17, 2006
  3. Piyush
    Replies:
    0
    Views:
    99
    Piyush
    Sep 28, 2004
  4. Andrew Crossley

    Creating User Controls Dynamically - Object Reference Not Set erro

    Andrew Crossley, Nov 7, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    152
    Andrew Crossley
    Nov 7, 2005
  5. Replies:
    2
    Views:
    94
    sunilp
    Jan 5, 2006
Loading...

Share This Page