Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > How do you databind a CheckBox?

Reply
Thread Tools

How do you databind a CheckBox?

 
 
aualias
Guest
Posts: n/a
 
      05-25-2004
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


 
Reply With Quote
 
 
 
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      05-26-2004
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
"aualias" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>


 
Reply With Quote
 
 
 
 
aualias
Guest
Posts: n/a
 
      05-26-2004
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



"Ken Cox [Microsoft MVP]" <(E-Mail Removed)> wrote in message
news:#(E-Mail Removed)...
> 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
> "aualias" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
> >
> >

>



 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      05-26-2004
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...abindingExpres
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.)

 
Reply With Quote
 
aualias
Guest
Posts: n/a
 
      05-26-2004
Steven,

Thanks. This is very helpful.

Alfredo


"Steven Cheng[MSFT]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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...abindingExpres
> 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.)
>



 
Reply With Quote
 
aualias
Guest
Posts: n/a
 
      06-05-2004
Steven,

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

Alfredo


"Steven Cheng[MSFT]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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...abindingExpres
> 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.)
>



 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      06-09-2004
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/quickst...tabinding.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





 
Reply With Quote
 
aualias
Guest
Posts: n/a
 
      06-10-2004
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





"Steven Cheng[MSFT]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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...abindingExpres
> 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.)
>



 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      06-11-2004
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


 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      06-15-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
When do you call DataGrid.DataBind() Water Cooler v2 ASP .Net Datagrid Control 9 06-13-2006 09:02 PM
Can you still databind to a single object in ASP .Net 2.0? =?Utf-8?B?Sm9obiBCYWlsZXk=?= ASP .Net 3 03-29-2006 04:41 PM
Can you databind a textbox to a datasource? Charley Jones ASP .Net Web Controls 0 08-03-2005 10:16 PM
BUG in DataBind? After .DataBind there are more DataGrid Items than DataSet Rows! Michael ASP .Net Datagrid Control 0 12-29-2003 07:47 PM
ATTN Programmers: WE pay YOU $1.00 if you let us find you a job. jdg Doris Cox Perl 0 12-02-2003 11:07 AM



Advertisments