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 10 11576
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" <au*****@newsgroups.nospam> wrote in message
news:ei**************@TK2MSFTNGP12.phx.gbl... 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
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]" <BA************@sympatico.ca> wrote in message
news:#K**************@TK2MSFTNGP12.phx.gbl... 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" <au*****@newsgroups.nospam> wrote in message news:ei**************@TK2MSFTNGP12.phx.gbl... 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
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.)
Steven,
Thanks. This is very helpful.
Alfredo
"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:KN**************@cpmsftngxa10.phx.gbl... 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.)
Steven,
I have one quick question. Can you databind a TextBox? If so, how.
Alfredo
"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:KN**************@cpmsftngxa10.phx.gbl... 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.)
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
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]" <v-******@online.microsoft.com> wrote in message
news:KN**************@cpmsftngxa10.phx.gbl... 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.)
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
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
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
"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:cP**************@cpmsftngxa10.phx.gbl... 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Dave |
last post by:
How do you databind a checkbox, "checkcontrol", control to a field in a
table, "MON". given that the dtr of the table is "somedatasource"?
This doesn't work.
checkcontrol.DataSource=...
|
by: Marty McDonald |
last post by:
I have <asp:Table... </asp:Table> on my webform. In codebehind, I populate
a DataTable whose data should appear in the asp:Table. I created my own
code to populate the asp:Table with the...
|
by: Jim |
last post by:
i am trying to bind a checkbox to a tablecolumn available (integer)
i get an error:
'The best overloaded method match for Company.Customers.fbool(int)' has some
invalid arguments'
...
|
by: LU |
last post by:
I want to add dropdown boxes based on a databind to a checkboxlist.
I database/databind to a checkboxlist but for each checkbox created I want
to also create a drop down next to it with some...
|
by: Rourke Eleven |
last post by:
I have looked and searched. What good is the databind property on
Radiobuttons? How does one go about actually using it? What is a good
resource on this? I understand that I can easily get/set...
|
by: rodchar |
last post by:
hey all,
this is just a for instance, but what's the major difference between the
events databinding and databound?
thanks,
rodchar
|
by: SAL |
last post by:
Hello,
When I try to use the DataBindings property in the properties window for a
web form checkbox, it changes the Text property also.
So, let's say I have chkbxMycheckBox with the text property...
|
by: jeremy |
last post by:
Had a tough time figuring this one out and couldn't find a good
solution, so I thought I would post this and hopefully it will help
someone out.
When using DataBind to dynamically bind a list to...
|
by: =?Utf-8?B?cmxt?= |
last post by:
I have standard gridview in an updatePanel with an AJAX timer that refreshes
the data every 5 seconds by calling the grids DataBind method in the tick
event. This works great.
However, I have a...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |