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>
|
|
| |
| |
| 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
|
|
| |
|
| |
| 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]
|
|
| |
|
| |
| 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
|
|
| |
|
| |
| 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
|
|
| |
|
|
|
|
|