Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_beginners thread: ChecklBoxList and data binding


Message #1 by "Norman Beresford" <n.beresford@a...> on Mon, 8 Jul 2002 17:17:41 +0100
Hi All

I'm building a simple .aspx form.  I've got a CheckBoxList control on my
page which I'm binding to a datareader when the page is initially loaded (so
when Page.IsPostBack is false).  This works fine and I'm getting the
checkboxes I expected.  The problem I have is when I try to access the
values of the collection when the form is submitted.  The Count property
simply gives me 0, no matter how many boxes have been checked.  If I create
the checkboxes on the page by hand (using <asp:ListItem></asp:ListItem>)
then the code gives me the values of them and if each one is checked or not,
it just doesn't work when I've used databind() to create them.  Does anyone
have any ideas?

Thanks in advance

Norman

Here's the main code:

On the .aspx page:

<asp:CheckBoxList id="cbImageCategory" runat="server" RepeatColumns="3"
EnableViewState="True"></asp:CheckBoxList>

In the .vb file:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        If Page.IsPostBack Then
            Dim i As Integer
            lblUploaded.Text += "<br>Categories"
            lblUploaded.Text += "<br>Category count = " &
cbImageCategory.Items.Count
            For i = 0 To cbImageCategory.Items.Count - 1
                lblUploaded.Text += "<br>" & cbImageCategory.Items(i).Text
                If cbImageCategory.Items(i).Selected Then
                    lblUploaded.Text += " selected"
                End If
            Next
        Else
            connSQLEden.Open()
            Dim cmdEdenImageCategories As New SqlCommand()
            cmdEdenImageCategories.CommandText = "SELECT categoryID,
categoryName FROM aspig_tblCategories WHERE categoryDeletedBY = 0"
            cmdEdenImageCategories.Connection = connSQLEden
            Dim drEdenImageCat As SqlDataReader
            drEdenImageCat = cmdEdenImageCategories.ExecuteReader
            cbImageCategory.DataSource = drEdenImageCat
            cbImageCategory.DataValueField = "categoryID"
            cbImageCategory.DataTextField = "categoryName"
            cbImageCategory.DataBind()
            drEdenImageCat.Close()
            connSQLEden.Close()
        End If

    End Sub

Message #2 by "Mitesh" <mitesh_bilimoria@y...> on Mon, 8 Jul 2002 20:18:55
I think its better you use DataAdapter and DataSet for that as the 
datasource you are assigning is datareader. As i have used i have used 
dataSet and source tobe dataset.Tables["Name of table"]

you can also use DataView...


Mitesh
> Hi All

I'm building a simple .aspx form.  I've got a CheckBoxList control on my
page which I'm binding to a datareader when the page is initially loaded 
(so
when Page.IsPostBack is false).  This works fine and I'm getting the
checkboxes I expected.  The problem I have is when I try to access the
values of the collection when the form is submitted.  The Count property
simply gives me 0, no matter how many boxes have been checked.  If I 
create
the checkboxes on the page by hand (using <asp:ListItem></asp:ListItem>)
then the code gives me the values of them and if each one is checked or 
not,
it just doesn't work when I've used databind() to create them.  Does 
anyone
have any ideas?

Thanks in advance

Norman

Here's the main code:

On the .aspx page:

<asp:CheckBoxList id="cbImageCategory" runat="server" RepeatColumns="3"
EnableViewState="True"></asp:CheckBoxList>

In the .vb file:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        If Page.IsPostBack Then
            Dim i As Integer
            lblUploaded.Text += "<br>Categories"
            lblUploaded.Text += "<br>Category count = " &
cbImageCategory.Items.Count
            For i = 0 To cbImageCategory.Items.Count - 1
                lblUploaded.Text += "<br>" & cbImageCategory.Items(i).Text
                If cbImageCategory.Items(i).Selected Then
                    lblUploaded.Text += " selected"
                End If
            Next
        Else
            connSQLEden.Open()
            Dim cmdEdenImageCategories As New SqlCommand()
            cmdEdenImageCategories.CommandText = "SELECT categoryID,
categoryName FROM aspig_tblCategories WHERE categoryDeletedBY = 0"
            cmdEdenImageCategories.Connection = connSQLEden
            Dim drEdenImageCat As SqlDataReader
            drEdenImageCat = cmdEdenImageCategories.ExecuteReader
            cbImageCategory.DataSource = drEdenImageCat
            cbImageCategory.DataValueField = "categoryID"
            cbImageCategory.DataTextField = "categoryName"
            cbImageCategory.DataBind()
            drEdenImageCat.Close()
            connSQLEden.Close()
        End If

    End Sub


  Return to Index