Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. how I can get id's of checked checkboxes, which are runtime added?

how I can get id's of checked checkboxes, which are runtime added?

Scheduled Pinned Locked Moved ASP.NET
helpdatabasesysadmindata-structuresquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kripa21
    wrote on last edited by
    #1

    Greetings, I have loaded checkboxes in asp table control at form load evevt; from database. Now I want to get all checked values in 1 array. I used for each loop & stored control in 1 checkbox object & if that is checked then add value in array. my code is as follows Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then fillControls() End If End Sub in fillControls(), I took datatable for Id & name. & added checkboxes as Dim objTr As New TableRow For intCnt = 0 To objDataTable.Rows.Count - 1 If intCnt Mod 4 = 0 Then objTr = New TableRow table1.Rows.Add(objTr) End If Dim objTd As New TableCell Dim objChk As New CheckBox objTr.Cells.Add(objTd) objTd.Controls.Add(objChk) objChk.Attributes.Add("runat", "server") objChk.ID = objDataTable.Rows(intCnt).Item(0) objChk.Text =objDataTable.Rows(intCnt).Item(1) Next And when user submit the form< I want to get all id's of checkboxes in 1 array for that I wrote, Dim arrSubMnu() As Integer = New Integer() {} For Each ctl As Control In table1.Controls If TypeOf ctl Is CheckBox Then If CType(ctl, CheckBox).Checked = True Then ReDim Preserve arrIds(arrIds.Length) arrIds(arrIds.Length) = ctl.ID End If End If Next ctl I couldn't find out where is error.I am not getting anything in array Please Help Me to solve this problem

    P K K 3 Replies Last reply
    0
    • K kripa21

      Greetings, I have loaded checkboxes in asp table control at form load evevt; from database. Now I want to get all checked values in 1 array. I used for each loop & stored control in 1 checkbox object & if that is checked then add value in array. my code is as follows Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then fillControls() End If End Sub in fillControls(), I took datatable for Id & name. & added checkboxes as Dim objTr As New TableRow For intCnt = 0 To objDataTable.Rows.Count - 1 If intCnt Mod 4 = 0 Then objTr = New TableRow table1.Rows.Add(objTr) End If Dim objTd As New TableCell Dim objChk As New CheckBox objTr.Cells.Add(objTd) objTd.Controls.Add(objChk) objChk.Attributes.Add("runat", "server") objChk.ID = objDataTable.Rows(intCnt).Item(0) objChk.Text =objDataTable.Rows(intCnt).Item(1) Next And when user submit the form< I want to get all id's of checkboxes in 1 array for that I wrote, Dim arrSubMnu() As Integer = New Integer() {} For Each ctl As Control In table1.Controls If TypeOf ctl Is CheckBox Then If CType(ctl, CheckBox).Checked = True Then ReDim Preserve arrIds(arrIds.Length) arrIds(arrIds.Length) = ctl.ID End If End If Next ctl I couldn't find out where is error.I am not getting anything in array Please Help Me to solve this problem

      P Offline
      P Offline
      PJWindsor
      wrote on last edited by
      #2

      What is the definition for table1 variable? Is it public? as from the code it looks like it could be used as a local variable. You could set a break point on the line starting "For Each ctl As Control In table1.Controls" and see if it says that the table contains any controls If it does then then next line to look at would be the runtime values of the "If CType(ctl, CheckBox).Checked = True Then" line does it say the value is checked? how does that corrospone to the database values? Any chance you could post the code as a code block? the entire page and behind code? Phil

      1 Reply Last reply
      0
      • K kripa21

        Greetings, I have loaded checkboxes in asp table control at form load evevt; from database. Now I want to get all checked values in 1 array. I used for each loop & stored control in 1 checkbox object & if that is checked then add value in array. my code is as follows Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then fillControls() End If End Sub in fillControls(), I took datatable for Id & name. & added checkboxes as Dim objTr As New TableRow For intCnt = 0 To objDataTable.Rows.Count - 1 If intCnt Mod 4 = 0 Then objTr = New TableRow table1.Rows.Add(objTr) End If Dim objTd As New TableCell Dim objChk As New CheckBox objTr.Cells.Add(objTd) objTd.Controls.Add(objChk) objChk.Attributes.Add("runat", "server") objChk.ID = objDataTable.Rows(intCnt).Item(0) objChk.Text =objDataTable.Rows(intCnt).Item(1) Next And when user submit the form< I want to get all id's of checkboxes in 1 array for that I wrote, Dim arrSubMnu() As Integer = New Integer() {} For Each ctl As Control In table1.Controls If TypeOf ctl Is CheckBox Then If CType(ctl, CheckBox).Checked = True Then ReDim Preserve arrIds(arrIds.Length) arrIds(arrIds.Length) = ctl.ID End If End If Next ctl I couldn't find out where is error.I am not getting anything in array Please Help Me to solve this problem

        K Offline
        K Offline
        Kannan Ar
        wrote on last edited by
        #3

        Controls created at runtime will lost at the time of post back. One solution is to call fillControl function outside the If Not IsPostBack checking.

        1 Reply Last reply
        0
        • K kripa21

          Greetings, I have loaded checkboxes in asp table control at form load evevt; from database. Now I want to get all checked values in 1 array. I used for each loop & stored control in 1 checkbox object & if that is checked then add value in array. my code is as follows Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then fillControls() End If End Sub in fillControls(), I took datatable for Id & name. & added checkboxes as Dim objTr As New TableRow For intCnt = 0 To objDataTable.Rows.Count - 1 If intCnt Mod 4 = 0 Then objTr = New TableRow table1.Rows.Add(objTr) End If Dim objTd As New TableCell Dim objChk As New CheckBox objTr.Cells.Add(objTd) objTd.Controls.Add(objChk) objChk.Attributes.Add("runat", "server") objChk.ID = objDataTable.Rows(intCnt).Item(0) objChk.Text =objDataTable.Rows(intCnt).Item(1) Next And when user submit the form< I want to get all id's of checkboxes in 1 array for that I wrote, Dim arrSubMnu() As Integer = New Integer() {} For Each ctl As Control In table1.Controls If TypeOf ctl Is CheckBox Then If CType(ctl, CheckBox).Checked = True Then ReDim Preserve arrIds(arrIds.Length) arrIds(arrIds.Length) = ctl.ID End If End If Next ctl I couldn't find out where is error.I am not getting anything in array Please Help Me to solve this problem

          K Offline
          K Offline
          kripa21
          wrote on last edited by
          #4

          Finally I got answer Just using Request object you can get all objects even runtime added on submit button : <pre>Dim arrSubMnu() As Integer = New Integer() {}             Dim strT As String             For Each strT In Request.Form                   If strT.ToString.Contains("Chk_") And Request.Form(strT).ToString.ToUpper = "ON" Then                         Dim strTemp As String = strT                         ReDim Preserve arrSubMnu(arrSubMnu.Length)                         arrSubMnu(arrSubMnu.Length - 1) = strTemp.Substring(strTemp.IndexOf("Chk_") + 4)                   End If             Next</pre> (here set 'Chk_' as prefix to checkbox id's to find control easily)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups