radiobuttonlist get selected item

Discussion in 'ASP .Net' started by =?Utf-8?B?anVzdHN0YXJ0ZXI=?=, Oct 25, 2005.

  1. Hello,
    I have an aspx file where i've put a placeholder element. On load
    (page_load) i create dynamically an html table which contains a checkbox and
    a radiobuttonlist in each tablerow . The radiobuttonlist contains two items
    (yes,no). Both the
    checkboxes and the radiobuttonlist are NOT autopostbacked ( .autopostback =
    false). When i press the submit button a sub (submit_pressed) is run. My
    problem is that i can not get the selected items in the radiobuttonlists.This
    is caused cos in the page_load event i call the sub that dynamically
    recreates the table described above and so all values are reset (there is a
    rdb.item(0).selected=true line in my
    code. If i try to remove that line of code, the radiobuttons keep their
    values or not,after each submit, without a pattern (at least not one that i
    can see)!). I've also tried to keep the values in a collection, instasiated
    in the page_load but i couldn't do so cos the table (that contains the
    checkboxes and radiobuttonlists) is not recognized at the beggining of the
    page_load (i guess it's not an object yet cos the sub that creates all that
    is at the end of page_load)....
    Anyway...any ideas on how to get the selected item ? ? ? ?
    thx a lot
    theodore

    here is a portion of the code
    in vb code
    --------------------
    1. create the (html) table through code

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs)
    Dim ID as integer = Request("ID")
    call addControlsInPlaceholder()
    End Sub


    sub addControlsInPlaceholder
    'create the table
    dim oTable as new table
    oTable.id="oTable"
    oTable.GridLines=GridLines.None


    'first add the titles of each column
    dim lb1 as new label
    dim lb2 as new label

    dim cell1 as new TableHeaderCell
    dim cell2 as new TableHeaderCell

    cell1.width=unit.pixel(180)
    cell2.width=unit.pixel(120)

    dim row1 as new tablerow

    lb1.text= Category"
    lb2.text= "isDisplayed"

    'add labels to cells
    Cell1.controls.add(lb1)
    Cell2.controls.add(lb2)

    'add (header) cells to row
    Row1.cells.add(Cell1)
    Row1.cells.add(Cell2)
    'add header row to table
    otable.rows.add(Row1)

    'fill the chkboxlist/radiobuttonlist(s) with items
    Dim id As String
    Dim subcListItem,subcListItem1,subcListItem2 As ListItem

    Dim myConnection As New OdbcConnection(Application("strConnect"))
    dim strSQL as string

    strSQL = " SELECT ctg_id, ctg_name FROM categories ORDER BY ctg_id"
    Dim myCommand As New OdbcCommand(strSQL, myConnection)
    myConnection.Open()
    Dim myReader As OdbcDataReader = myCommand.ExecuteReader()

    while myReader.Read()
    'create new Row for the table
    dim oRow as new tableRow
    'create 2 cells for the row
    dim oCell1 as new tableCell
    dim oCell2 as new tableCell


    oCell1.width=unit.pixel(180)
    oCell2.width=unit.pixel(120)

    'create checkbox and add it cell1 (the first cell of each row)
    dim chkboxlist as new checkbox
    chkboxlist.id = "categories_" & myReader.Item("ctg_id").ToString
    chkboxlist.text=myReader.Item("ctg_name").ToString


    oCell1.controls.add(chkboxlist)

    'create radiobuttonlist and add it cell2 (the second cell of each row)
    dim rdbDisplayed as new radiobuttonlist
    rdbDisplayed.RepeatDirection=repeatdirection.Horizontal
    rdbDisplayed.id="cs_isDisplayed_" & myReader.Item("ctg_id").ToString

    'add items in radiobuttonlists
    subcListItem1 = New ListItem
    subcListItem1.Text = "yes"
    subcListItem1.Value = "1"
    rdbDisplayed.Items.Add(subcListItem1)

    subcListItem2 = New ListItem
    subcListItem2.Text = "no"
    subcListItem2.Value = "0"
    rdbDisplayed.Items.Add(subcListItem2)
    rdbDisplayed.items(0).selected=true

    oCell2.controls.add(rdbDisplayed)


    'add cells to row
    oRow.cells.add(oCell1)
    oRow.cells.add(oCell2)

    'add row to table
    otable.rows.add(oRow)
    'increase counter i, to create new row
    i = i +1

    end while
    'add table to placeholder
    plh1.controls.add(oTable)

    myCommand.dispose
    myConnection.close

    end sub


    3. when the submit button is pressed ,i try something like

    private sub submit_pressed

    dim otable as new table
    dim tr as new tablerow
    dim tc as new tablecell
    dim chk as checkbox
    dim i,displayed as int16
    dim rdbDisplayed,rdbAdult as radiobuttonlist

    'plh1 is the placeholder in the html code
    otable = ctype((plh1.controls(0)),table)

    for i = 1 to otable.rows.count-1

    tr = otable.controls(i)
    tc = tr.controls(0)
    'tc.control(0) is a checkbox
    chk = tc.controls(0)

    'table cell
    tc = tr.controls(1)
    'tc.ctontrol(0) is a radiobuttonlist
    rdbDisplayed = tc.controls(0)

    '****************************
    'the following line always returns item(0) cos the sub
    addControlsInPlaceholder has already run (it's called in the page_load event)
    before the submit_pressed is called
    displayed = rdbDisplayed.selecteditem.value
    '*****************************

    if chk.checked = true then
    'show displayed
    end if

    next

    end sub
     
    =?Utf-8?B?anVzdHN0YXJ0ZXI=?=, Oct 25, 2005
    #1
    1. Advertising

  2. Remove the bind from your Page_Load and put into its own routine. Only call
    from Page_Load if it is not a Postback. If you need to rebind, do it from
    your button click after you retrieve values. Ordering of events is fixed, so
    you have to be flexible.

    --
    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA

    ***************************
    Think Outside the Box!
    ***************************


    "juststarter" wrote:

    > Hello,
    > I have an aspx file where i've put a placeholder element. On load
    > (page_load) i create dynamically an html table which contains a checkbox and
    > a radiobuttonlist in each tablerow . The radiobuttonlist contains two items
    > (yes,no). Both the
    > checkboxes and the radiobuttonlist are NOT autopostbacked ( .autopostback =
    > false). When i press the submit button a sub (submit_pressed) is run. My
    > problem is that i can not get the selected items in the radiobuttonlists.This
    > is caused cos in the page_load event i call the sub that dynamically
    > recreates the table described above and so all values are reset (there is a
    > rdb.item(0).selected=true line in my
    > code. If i try to remove that line of code, the radiobuttons keep their
    > values or not,after each submit, without a pattern (at least not one that i
    > can see)!). I've also tried to keep the values in a collection, instasiated
    > in the page_load but i couldn't do so cos the table (that contains the
    > checkboxes and radiobuttonlists) is not recognized at the beggining of the
    > page_load (i guess it's not an object yet cos the sub that creates all that
    > is at the end of page_load)....
    > Anyway...any ideas on how to get the selected item ? ? ? ?
    > thx a lot
    > theodore
    >
    > here is a portion of the code
    > in vb code
    > --------------------
    > 1. create the (html) table through code
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs)
    > Dim ID as integer = Request("ID")
    > call addControlsInPlaceholder()
    > End Sub
    >
    >
    > sub addControlsInPlaceholder
    > 'create the table
    > dim oTable as new table
    > oTable.id="oTable"
    > oTable.GridLines=GridLines.None
    >
    >
    > 'first add the titles of each column
    > dim lb1 as new label
    > dim lb2 as new label
    >
    > dim cell1 as new TableHeaderCell
    > dim cell2 as new TableHeaderCell
    >
    > cell1.width=unit.pixel(180)
    > cell2.width=unit.pixel(120)
    >
    > dim row1 as new tablerow
    >
    > lb1.text= Category"
    > lb2.text= "isDisplayed"
    >
    > 'add labels to cells
    > Cell1.controls.add(lb1)
    > Cell2.controls.add(lb2)
    >
    > 'add (header) cells to row
    > Row1.cells.add(Cell1)
    > Row1.cells.add(Cell2)
    > 'add header row to table
    > otable.rows.add(Row1)
    >
    > 'fill the chkboxlist/radiobuttonlist(s) with items
    > Dim id As String
    > Dim subcListItem,subcListItem1,subcListItem2 As ListItem
    >
    > Dim myConnection As New OdbcConnection(Application("strConnect"))
    > dim strSQL as string
    >
    > strSQL = " SELECT ctg_id, ctg_name FROM categories ORDER BY ctg_id"
    > Dim myCommand As New OdbcCommand(strSQL, myConnection)
    > myConnection.Open()
    > Dim myReader As OdbcDataReader = myCommand.ExecuteReader()
    >
    > while myReader.Read()
    > 'create new Row for the table
    > dim oRow as new tableRow
    > 'create 2 cells for the row
    > dim oCell1 as new tableCell
    > dim oCell2 as new tableCell
    >
    >
    > oCell1.width=unit.pixel(180)
    > oCell2.width=unit.pixel(120)
    >
    > 'create checkbox and add it cell1 (the first cell of each row)
    > dim chkboxlist as new checkbox
    > chkboxlist.id = "categories_" & myReader.Item("ctg_id").ToString
    > chkboxlist.text=myReader.Item("ctg_name").ToString
    >
    >
    > oCell1.controls.add(chkboxlist)
    >
    > 'create radiobuttonlist and add it cell2 (the second cell of each row)
    > dim rdbDisplayed as new radiobuttonlist
    > rdbDisplayed.RepeatDirection=repeatdirection.Horizontal
    > rdbDisplayed.id="cs_isDisplayed_" & myReader.Item("ctg_id").ToString
    >
    > 'add items in radiobuttonlists
    > subcListItem1 = New ListItem
    > subcListItem1.Text = "yes"
    > subcListItem1.Value = "1"
    > rdbDisplayed.Items.Add(subcListItem1)
    >
    > subcListItem2 = New ListItem
    > subcListItem2.Text = "no"
    > subcListItem2.Value = "0"
    > rdbDisplayed.Items.Add(subcListItem2)
    > rdbDisplayed.items(0).selected=true
    >
    > oCell2.controls.add(rdbDisplayed)
    >
    >
    > 'add cells to row
    > oRow.cells.add(oCell1)
    > oRow.cells.add(oCell2)
    >
    > 'add row to table
    > otable.rows.add(oRow)
    > 'increase counter i, to create new row
    > i = i +1
    >
    > end while
    > 'add table to placeholder
    > plh1.controls.add(oTable)
    >
    > myCommand.dispose
    > myConnection.close
    >
    > end sub
    >
    >
    > 3. when the submit button is pressed ,i try something like
    >
    > private sub submit_pressed
    >
    > dim otable as new table
    > dim tr as new tablerow
    > dim tc as new tablecell
    > dim chk as checkbox
    > dim i,displayed as int16
    > dim rdbDisplayed,rdbAdult as radiobuttonlist
    >
    > 'plh1 is the placeholder in the html code
    > otable = ctype((plh1.controls(0)),table)
    >
    > for i = 1 to otable.rows.count-1
    >
    > tr = otable.controls(i)
    > tc = tr.controls(0)
    > 'tc.control(0) is a checkbox
    > chk = tc.controls(0)
    >
    > 'table cell
    > tc = tr.controls(1)
    > 'tc.ctontrol(0) is a radiobuttonlist
    > rdbDisplayed = tc.controls(0)
    >
    > '****************************
    > 'the following line always returns item(0) cos the sub
    > addControlsInPlaceholder has already run (it's called in the page_load event)
    > before the submit_pressed is called
    > displayed = rdbDisplayed.selecteditem.value
    > '*****************************
    >
    > if chk.checked = true then
    > 'show displayed
    > end if
    >
    > next
    >
    > end sub
     
    =?Utf-8?B?Q293Ym95IChHcmVnb3J5IEEuIEJlYW1lcikgLSBN, Oct 25, 2005
    #2
    1. Advertising

  3. juststarter,

    I have some example code on using dynamic controls on my website:
    http://www.aboutfortunate.com?page=codelibrary if you go to the given page
    and use the search box there to search for "dynamic" you'll get all the
    examples in the search results. There are only about 3 or 4 and I think that
    it would benefit you to take a look at them.

    --
    Sincerely,

    S. Justin Gengo, MCP
    Web Developer / Programmer

    www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzsche
    "juststarter" <> wrote in message
    news:...
    > Hello,
    > I have an aspx file where i've put a placeholder element. On load
    > (page_load) i create dynamically an html table which contains a checkbox
    > and
    > a radiobuttonlist in each tablerow . The radiobuttonlist contains two
    > items
    > (yes,no). Both the
    > checkboxes and the radiobuttonlist are NOT autopostbacked ( .autopostback
    > =
    > false). When i press the submit button a sub (submit_pressed) is run. My
    > problem is that i can not get the selected items in the
    > radiobuttonlists.This
    > is caused cos in the page_load event i call the sub that dynamically
    > recreates the table described above and so all values are reset (there is
    > a
    > rdb.item(0).selected=true line in my
    > code. If i try to remove that line of code, the radiobuttons keep their
    > values or not,after each submit, without a pattern (at least not one that
    > i
    > can see)!). I've also tried to keep the values in a collection,
    > instasiated
    > in the page_load but i couldn't do so cos the table (that contains the
    > checkboxes and radiobuttonlists) is not recognized at the beggining of the
    > page_load (i guess it's not an object yet cos the sub that creates all
    > that
    > is at the end of page_load)....
    > Anyway...any ideas on how to get the selected item ? ? ? ?
    > thx a lot
    > theodore
    >
    > here is a portion of the code
    > in vb code
    > --------------------
    > 1. create the (html) table through code
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs)
    > Dim ID as integer = Request("ID")
    > call addControlsInPlaceholder()
    > End Sub
    >
    >
    > sub addControlsInPlaceholder
    > 'create the table
    > dim oTable as new table
    > oTable.id="oTable"
    > oTable.GridLines=GridLines.None
    >
    >
    > 'first add the titles of each column
    > dim lb1 as new label
    > dim lb2 as new label
    >
    > dim cell1 as new TableHeaderCell
    > dim cell2 as new TableHeaderCell
    >
    > cell1.width=unit.pixel(180)
    > cell2.width=unit.pixel(120)
    >
    > dim row1 as new tablerow
    >
    > lb1.text= Category"
    > lb2.text= "isDisplayed"
    >
    > 'add labels to cells
    > Cell1.controls.add(lb1)
    > Cell2.controls.add(lb2)
    >
    > 'add (header) cells to row
    > Row1.cells.add(Cell1)
    > Row1.cells.add(Cell2)
    > 'add header row to table
    > otable.rows.add(Row1)
    >
    > 'fill the chkboxlist/radiobuttonlist(s) with items
    > Dim id As String
    > Dim subcListItem,subcListItem1,subcListItem2 As ListItem
    >
    > Dim myConnection As New OdbcConnection(Application("strConnect"))
    > dim strSQL as string
    >
    > strSQL = " SELECT ctg_id, ctg_name FROM categories ORDER BY ctg_id"
    > Dim myCommand As New OdbcCommand(strSQL, myConnection)
    > myConnection.Open()
    > Dim myReader As OdbcDataReader = myCommand.ExecuteReader()
    >
    > while myReader.Read()
    > 'create new Row for the table
    > dim oRow as new tableRow
    > 'create 2 cells for the row
    > dim oCell1 as new tableCell
    > dim oCell2 as new tableCell
    >
    >
    > oCell1.width=unit.pixel(180)
    > oCell2.width=unit.pixel(120)
    >
    > 'create checkbox and add it cell1 (the first cell of each row)
    > dim chkboxlist as new checkbox
    > chkboxlist.id = "categories_" & myReader.Item("ctg_id").ToString
    > chkboxlist.text=myReader.Item("ctg_name").ToString
    >
    >
    > oCell1.controls.add(chkboxlist)
    >
    > 'create radiobuttonlist and add it cell2 (the second cell of each row)
    > dim rdbDisplayed as new radiobuttonlist
    > rdbDisplayed.RepeatDirection=repeatdirection.Horizontal
    > rdbDisplayed.id="cs_isDisplayed_" & myReader.Item("ctg_id").ToString
    >
    > 'add items in radiobuttonlists
    > subcListItem1 = New ListItem
    > subcListItem1.Text = "yes"
    > subcListItem1.Value = "1"
    > rdbDisplayed.Items.Add(subcListItem1)
    >
    > subcListItem2 = New ListItem
    > subcListItem2.Text = "no"
    > subcListItem2.Value = "0"
    > rdbDisplayed.Items.Add(subcListItem2)
    > rdbDisplayed.items(0).selected=true
    >
    > oCell2.controls.add(rdbDisplayed)
    >
    >
    > 'add cells to row
    > oRow.cells.add(oCell1)
    > oRow.cells.add(oCell2)
    >
    > 'add row to table
    > otable.rows.add(oRow)
    > 'increase counter i, to create new row
    > i = i +1
    >
    > end while
    > 'add table to placeholder
    > plh1.controls.add(oTable)
    >
    > myCommand.dispose
    > myConnection.close
    >
    > end sub
    >
    >
    > 3. when the submit button is pressed ,i try something like
    >
    > private sub submit_pressed
    >
    > dim otable as new table
    > dim tr as new tablerow
    > dim tc as new tablecell
    > dim chk as checkbox
    > dim i,displayed as int16
    > dim rdbDisplayed,rdbAdult as radiobuttonlist
    >
    > 'plh1 is the placeholder in the html code
    > otable = ctype((plh1.controls(0)),table)
    >
    > for i = 1 to otable.rows.count-1
    >
    > tr = otable.controls(i)
    > tc = tr.controls(0)
    > 'tc.control(0) is a checkbox
    > chk = tc.controls(0)
    >
    > 'table cell
    > tc = tr.controls(1)
    > 'tc.ctontrol(0) is a radiobuttonlist
    > rdbDisplayed = tc.controls(0)
    >
    > '****************************
    > 'the following line always returns item(0) cos the sub
    > addControlsInPlaceholder has already run (it's called in the page_load
    > event)
    > before the submit_pressed is called
    > displayed = rdbDisplayed.selecteditem.value
    > '*****************************
    >
    > if chk.checked = true then
    > 'show displayed
    > end if
    >
    > next
    >
    > end sub
     
    S. Justin Gengo, Oct 25, 2005
    #3
  4. Thx both for your help,
    what i did is the following :
    in page_load event i added:

    if not page.ispostback then
    call addControlsInPlaceholder()
    end if

    in the submit_pressed sub i added the following line in the end of the sub
    (so that i recreate the html table after i save the values of the
    radiobuttonlist :
    call addControlsInPlaceholder()

    the problem is that the line (in the submit_pressed sub) :
    otable = ctype((plh1.controls(0)),table)
    is returning an error because plh1 doesn't have any controls any more.

    Doesn't the placeholder retain the controls added to it, after the postback
    is done, or am i missing something?

    theodore



    "Cowboy (Gregory A. Beamer) - MVP" wrote:

    > Remove the bind from your Page_Load and put into its own routine. Only call
    > from Page_Load if it is not a Postback. If you need to rebind, do it from
    > your button click after you retrieve values. Ordering of events is fixed, so
    > you have to be flexible.
    >
    > --
    > Gregory A. Beamer
    > MVP; MCP: +I, SE, SD, DBA
    >
    > ***************************
    > Think Outside the Box!
    > ***************************
    >
    >
    > "juststarter" wrote:
    >
    > > Hello,
    > > I have an aspx file where i've put a placeholder element. On load
    > > (page_load) i create dynamically an html table which contains a checkbox and
    > > a radiobuttonlist in each tablerow . The radiobuttonlist contains two items
    > > (yes,no). Both the
    > > checkboxes and the radiobuttonlist are NOT autopostbacked ( .autopostback =
    > > false). When i press the submit button a sub (submit_pressed) is run. My
    > > problem is that i can not get the selected items in the radiobuttonlists.This
    > > is caused cos in the page_load event i call the sub that dynamically
    > > recreates the table described above and so all values are reset (there is a
    > > rdb.item(0).selected=true line in my
    > > code. If i try to remove that line of code, the radiobuttons keep their
    > > values or not,after each submit, without a pattern (at least not one that i
    > > can see)!). I've also tried to keep the values in a collection, instasiated
    > > in the page_load but i couldn't do so cos the table (that contains the
    > > checkboxes and radiobuttonlists) is not recognized at the beggining of the
    > > page_load (i guess it's not an object yet cos the sub that creates all that
    > > is at the end of page_load)....
    > > Anyway...any ideas on how to get the selected item ? ? ? ?
    > > thx a lot
    > > theodore
    > >
    > > here is a portion of the code
    > > in vb code
    > > --------------------
    > > 1. create the (html) table through code
    > >
    > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > > System.EventArgs)
    > > Dim ID as integer = Request("ID")
    > > call addControlsInPlaceholder()
    > > End Sub
    > >
    > >
    > > sub addControlsInPlaceholder
    > > 'create the table
    > > dim oTable as new table
    > > oTable.id="oTable"
    > > oTable.GridLines=GridLines.None
    > >
    > >
    > > 'first add the titles of each column
    > > dim lb1 as new label
    > > dim lb2 as new label
    > >
    > > dim cell1 as new TableHeaderCell
    > > dim cell2 as new TableHeaderCell
    > >
    > > cell1.width=unit.pixel(180)
    > > cell2.width=unit.pixel(120)
    > >
    > > dim row1 as new tablerow
    > >
    > > lb1.text= Category"
    > > lb2.text= "isDisplayed"
    > >
    > > 'add labels to cells
    > > Cell1.controls.add(lb1)
    > > Cell2.controls.add(lb2)
    > >
    > > 'add (header) cells to row
    > > Row1.cells.add(Cell1)
    > > Row1.cells.add(Cell2)
    > > 'add header row to table
    > > otable.rows.add(Row1)
    > >
    > > 'fill the chkboxlist/radiobuttonlist(s) with items
    > > Dim id As String
    > > Dim subcListItem,subcListItem1,subcListItem2 As ListItem
    > >
    > > Dim myConnection As New OdbcConnection(Application("strConnect"))
    > > dim strSQL as string
    > >
    > > strSQL = " SELECT ctg_id, ctg_name FROM categories ORDER BY ctg_id"
    > > Dim myCommand As New OdbcCommand(strSQL, myConnection)
    > > myConnection.Open()
    > > Dim myReader As OdbcDataReader = myCommand.ExecuteReader()
    > >
    > > while myReader.Read()
    > > 'create new Row for the table
    > > dim oRow as new tableRow
    > > 'create 2 cells for the row
    > > dim oCell1 as new tableCell
    > > dim oCell2 as new tableCell
    > >
    > >
    > > oCell1.width=unit.pixel(180)
    > > oCell2.width=unit.pixel(120)
    > >
    > > 'create checkbox and add it cell1 (the first cell of each row)
    > > dim chkboxlist as new checkbox
    > > chkboxlist.id = "categories_" & myReader.Item("ctg_id").ToString
    > > chkboxlist.text=myReader.Item("ctg_name").ToString
    > >
    > >
    > > oCell1.controls.add(chkboxlist)
    > >
    > > 'create radiobuttonlist and add it cell2 (the second cell of each row)
    > > dim rdbDisplayed as new radiobuttonlist
    > > rdbDisplayed.RepeatDirection=repeatdirection.Horizontal
    > > rdbDisplayed.id="cs_isDisplayed_" & myReader.Item("ctg_id").ToString
    > >
    > > 'add items in radiobuttonlists
    > > subcListItem1 = New ListItem
    > > subcListItem1.Text = "yes"
    > > subcListItem1.Value = "1"
    > > rdbDisplayed.Items.Add(subcListItem1)
    > >
    > > subcListItem2 = New ListItem
    > > subcListItem2.Text = "no"
    > > subcListItem2.Value = "0"
    > > rdbDisplayed.Items.Add(subcListItem2)
    > > rdbDisplayed.items(0).selected=true
    > >
    > > oCell2.controls.add(rdbDisplayed)
    > >
    > >
    > > 'add cells to row
    > > oRow.cells.add(oCell1)
    > > oRow.cells.add(oCell2)
    > >
    > > 'add row to table
    > > otable.rows.add(oRow)
    > > 'increase counter i, to create new row
    > > i = i +1
    > >
    > > end while
    > > 'add table to placeholder
    > > plh1.controls.add(oTable)
    > >
    > > myCommand.dispose
    > > myConnection.close
    > >
    > > end sub
    > >
    > >
    > > 3. when the submit button is pressed ,i try something like
    > >
    > > private sub submit_pressed
    > >
    > > dim otable as new table
    > > dim tr as new tablerow
    > > dim tc as new tablecell
    > > dim chk as checkbox
    > > dim i,displayed as int16
    > > dim rdbDisplayed,rdbAdult as radiobuttonlist
    > >
    > > 'plh1 is the placeholder in the html code
    > > otable = ctype((plh1.controls(0)),table)
    > >
    > > for i = 1 to otable.rows.count-1
    > >
    > > tr = otable.controls(i)
    > > tc = tr.controls(0)
    > > 'tc.control(0) is a checkbox
    > > chk = tc.controls(0)
    > >
    > > 'table cell
    > > tc = tr.controls(1)
    > > 'tc.ctontrol(0) is a radiobuttonlist
    > > rdbDisplayed = tc.controls(0)
    > >
    > > '****************************
    > > 'the following line always returns item(0) cos the sub
    > > addControlsInPlaceholder has already run (it's called in the page_load event)
    > > before the submit_pressed is called
    > > displayed = rdbDisplayed.selecteditem.value
    > > '*****************************
    > >
    > > if chk.checked = true then
    > > 'show displayed
    > > end if
    > >
    > > next
    > >
    > > end sub
     
    =?Utf-8?B?anVzdHN0YXJ0ZXI=?=, Oct 26, 2005
    #4
  5. juststarter,

    No you have to recreate any controls added dynamically to a page upon each
    page load.

    --
    Sincerely,

    S. Justin Gengo, MCP
    Web Developer / Programmer

    www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzsche
    "juststarter" <> wrote in message
    news:...
    > Thx both for your help,
    > what i did is the following :
    > in page_load event i added:
    >
    > if not page.ispostback then
    > call addControlsInPlaceholder()
    > end if
    >
    > in the submit_pressed sub i added the following line in the end of the sub
    > (so that i recreate the html table after i save the values of the
    > radiobuttonlist :
    > call addControlsInPlaceholder()
    >
    > the problem is that the line (in the submit_pressed sub) :
    > otable = ctype((plh1.controls(0)),table)
    > is returning an error because plh1 doesn't have any controls any more.
    >
    > Doesn't the placeholder retain the controls added to it, after the
    > postback
    > is done, or am i missing something?
    >
    > theodore
    >
    >
    >
    > "Cowboy (Gregory A. Beamer) - MVP" wrote:
    >
    >> Remove the bind from your Page_Load and put into its own routine. Only
    >> call
    >> from Page_Load if it is not a Postback. If you need to rebind, do it from
    >> your button click after you retrieve values. Ordering of events is fixed,
    >> so
    >> you have to be flexible.
    >>
    >> --
    >> Gregory A. Beamer
    >> MVP; MCP: +I, SE, SD, DBA
    >>
    >> ***************************
    >> Think Outside the Box!
    >> ***************************
    >>
    >>
    >> "juststarter" wrote:
    >>
    >> > Hello,
    >> > I have an aspx file where i've put a placeholder element. On load
    >> > (page_load) i create dynamically an html table which contains a
    >> > checkbox and
    >> > a radiobuttonlist in each tablerow . The radiobuttonlist contains two
    >> > items
    >> > (yes,no). Both the
    >> > checkboxes and the radiobuttonlist are NOT autopostbacked (
    >> > .autopostback =
    >> > false). When i press the submit button a sub (submit_pressed) is run.
    >> > My
    >> > problem is that i can not get the selected items in the
    >> > radiobuttonlists.This
    >> > is caused cos in the page_load event i call the sub that dynamically
    >> > recreates the table described above and so all values are reset (there
    >> > is a
    >> > rdb.item(0).selected=true line in my
    >> > code. If i try to remove that line of code, the radiobuttons keep their
    >> > values or not,after each submit, without a pattern (at least not one
    >> > that i
    >> > can see)!). I've also tried to keep the values in a collection,
    >> > instasiated
    >> > in the page_load but i couldn't do so cos the table (that contains the
    >> > checkboxes and radiobuttonlists) is not recognized at the beggining of
    >> > the
    >> > page_load (i guess it's not an object yet cos the sub that creates all
    >> > that
    >> > is at the end of page_load)....
    >> > Anyway...any ideas on how to get the selected item ? ? ? ?
    >> > thx a lot
    >> > theodore
    >> >
    >> > here is a portion of the code
    >> > in vb code
    >> > --------------------
    >> > 1. create the (html) table through code
    >> >
    >> > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    >> > System.EventArgs)
    >> > Dim ID as integer = Request("ID")
    >> > call addControlsInPlaceholder()
    >> > End Sub
    >> >
    >> >
    >> > sub addControlsInPlaceholder
    >> > 'create the table
    >> > dim oTable as new table
    >> > oTable.id="oTable"
    >> > oTable.GridLines=GridLines.None
    >> >
    >> >
    >> > 'first add the titles of each column
    >> > dim lb1 as new label
    >> > dim lb2 as new label
    >> >
    >> > dim cell1 as new TableHeaderCell
    >> > dim cell2 as new TableHeaderCell
    >> >
    >> > cell1.width=unit.pixel(180)
    >> > cell2.width=unit.pixel(120)
    >> >
    >> > dim row1 as new tablerow
    >> >
    >> > lb1.text= Category"
    >> > lb2.text= "isDisplayed"
    >> >
    >> > 'add labels to cells
    >> > Cell1.controls.add(lb1)
    >> > Cell2.controls.add(lb2)
    >> >
    >> > 'add (header) cells to row
    >> > Row1.cells.add(Cell1)
    >> > Row1.cells.add(Cell2)
    >> > 'add header row to table
    >> > otable.rows.add(Row1)
    >> >
    >> > 'fill the chkboxlist/radiobuttonlist(s) with items
    >> > Dim id As String
    >> > Dim subcListItem,subcListItem1,subcListItem2 As ListItem
    >> >
    >> > Dim myConnection As New OdbcConnection(Application("strConnect"))
    >> > dim strSQL as string
    >> >
    >> > strSQL = " SELECT ctg_id, ctg_name FROM categories ORDER BY ctg_id"
    >> > Dim myCommand As New OdbcCommand(strSQL, myConnection)
    >> > myConnection.Open()
    >> > Dim myReader As OdbcDataReader = myCommand.ExecuteReader()
    >> >
    >> > while myReader.Read()
    >> > 'create new Row for the table
    >> > dim oRow as new tableRow
    >> > 'create 2 cells for the row
    >> > dim oCell1 as new tableCell
    >> > dim oCell2 as new tableCell
    >> >
    >> >
    >> > oCell1.width=unit.pixel(180)
    >> > oCell2.width=unit.pixel(120)
    >> >
    >> > 'create checkbox and add it cell1 (the first cell of each row)
    >> > dim chkboxlist as new checkbox
    >> > chkboxlist.id = "categories_" & myReader.Item("ctg_id").ToString
    >> > chkboxlist.text=myReader.Item("ctg_name").ToString
    >> >
    >> >
    >> > oCell1.controls.add(chkboxlist)
    >> >
    >> > 'create radiobuttonlist and add it cell2 (the second cell of each row)
    >> > dim rdbDisplayed as new radiobuttonlist
    >> > rdbDisplayed.RepeatDirection=repeatdirection.Horizontal
    >> > rdbDisplayed.id="cs_isDisplayed_" & myReader.Item("ctg_id").ToString
    >> >
    >> > 'add items in radiobuttonlists
    >> > subcListItem1 = New ListItem
    >> > subcListItem1.Text = "yes"
    >> > subcListItem1.Value = "1"
    >> > rdbDisplayed.Items.Add(subcListItem1)
    >> >
    >> > subcListItem2 = New ListItem
    >> > subcListItem2.Text = "no"
    >> > subcListItem2.Value = "0"
    >> > rdbDisplayed.Items.Add(subcListItem2)
    >> > rdbDisplayed.items(0).selected=true
    >> >
    >> > oCell2.controls.add(rdbDisplayed)
    >> >
    >> >
    >> > 'add cells to row
    >> > oRow.cells.add(oCell1)
    >> > oRow.cells.add(oCell2)
    >> >
    >> > 'add row to table
    >> > otable.rows.add(oRow)
    >> > 'increase counter i, to create new row
    >> > i = i +1
    >> >
    >> > end while
    >> > 'add table to placeholder
    >> > plh1.controls.add(oTable)
    >> >
    >> > myCommand.dispose
    >> > myConnection.close
    >> >
    >> > end sub
    >> >
    >> >
    >> > 3. when the submit button is pressed ,i try something like
    >> >
    >> > private sub submit_pressed
    >> >
    >> > dim otable as new table
    >> > dim tr as new tablerow
    >> > dim tc as new tablecell
    >> > dim chk as checkbox
    >> > dim i,displayed as int16
    >> > dim rdbDisplayed,rdbAdult as radiobuttonlist
    >> >
    >> > 'plh1 is the placeholder in the html code
    >> > otable = ctype((plh1.controls(0)),table)
    >> >
    >> > for i = 1 to otable.rows.count-1
    >> >
    >> > tr = otable.controls(i)
    >> > tc = tr.controls(0)
    >> > 'tc.control(0) is a checkbox
    >> > chk = tc.controls(0)
    >> >
    >> > 'table cell
    >> > tc = tr.controls(1)
    >> > 'tc.ctontrol(0) is a radiobuttonlist
    >> > rdbDisplayed = tc.controls(0)
    >> >
    >> > '****************************
    >> > 'the following line always returns item(0) cos the sub
    >> > addControlsInPlaceholder has already run (it's called in the page_load
    >> > event)
    >> > before the submit_pressed is called
    >> > displayed = rdbDisplayed.selecteditem.value
    >> > '*****************************
    >> >
    >> > if chk.checked = true then
    >> > 'show displayed
    >> > end if
    >> >
    >> > next
    >> >
    >> > end sub
     
    S. Justin Gengo, Oct 26, 2005
    #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?UmljaGFyZA==?=

    Selected item in RadioButtonList

    =?Utf-8?B?UmljaGFyZA==?=, Jan 7, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    779
    =?Utf-8?B?UmljaGFyZA==?=
    Jan 7, 2005
  2. Groove
    Replies:
    1
    Views:
    14,394
    Groove
    May 11, 2006
  3. Iain
    Replies:
    3
    Views:
    944
  4. mldardy
    Replies:
    0
    Views:
    974
    mldardy
    Sep 28, 2010
  5. Peter Afonin

    How to change font of selected item in RadioButtonList

    Peter Afonin, Feb 22, 2004, in forum: ASP .Net Web Controls
    Replies:
    6
    Views:
    658
    Peter Afonin
    Feb 23, 2004
Loading...

Share This Page