Dynamic CheckBoxList Creation
-
I am trying to get the Page_load event to dynamically update my checkboxlist values to correspond with the columns in my grid view. the code I have is as follows: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Dim listItemCount As Integer = -1 Dim totalColNum As Integer = GridView1.Columns.Count If Page.IsPostBack Then ''Do Something Else While listItemCount <= totalColNum listItemCount += 1 CheckBoxList2.Items.Add(GridView1.Columns(listItemCount).ToString) End While End If End Sub But I am getting this error: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index Any ideas??
-
I am trying to get the Page_load event to dynamically update my checkboxlist values to correspond with the columns in my grid view. the code I have is as follows: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Dim listItemCount As Integer = -1 Dim totalColNum As Integer = GridView1.Columns.Count If Page.IsPostBack Then ''Do Something Else While listItemCount <= totalColNum listItemCount += 1 CheckBoxList2.Items.Add(GridView1.Columns(listItemCount).ToString) End While End If End Sub But I am getting this error: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index Any ideas??
Try replacing '<=' with '<' in 'While listItemCount <= totalColNum'. If this doesn't fix it put a break inside the loop and see just how many times you are actually looping through before the error occurs. When the program reaches the break you can hold the mouse over the 'totalColNum' variable and see how many times its trying to loop.
-
I am trying to get the Page_load event to dynamically update my checkboxlist values to correspond with the columns in my grid view. the code I have is as follows: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Dim listItemCount As Integer = -1 Dim totalColNum As Integer = GridView1.Columns.Count If Page.IsPostBack Then ''Do Something Else While listItemCount <= totalColNum listItemCount += 1 CheckBoxList2.Items.Add(GridView1.Columns(listItemCount).ToString) End While End If End Sub But I am getting this error: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index Any ideas??
I figured it out:
Protected Sub Page\_Load(ByVal sender As Object, ByVal e As System.EventArgs) Dim listItemCount As Integer = -1 Dim totalColNum As Integer = GridView1.Columns.Count Dim valNum As Integer = -1 If Not Page.IsPostBack Then totalColNum = totalColNum - 1 While listItemCount < -1 listItemCount += 1 valNum += 1 CheckBoxList2.Items.Add(GridView1.Columns(listItemCount).ToString) CheckBoxList2.Items.Item(listItemCount).Value = valNum.ToString End While End If End Sub