How do you databind a CheckBox?

Discussion in 'ASP .Net' started by aualias, May 25, 2004.

  1. aualias

    aualias Guest

    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
     
    aualias, May 25, 2004
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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 Cox [Microsoft MVP], May 26, 2004
    #2
    1. Advertising

  3. aualias

    aualias Guest

    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]" <> wrote in message
    news:#...
    > 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" <> wrote in message
    > news:...
    > > 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
    > >
    > >

    >
     
    aualias, May 26, 2004
    #3
  4. 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.)
     
    Steven Cheng[MSFT], May 26, 2004
    #4
  5. aualias

    aualias Guest

    Steven,

    Thanks. This is very helpful.

    Alfredo


    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > 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.)
    >
     
    aualias, May 26, 2004
    #5
  6. aualias

    aualias Guest

    Steven,

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

    Alfredo


    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > 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.)
    >
     
    aualias, Jun 5, 2004
    #6
  7. 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
     
    Steven Cheng[MSFT], Jun 9, 2004
    #7
  8. aualias

    aualias Guest

    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]" <> wrote in message
    news:...
    > 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.)
    >
     
    aualias, Jun 10, 2004
    #8
  9. 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
     
    Steven Cheng[MSFT], Jun 11, 2004
    #9
  10. 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 Cheng[MSFT], Jun 15, 2004
    #10
  11. aualias

    aualias Guest

    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]" <> wrote in message
    news:...
    > 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
    >
     
    aualias, Jun 15, 2004
    #11
  12. aualias

    npclaudiu

    Joined:
    Apr 29, 2011
    Messages:
    1
    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...
     
    npclaudiu, Apr 29, 2011
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jasonkester

    checkbox databind does not work

    jasonkester, Jan 4, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    6,191
  2. =?Utf-8?B?cm9kY2hhcg==?=

    checkbox databind

    =?Utf-8?B?cm9kY2hhcg==?=, Nov 10, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    482
    Mark Rae
    Nov 10, 2006
  3. SAL

    Databind checkbox

    SAL, Dec 29, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    423
  4. Michael
    Replies:
    0
    Views:
    233
    Michael
    Dec 29, 2003
  5. Jim

    checkbox databind does not work

    Jim, Jan 5, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    167
Loading...

Share This Page