你如何数据绑定CheckBox? [英] How do you databind a CheckBox?

查看:53
本文介绍了你如何数据绑定CheckBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这看起来像是一个蹩脚的问题,但是如何用一个带有

ASP.NET的CheckBox绑定?使用Windows窗体,有一个DataBindings集合。我不用

看到它的控件的网页版本,但有一个DataBinding

事件。


谢谢。


AU

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

推荐答案

这是一个蹩脚的复选框控件。您只能将文本值

指定为字符串,并为Checked属性指定布尔值。


CheckBoxList更像常规控件,如图所示代码

以下。


Private Sub Page_Load _

(ByVal sender As System.Object,_

ByVal e As System.EventArgs)_

处理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


函数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

对于i = 0到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(博士)

下一页我

返回dt

结束功能

" aualias" < AU ***** @ newsgroups.nospam>在留言中写道

新闻:ei ************** @ 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...
这看起来像这样一个蹩脚的问题,但是如何用ASP.NET绑定到CheckBox
?使用Windows窗体,有一个DataBindings集合。我没有用控件的网页版本看到它,但有一个DataBinding
事件。

谢谢。

AU
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






谢谢Ken,


我想这就解释了为什么我找不到任何例子数据绑定到

CheckBox ...


一个问题,导致DataBinding事件触发的原因是什么?


Alfredo


" Ken Cox [Microsoft MVP]" < BA ************ @ sympatico.ca>在消息中写道

新闻:#K ************** @ TK2MSFTNGP12.phx.gbl ...
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...
这是复选框控制是蹩脚的。您只能将文本
值指定为字符串,并为Checked属性指定布尔值。

CheckBoxList更像常规控件,如下面的代码所示。

Private Sub Page_Load _
(ByVal sender As System.Object,_
ByVal e As System.EventArgs)_
处理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()<结束Sub

函数CreateDataSource()As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn(&) ; IntegerValue",GetTy pe(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
对于i = 0到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)
接下来我
返回dt
结束功能
aualias < AU ***** @ newsgroups.nospam>在消息中写道
新闻:ei ************** @ 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...
这看起来像是一个蹩脚的问题,但是如何用ASP.NET绑定到CheckBox
?使用Windows窗体,有一个DataBindings集合。我用
看不到控件的网页版本,但有一个DataBinding
活动。

谢谢。

AU
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



您好阿尔弗雷多,


实际上,WebForm中的DataBinding模式与在

Winform应用程序中。因为Winform应用程序在一台机器上运行并且所有运行时数据都存储并存储在内存中,所以控件的DataBinding项目会自动同步
/>
使用数据源,我们只需要设置数据源,但不需要手动b / b
。但是,在Web应用程序中,ASP.NET页面是基于请求/响应的
,页面对象及其包含的控件的实际

实例仅在请求时存在在服务器端处理。

这称为网页的生命周期,之后,页面呈现给客户端,页面和控件实例'' s lifecyle结束。所以那些

Page的Serverside事件,如Load,PreRender ..是唯一的地方

设置控件的成员/属性或用它们绑定数据。并且ASP.NET

服务器控件没有DataBindings。我们通过页面模板中的DataBinding Expression设置了他们的

数据绑定,例如

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

< / asp:TextBox>

< asp:CheckBox id =" CheckBox1" RUNAT = QUOT;服务器" Text ="<%#expression或

function%>">

< / asp:CheckBox>


然后,在页面'的事件中,例如Page_Load或其他事件,我们可以明确地告诉
调用Control的DataBind()方法来触发
的DataBinding事件。
控制,然后它将通过上述<%#

%>绑定控件的某些属性。表达式在页面模板中设置。


另外,我们可以调用Page.DataBind()方法,这会使页面触发

所有控件都有数据绑定表达式绑定数据。


这是关于Web表单数据绑定语法的参考:

#Data绑定表达式语法
http://msdn.microsoft.com/library/en...abindingExpres

sionSyntax.asp?frame = true


希望也很有帮助。谢谢。


问候,


Steven Cheng

微软在线支持


安全! www.microsoft.com/security

(此帖子按原样提供,不作任何保证,并且不授予

权利。)

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.)


这篇关于你如何数据绑定CheckBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