Link to home
Start Free TrialLog in
Avatar of Nizix
Nizix

asked on

Problem pre-selecitng checkboxlist in asp.net (vb)

Hi - when i try to Pre-select checkboxes from a databound checkbox list - my code only selects the first box and ignores the others.

The status field is coming from database, and can be 1 or 0. If its 1, i want the checkbox to be checked, if its 0, then nothing. It only selects the first item, but the rest that have 1s dont get selected. I think its missing something, like movenext  but i dont know what or where?

i used the reponse.write to help debug, to make sure the reader was reading through every item, and it was.

Output from response.write was:
100101001000

Only the first one gets read, i have no idea why. If i make it so its 0's gets checked, then it will be the 2nd item that gets checked, but again the rest will be ignored.
objConn.Open()
        Dim objReader As OleDbDataReader = objCmd.ExecuteReader(CommandBehavior.CloseConnection)
 
        While objReader.Read()
            Dim currentCheckBox As ListItem = CheckBoxList1.Items.FindByValue(objReader("status"))
            Response.Write(currentCheckBox)
            If currentCheckBox.Value = 1 Then
                currentCheckBox.Selected = True
            End If
        End While

Open in new window

Avatar of Nizix
Nizix

ASKER

I would like to add:

When i took out the If - Then statement, and just left it to:
currentCheckBox.Selected = True

It only selects the first two, when theortically it should be selecting all of them. So for some reason its limited to the first two, instead of all of them. I think there is something wrong with the listitem declaration, with the FindByValue(objReader("status"))
Avatar of Carl Tawn
The problem you are having is down to the value of the checkboxes. How are you adding items to your listbox in the first place ?
Avatar of Nizix

ASKER

Even though i set the DatavalueField to "Status", when i checked the source code, there is no "value" field in the html, just auto generated name, id, and then The DataTextField inside the label tags.

I am adding items through a command, I just databind the results of the command from a stored procedure in the database. i attached the code.

The datagrid is just for testing purposes.
Imports System.Data
Imports System.Data.OleDb
 
Public Class WebForm1
    Inherits System.Web.UI.Page
 
#Region " Web Form Designer Generated Code "
 
    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
 
    End Sub
    Protected WithEvents CheckBoxList1 As System.Web.UI.WebControls.CheckBoxList
    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
 
    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object
 
    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub
 
#End Region
 
    Dim cnstr As String = ConfigurationSettings.AppSettings("strConn")
    Dim objConn As New OleDbConnection(cnstr)
    Dim objCmd As OleDbCommand = New OleDbCommand("sp_CheckBoxList", objConn)
    Dim i As Integer
 
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Page.IsPostBack = False Then
            BindCheckBox()
        End If
    End Sub
 
    Sub BindCheckBox()
        objCmd.CommandType = CommandType.StoredProcedure
        objCmd.Parameters.Add("@id", 826).Direction = ParameterDirection.Input
 
        objConn.Open()
        CheckBoxList1.DataSource = objCmd.ExecuteReader(CommandBehavior.CloseConnection)
        CheckBoxList1.DataBind()
 
        objConn.Open()
        DataGrid1.DataSource = objCmd.ExecuteReader(CommandBehavior.CloseConnection)
        DataGrid1.DataBind()
 
        objConn.Open()
        Dim objReader As OleDbDataReader = objCmd.ExecuteReader(CommandBehavior.CloseConnection)
 
        While objReader.Read()
            Dim currentCheckBox As ListItem = CheckBoxList1.Items.FindByValue(objReader("status").ToString())
            Response.Write(currentCheckBox)
            If currentCheckBox.Value = 1 Then
                currentCheckBox.Selected = True
            End If
 
        End While
 
        'Do While objReader.Read()
        '    Dim Item As ListItem = CheckBoxList1.Items.FindByValue(objReader("status"))
        '    For Each Item In CheckBoxList1.Items
        '        CheckBoxList1.Items.FindByValue(objReader("status")).Selected = True
        '    Next
        'Loop
 
    End Sub
End Class
 
-----------------HTML---------------
		<form id="Form1" method="post" runat="server">
			<asp:checkboxlist id="CheckBoxList1" runat="server" DataTextField="Page_title" DataValueField="Status"></asp:checkboxlist>
			<asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
		</form>

Open in new window

What columns does the stored procedure, that you use to pouplate your checkboxlist, return ?
Avatar of Nizix

ASKER

It returns all columns that i made through the SQL statement, in the datagrid it displays them all. But for the checkbox list im just using "Page_title" and "Status". I think there are around 10 columns, like ID, title, description, and the last is status, as string (1 or 0), not boolean.
ASKER CERTIFIED SOLUTION
Avatar of Nizix
Nizix

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial