How do you databind a CheckBox?

A

aualias

This seems like such a lame question, but how do you bind to a CheckBox with
ASP.NET? With Windows Forms there is a DataBindings collection. I do not
see it with the web version of the control, but there is a DataBinding
event.

Thanks.

AU
 
K

Ken Cox [Microsoft MVP]

It is the checkbox control that is lame. You can only assign the text value
as a string and a boolean for the Checked property.

The CheckBoxList acts more like a regular control, as shown in the code
below.



Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim dt As New DataTable
dt = CreateDataSource()
CheckBox1.Text = dt.Rows(0).Item("StringValue")
CheckBox1.Checked = dt.Rows(0).Item("Boolean")
CheckBoxList1.DataSource = dt
CheckBoxList1.DataTextField = "StringValue"
CheckBoxList1.DataValueField = "Boolean"
CheckBoxList1.DataBind()
End Sub

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function
 
A

aualias

Thanks Ken,

I guess that explains why I could not find any examples of databinding to a
CheckBox...

One question, what causes the DataBinding event to fire?

Alfredo
 
S

Steven Cheng[MSFT]

Hi Alfredo,

In fact the DataBinding mode in WebForm is quite different from that in
Winform application. Because the Winform application are running on a
single machine and all the runtime datas are stored and persiste in memory,
so the DataBinding items of the controls are automatically synchronized
with the datasource, we only need to set the datasource but needn't
manually bing them. However, in web application the ASP.NET page is
request/response based, the page object and its contained controls's actual
instance are only existing when the request is processing at serverside.
This is called the web page's lifecycle, after that, the page is rendered
to client, and the page and controls instance's lifecyle end. So those
Page's Serverside events such as Load, PreRender.. are the only place to
set the control's member/property or bind data with them. And the ASP.NET
server control don't have "DataBindings" collection, we set their
databindings via the DataBinding Expression in page template such as
<asp:TextBox id=TextBox1runat="server" Text="<%# expression or function%>" >
</asp:TextBox>
<asp:CheckBox id="CheckBox1" runat="server" Text="<%# expression or
function%>">
</asp:CheckBox>

Then, in page's event such as Page_Load or other event, we can explicitly
call the Control's DataBind() method to fire the DataBinding event of the
control, then it will bind the control's certain property via the above <%#
%> expression set in page template.

Also, we can call Page.DataBind() method, this will make the page to fire
all the controls which has databind expression to bind data.

Here is the reference on web form databinding syntax :
#Data Binding Expression Syntax
http://msdn.microsoft.com/library/en-us/cpgenref/html/cpconDatabindingExpres
sionSyntax.asp?frame=true

Hope also helpful. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
A

aualias

Steven,

I have one quick question. Can you databind a TextBox? If so, how.

Alfredo
 
S

Steven Cheng[MSFT]

Hi Alfredo,

As for binding a TextBox, it is just the same as Binding other controls,
first add the Binding Expression in page template such as

<asp:TextBox id="TextBox1" runat="server" Text='<%# GetTextValue()
%>'></asp:TextBox>

Then implement the "GetTextValue()" function in the Page(in codebehind) ,
just as
Public Class TextBox
Inherits System.Web.UI.Page

Public Function GetTextValue() As String
Return "Text Value"
End Function

........................

After that, we need to call TextBox1.DataBind() or
Page.DataBind to invoke the Control's actual databinding operation.
#Another databinding tutorial in ASP.NET QuickStart
http://samples.gotdotnet.com/quickstart/aspplus/doc/webdatabinding.aspx

In addition, for ASP.NET Server Control especiall those simple ones such as
TextBox or Label, its rather easier to set their value by their property
directly. For example:

TextBox.Text = xxxx;

Do you think so? If you have any further questions, please feel free to
post here.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
A

aualias

Steven,



From your example:



<asp:TextBox id=TextBox1runat="server" Text="<%# expression or function%>" >
</asp:TextBox>



An asp:TextBox does not have a Text property that can be assigned to in this
manner. When you assign a value to the Text property it appears here:

<asp:TextBox ...>HERE</asp:TextBox>



Am I missing something?



David
 
S

Steven Cheng[MSFT]

Hi David,

We can use the
<asp:TextBox id=TextBox1runat="server" Text="<%# expression or function%>" >
</asp:TextBox>
or
<asp:TextBox id=TextBox1runat="server" Text="fdsfdsfdsfsdf" >
</asp:TextBox>

style template to assign TextBox value because the TextBox control does
have a "Text"
property, the reason it doesn't appear in aspx template is something
different at the IDE's intellisense. I've tried both the above styles in my
aspx page and work ok. Also, as for the
<asp:TextBox ...>HERE</asp:TextBox>
style , it's ok for plain text value, but if we assign DataBinding
expression as

<asp:TextBox ...><%# expression %></asp:TextBox>

that'll cause error. Please have a try and confirm this. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
S

Steven Cheng[MSFT]

Hi David,

Have you had a chance to check out the suggestions in my last reply or have
you got any further ideas on this issue? If you have anything unclear or if
there're anything else we can help, please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
A

aualias

Steven,

Sorry I didn't get a chance to test this sooner. It does work fine.

I'm appalled at my own reliance on Intellisense. When the Text property
didn't pop up, I just assumed that it could not be assigned to. And I used
to write code in assembler...

Thanks.

AU
 
Joined
Apr 29, 2011
Messages
1
Reaction score
0
Possible solution

I managed to bind this way:


// Test.aspx:
<asp:CheckBox ID="chkExclusive" runat="server" Checked='<%# Bind("ExclusiveGrouping") %>' />

// SQL for DataSource:
select ..., convert(bit, case when lgt.LGT_EXLUSIVE_GROUPING = 0 then 0 else 1 end) as ExclusiveGrouping from SomeTable where ...


Hope this helps...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top