.NETGURU
DataBinding to CheckBoxList Control?
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngcontrolsvb' list.


Mike Amundsen
I can't seem to find a way to complete databinding for a CheckBoxList
control.

With RadioButtonList controls I can use the DataTextField and DataValueField
properties to bind both display and datastorage values to the controls.
However, this is ignored by the CheckBoxList control and I see nothing in
the docs that say much about this.

Any help would be appreciated. Below is a simple page that I would expect to
display *all* values checked ON when the page first loads. However they are
alway all checked off instead.

TIA

MCA
Mike Amundsen
host your .NET Webs @ www.EraServer.NET

CODE FOLLOWS:
***************************************
<%@ Page Description="CheckBoxListDB.aspx" %>
<%@ import namespace="System.Data" %>

<script Language="VB" runat="server">

' display list the very first time only
sub Page_Load(Source As object, e As EventArgs)

if IsPostBack=false then
with cbListDB
.DataSource = CreateDataSource()
.DataTextField="ItemName"
.DataValueField="ItemSelected"
.DataBind()
end with
end if

end sub

' build selection list for display
sub Check_Clicked(sender As Object, e As EventArgs)

Dim i As Integer

Message.Text = "You selected:<ul>"

For i=0 To cbListDB.Items.Count - 1
If cbListDB.Items(i).Selected Then
Message.Text &= "<li>" & cbListDB.Items(i).Text & "</li>"
End If
Next

Message.Text &= "</ul>"

end sub

' create some data for this example
function CreateDataSource() as DataView

dim dt as DataTable = new DataTable()
dim dr as DataRow
dim i as integer

with dt.Columns
.Add(new DataColumn("ItemName",GetType(String)))
.Add(new DataColumn("ItemSelected",GetType(boolean)))
end with

for i=0 to 4
dr = dt.NewRow()
dr(0) = "Widget " & chr(i+65)
dr(1) = true

dt.rows.Add(dr)
next

dim dv as DataView = new DataView(dt)
return dv

end function

</script>

<body>

<h2>Data Bound Check Box List Demo</h2>
<hr />

<form runat="server">

<asp:CheckBoxList id="cbListDB"
RepeatColumns="2"
TextAlign="right"
AutoPostBack="True"
OnSelectedIndexChanged="Check_Clicked"
runat="server">

</asp:CheckBoxList>

<p>
<asp:label id="Message" runat="server"/>
</p>

</form>
</body>
</html>

Reply to this message...
 
    
Doug Seven

Mike,

Here is a working sample for you. It seemed to work as expected. Are you
getting an error message?

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
If Not Page.IsPostBack Then
Dim myDataSet As New DataSet
Dim myDataAdapter As SqlDataAdapter

myDataAdapter = New SqlDataAdapter("SELECT TOP 5 * FROM Customers",
"server=localhost;database=Northwind;uid=sa;pwd=;")
myDataAdapter.Fill(myDataSet, "Customers")

With myCheckboxList
    .DataSource = myDataSet.Tables("Customers")
    .DataValueField="CustomerID"
    .DataTextField="CompanyName"
End With
Page.DataBind()
Else
Response.Write("You Chose: " + myCheckboxList.SelectedItem.Value)
End If
End Sub
</script>
<html>
<body>
<form runat="server">
<p>
<b>Binding to a CheckboxList</b><br>
<asp:CheckboxList id="myCheckboxList" runat="server" />
</p>
<asp:Button runat="server" Text="Submit" />
</form>
</body>
</html>
______________________________________________
doug.seven | sr. junkie | www.dotnetjunkies.com

[ putting the dot in .net ]

programming data-driven web applications with asp.net
by doug.seven and donny.mack
http://www.amazon.com/exec/obidos/ASIN/0672321068/dotnetjunkies-20/

"Honestly, this really isn't a brains kind of operation"
- Benicio del Toro, The Way of the Gun

-----Original Message-----
From: Mike Amundsen [mailto:Click here to reveal e-mail address]
Sent: Thursday, August 02, 2001 11:59 AM
To: aspngcontrolsvb
Subject: [aspngcontrolsvb] DataBinding to CheckBoxList Control???

I can't seem to find a way to complete databinding for a CheckBoxList
control.

With RadioButtonList controls I can use the DataTextField and DataValueField
properties to bind both display and datastorage values to the controls.
However, this is ignored by the CheckBoxList control and I see nothing in
the docs that say much about this.

Any help would be appreciated. Below is a simple page that I would expect to
display *all* values checked ON when the page first loads. However they are
alway all checked off instead.

TIA

MCA
Mike Amundsen
host your .NET Webs @ www.EraServer.NET

