getting values of dynamically added controls

Discussion in 'ASP .Net Web Controls' started by June Blasey, Feb 12, 2004.

  1. June Blasey

    June Blasey Guest

    I am adding controls(dropdowns and checkboxes) dynamically to a placeholder or a htmltable control on a web form. I am able to load the page successfully. However, upon selecting a button(submit) on the page I am unable to get the values entered in the controls that were added. I can not even use the findcontrol method to find the controls.
    Has anyone done this successfully? It seems to do with state?!?!?!
    PLEASE, any ideas would be GREATLY APPRECIATED!!

    Thanks,
    June

    ___
    Newsgroups brought to you courtesy of www.dotnetjohn.com
     
    June Blasey, Feb 12, 2004
    #1
    1. Advertising

  2. June Blasey

    Teemu Keiski Guest

    Hi,

    do you add the controls to the PlaceHolder on postback as well? If you
    don't, you need to do that as controls need to be recreated on postback for
    them to keep state, be searchable (in other words exist in Controls
    collection)

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist


    "June Blasey" <> wrote in message
    news:...
    I am adding controls(dropdowns and checkboxes) dynamically to a placeholder
    or a htmltable control on a web form. I am able to load the page
    successfully. However, upon selecting a button(submit) on the page I am
    unable to get the values entered in the controls that were added. I can not
    even use the findcontrol method to find the controls.
    Has anyone done this successfully? It seems to do with state?!?!?!
    PLEASE, any ideas would be GREATLY APPRECIATED!!

    Thanks,
    June

    ___
    Newsgroups brought to you courtesy of www.dotnetjohn.com
     
    Teemu Keiski, Feb 12, 2004
    #2
    1. Advertising

  3. June Blasey

    June Blasey Guest

    Hey, Thanks. I am able to now access the controls. Will I also be able to get the value of selected item(s) in a dropdown list. Assuming a dropdown list is one of the dynamically added controls?

    Thanks again,
    June
    >Hi,
    >
    > do you add the controls to the PlaceHolder on postback as well? If you
    > don't, you need to do that as controls need to be recreated on postback for
    > them to keep state, be searchable (in other words exist in Controls
    > collection)
    >
    > --
    > Teemu Keiski
    > MCP, Microsoft MVP (ASP.NET), AspInsiders member
    > ASP.NET Forum Moderator, AspAlliance Columnist
    >
    >
    > "June Blasey" <> wrote in message
    > news:...
    > I am adding controls(dropdowns and checkboxes) dynamically to a placeholder
    > or a htmltable control on a web form. I am able to load the page
    > successfully. However, upon selecting a button(submit) on the page I am
    > unable to get the values entered in the controls that were added. I can not
    > even use the findcontrol method to find the controls.
    > Has anyone done this successfully? It seems to do with state?!?!?!
    > PLEASE, any ideas would be GREATLY APPRECIATED!!
    >
    > Thanks,
    > June
    >
    > ___
    > Newsgroups brought to you courtesy of www.dotnetjohn.com
    >
    >
    >


    ___
    Newsgroups brought to you courtesy of www.dotnetjohn.com
     
    June Blasey, Feb 12, 2004
    #3
  4. June Blasey

    josema Guest

    Juney, how you solve that?, im getting the same problem since a long time...

    I create controls dinamically but when i click the button, the controls dissapear, and i can get the values...

    Thanks Juney.
    Josema
     
    josema, Feb 13, 2004
    #4
  5. June Blasey

    Teemu Keiski Guest

    Hi,

    yes you are if you just add the Control to the Controls collection before
    populating (populating happening on initial request).

    e.g:
    1. Create control instance
    2. Add to Controls collection
    3. populate inside IsPostBack check

    Another solution is to populate before adding to the Controls collection (in
    this case populating on every request). This way values are considered to be
    initial values and aren't stored to ViewState, but on postback repopulation
    happens before postback data processing, so actual selection can be kept.
    ( Control will save its state only after it is added to the Controls
    collection, therefore first to the collection and then populating)

    1. Create control instance
    2. Populate on every request
    3. Add to Controls collection

    With some controls like DataGrid etc the latter will mix up with postback
    events (databinding loses old state so events cannot be raised). With some
    controls this latter one will work, but might be easier to use the first
    one.

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist


    "June Blasey" <> wrote in message
    news:%...
    Hey, Thanks. I am able to now access the controls. Will I also be able to
    get the value of selected item(s) in a dropdown list. Assuming a dropdown
    list is one of the dynamically added controls?

    Thanks again,
    June
    >Hi,
    >
    > do you add the controls to the PlaceHolder on postback as well? If you
    > don't, you need to do that as controls need to be recreated on postback

    for
    > them to keep state, be searchable (in other words exist in Controls
    > collection)
    >
    > --
    > Teemu Keiski
    > MCP, Microsoft MVP (ASP.NET), AspInsiders member
    > ASP.NET Forum Moderator, AspAlliance Columnist
    >
    >
    > "June Blasey" <> wrote in message
    > news:...
    > I am adding controls(dropdowns and checkboxes) dynamically to a

    placeholder
    > or a htmltable control on a web form. I am able to load the page
    > successfully. However, upon selecting a button(submit) on the page I am
    > unable to get the values entered in the controls that were added. I can

    not
    > even use the findcontrol method to find the controls.
    > Has anyone done this successfully? It seems to do with state?!?!?!
    > PLEASE, any ideas would be GREATLY APPRECIATED!!
    >
    > Thanks,
    > June
    >
    > ___
    > Newsgroups brought to you courtesy of www.dotnetjohn.com
    >
    >
    >


    ___
    Newsgroups brought to you courtesy of www.dotnetjohn.com
     
    Teemu Keiski, Feb 13, 2004
    #5
  6. June Blasey

    June Blasey Guest

    Thanks Teemu - I really appreciate you assistance.
    Please forgive my ignorance. Below is a snipet of my code and how I am
    adding the controls can you help me apply what you are saying about
    creating the control, populating, then adding it??

    Thanks again,
    June
    ---------------
    'Getting page layout and controls from database

    SQL = "Select * from WebDSSSearch where SearchPage Like '" & SearchPage
    & "' order by [Rw], [Col]"
    SearchDA = New OleDb.OleDbDataAdapter(SQL, DSSConn)
    SearchDA.Fill(SearchDS, "Search")
    SearchTbl = SearchDS.Tables("Search")
    NumRows = SearchTbl.Rows.Count
    numcontrols = 0
    If NumRows > 0 Then
    SearchTable.Visible = True
    Do While (I < NumRows) And (SearchTbl.Rows(I).Item("rw")
    = PrevRow)
    Dim sCell As New TableCell
    ControlStr = Trim(SearchTbl.Rows(I).Item("Control"))

    Select Case ControlStr
    Case "ListBox"
    Dim AClistlabel As New Label
    AClistlabel.ID =
    SearchTbl.Rows(I).Item("Fieldname")
    sCell.Controls.AddAt(0, AClistlabel)

    AClistlabel.Text =
    SearchTbl.Rows(I).Item("Fieldname")
    AClistlabel.Font.Size = FontUnit.XSmall
    AClistlabel.Font.Bold = True
    AClistlabel.Font.Name = "ariel"
    AClistlabel.ForeColor = Color.Navy

    Dim listDR As OleDb.OleDbDataReader
    Query = SearchTbl.Rows(I).Item("DBMethod")


    SqlCommand2.CommandText = Query
    listDR = SqlCommand2.ExecuteReader

    Dim AClist As New ListBox
    AClist.ID =
    Trim(SearchTbl.Rows(I).Item("ControlID"))
    sCell.Controls.AddAt(1, AClist)

    AClist.SelectionMode =
    ListSelectionMode.Multiple
    AClist.Rows = 2
    AClist.DataSource = listDR
    AClist.DataValueField =
    Trim(SearchTbl.Rows(I).Item("DataField"))
    AClist.DataBind()
    listDR.Close()

    sCell.ID = "cell" & I
    numcontrols = numcontrols + 1
    End Select
    srow.Cells.Add(sCell)

    PrevRow = SearchTbl.Rows(I).Item("rw")

    Loop 'while row = prevrow
    SearchTable.Rows.Add(srow)




    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    June Blasey, Feb 17, 2004
    #6
    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. Jeffrey Todd
    Replies:
    1
    Views:
    4,152
    Peter Blum
    Jun 2, 2005
  2. =?Utf-8?B?ZG90bmV0dGVzdGVy?=

    Accessing values of dynamically added controls on Postback

    =?Utf-8?B?ZG90bmV0dGVzdGVy?=, Jul 14, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    500
    Eliyahu Goldin
    Jul 14, 2005
  3. =?Utf-8?B?ZG90bmV0dGVzdGVy?=

    Getting values from dynamically added controls

    =?Utf-8?B?ZG90bmV0dGVzdGVy?=, Dec 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    444
    Eliyahu Goldin
    Dec 5, 2005
  4. Nathan Sokalski

    Retrieving values from dynamically added controls

    Nathan Sokalski, Sep 9, 2007, in forum: ASP .Net
    Replies:
    7
    Views:
    1,262
    Scott Roberts
    Nov 20, 2007
  5. Aruna Bajpayee

    How to retrieve values of dynamically added web controls on the form??

    Aruna Bajpayee, Jul 26, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    140
Loading...

Share This Page