how I can get id's of checked checkboxes, which are runtime added?
-
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
-
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
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
-
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
-
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
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)