CODE FOLLOWS:
***************************************
<%@ Page Description="CheckBoxListDB.aspx" %>
<%@ import namespace="System.Data" %>

<script Language="VB" runat="server">

' display list the very first time only
sub Page_Load(Source As object, e As EventArgs)

if IsPostBack=false then
with cbListDB
.DataSource = CreateDataSource()
.DataTextField="ItemName"
.DataValueField="ItemSelected"
.DataBind()
end with
end if

end sub

' build selection list for display
sub Check_Clicked(sender As Object, e As EventArgs)

Dim i As Integer

Message.Text = "You selected:<ul>"

For i=0 To cbListDB.Items.Count - 1
If cbListDB.Items(i).Selected Then
Message.Text &= "<li>" & cbListDB.Items(i).Text & "</li>"
End If
Next

Message.Text &= "</ul>"

end sub

' create some data for this example
function CreateDataSource() as DataView

dim dt as DataTable = new DataTable()
dim dr as DataRow
dim i as integer

with dt.Columns
.Add(new DataColumn("ItemName",GetType(String)))
.Add(new DataColumn("ItemSelected",GetType(boolean)))
end with

for i=0 to 4
dr = dt.NewRow()
dr(0) = "Widget " & chr(i+65)
dr(1) = true

dt.rows.Add(dr)
next

dim dv as DataView = new DataView(dt)
return dv

end function

</script>

<body>

<h2>Data Bound Check Box List Demo</h2>
<hr />

<form runat="server">

<asp:CheckBoxList id="cbListDB"
RepeatColumns="2"
TextAlign="right"
AutoPostBack="True"
OnSelectedIndexChanged="Check_Clicked"
runat="server">

</asp:CheckBoxList>

<p>
<asp:label id="Message" runat="server"/>
</p>

</form>
</body>
</html>

| [aspngcontrolsvb] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngcontrolsvb.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

Reply to this message...
 
    
Mike Amundsen
DS:

Thanks for the post. Your example works, but not as I had hoped.

I am trying to databind to the *Checked* property of the control. In
otherwords, for all items that have a column set to TRUE, I was expecting to
see the checkbox turned ON.

Basically, I am expecting to be able to Bind to the Checked Property of the
collection, but I don't see this anywhere.

Seems a disappointment that I would need to 'bixel' through the list to set
the proper Checked=true, Checked=false properties for all the controls in
the databound list.

Maybe a function....

MCA

"Doug Seven" <Click here to reveal e-mail address> wrote in message
news:451392@aspngcontrolsvb...
[Original message clipped]

Reply to this message...
 
    
Doug Seven

Mike,

Here's a revised sample. Not sure if this is what you are looking for (I
don't think it is), but it is a viable workaround. I added an event handler
for the CheckBox.OnLoad event and set the Selected property there.

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
If Not Page.IsPostBack Then
Dim myDataSet As New DataSet
Dim myDataAdapter As SqlDataAdapter

myDataAdapter = New SqlDataAdapter("SELECT TOP 5 * FROM Customers",
"server=localhost;database=Northwind;uid=sa;pwd=;")
myDataAdapter.Fill(myDataSet, "Customers")

With myCheckboxList
    .DataSource = myDataSet.Tables("Customers")
    .DataValueField="CustomerID"
    .DataTextField="CompanyName"
End With
Page.DataBind()
Else
Response.Write("You Chose: " + myCheckboxList.SelectedItem.Value)
End If
End Sub

Sub Check_OnLoad(Sender As Object, E As EventArgs)
    Dim i As Integer
    For i = 0 to myCheckboxList.Items.Count-1
        If myCheckboxList.Items(i).Value.StartsWith("A") Then
            myCheckboxList.Items(i).Selected = True
        End If
    Next
End Sub
</script>
<html>
<body>
<form runat="server">
<p>
<b>Binding to a CheckboxList</b><br>
<asp:CheckboxList id="myCheckboxList" runat="server"
OnLoad="Check_OnLoad"/>
</p>
<asp:Button runat="server" Text="Submit" />
</form>
</body>
</html>

______________________________________________
doug.seven | sr. junkie | www.dotnetjunkies.com

[ putting the dot in .net ]

programming data-driven web applications with asp.net
by doug.seven and donny.mack
http://www.amazon.com/exec/obidos/ASIN/0672321068/dotnetjunkies-20/

"Honestly, this really isn't a brains kind of operation"
- Benicio del Toro, The Way of the Gun

-----Original Message-----
From: Mike Amundsen [mailto:Click here to reveal e-mail address]
Sent: Thursday, August 02, 2001 12:42 PM
To: aspngcontrolsvb
Subject: [aspngcontrolsvb] Re: DataBinding to CheckBoxList Control???

DS:

Thanks for the post. Your example works, but not as I had hoped.

I am trying to databind to the *Checked* property of the control. In
otherwords, for all items that have a column set to TRUE, I was expecting to
see the checkbox turned ON.

Basically, I am expecting to be able to Bind to the Checked Property of the
collection, but I don't see this anywhere.

Seems a disappointment that I would need to 'bixel' through the list to set
the proper Checked=true, Checked=false properties for all the controls in
the databound list.

Maybe a function....

MCA

"Doug Seven" <Click here to reveal e-mail address> wrote in message
news:451392@aspngcontrolsvb...
[Original message clipped]

| [aspngcontrolsvb] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngcontrolsvb.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

Reply to this message...
 
    
Mike Amundsen
DS:

yep, this is a decent workaround that accomplishes what I was hoping for.

Looks like another solution would be to create a derived class
"myCheckBoxList" that has a new bindable property, but I haven't tried that
yet.

Thanks for your help!

MCA
Mike Amundsen
host your .NET Webs at www.EraServer.NET

-----Original Message-----
From: Doug Seven [mailto:Click here to reveal e-mail address]
Sent: Thursday, August 02, 2001 2:58 PM
To: aspngcontrolsvb
Subject: [aspngcontrolsvb] Re: DataBinding to CheckBoxList Control???

Mike,

Here's a revised sample. Not sure if this is what you are looking for (I
don't think it is), but it is a viable workaround. I added an event handler
for the CheckBox.OnLoad event and set the Selected property there.

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
If Not Page.IsPostBack Then
Dim myDataSet As New DataSet
Dim myDataAdapter As SqlDataAdapter

myDataAdapter = New SqlDataAdapter("SELECT TOP 5 * FROM Customers",
"server=localhost;database=Northwind;uid=sa;pwd=;")
myDataAdapter.Fill(myDataSet, "Customers")

With myCheckboxList
    .DataSource = myDataSet.Tables("Customers")
    .DataValueField="CustomerID"
    .DataTextField="CompanyName"
End With
Page.DataBind()
Else
Response.Write("You Chose: " + myCheckboxList.SelectedItem.Value)
End If
End Sub

Sub Check_OnLoad(Sender As Object, E As EventArgs)
    Dim i As Integer
    For i = 0 to myCheckboxList.Items.Count-1
        If myCheckboxList.Items(i).Value.StartsWith("A") Then
            myCheckboxList.Items(i).Selected = True
        End If
    Next
End Sub
</script>
<html>
<body>
<form runat="server">
<p>
<b>Binding to a CheckboxList</b><br>
<asp:CheckboxList id="myCheckboxList" runat="server"
OnLoad="Check_OnLoad"/>
</p>
<asp:Button runat="server" Text="Submit" />
</form>
</body>
</html>

______________________________________________
doug.seven | sr. junkie | www.dotnetjunkies.com

[ putting the dot in .net ]

programming data-driven web applications with asp.net
by doug.seven and donny.mack
http://www.amazon.com/exec/obidos/ASIN/0672321068/dotnetjunkies-20/

"Honestly, this really isn't a brains kind of operation"
- Benicio del Toro, The Way of the Gun

-----Original Message-----
From: Mike Amundsen [mailto:Click here to reveal e-mail address]
Sent: Thursday, August 02, 2001 12:42 PM
To: aspngcontrolsvb
Subject: [aspngcontrolsvb] Re: DataBinding to CheckBoxList Control???

DS:

Thanks for the post. Your example works, but not as I had hoped.

I am trying to databind to the *Checked* property of the control. In
otherwords, for all items that have a column set to TRUE, I was expecting to
see the checkbox turned ON.

Basically, I am expecting to be able to Bind to the Checked Property of the
collection, but I don't see this anywhere.

Seems a disappointment that I would need to 'bixel' through the list to set
the proper Checked=true, Checked=false properties for all the controls in
the databound list.

Maybe a function....

MCA

"Doug Seven" <Click here to reveal e-mail address> wrote in message
news:451392@aspngcontrolsvb...
[Original message clipped]

| [aspngcontrolsvb] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngcontrolsvb.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

| [aspngcontrolsvb] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngcontrolsvb.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

Reply to this message...
 
 
System.Data.DataColumn
System.Data.DataRow
System.Data.DataSet
System.Data.DataTable
System.Data.DataView
System.Data.SqlClient.SqlDataAdapter
System.EventArgs
System.Messaging.Message
System.Web.UI.DataBinding
System.Web.UI.Page
System.Web.UI.WebControls.CheckBoxList
System.Web.UI.WebControls.RadioButtonList
System.Web.UI.WebControls.TextAlign
System.Windows.Forms.CheckBox




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification