Checkbox in template coloumn

Discussion in 'ASP .Net Datagrid Control' started by ClearConcepts, Jun 24, 2006.

  1. Hi,

    I am using checkbox in template column of datagrid in aspx page.

    I am fetching boolean field from database and binding to this template
    column.

    If the values is "true" checkbox should checkd. otherwise it should be
    unchecked.

    How to do this?

    Please suggest me

    --
    Regards
    ClearConcepts
     
    ClearConcepts, Jun 24, 2006
    #1
    1. Advertising

  2. Hi Clear (real names are considered more polite)

    You just need to set the checked value to the boolean. Here's the idea, full
    code below.

    <asp:templatecolumn headertext="Boolean">
    <itemtemplate>
    <asp:checkbox id="CheckBox1" runat="server"
    checked='<%# DataBinder.Eval(Container, "DataItem.Boolean") %>' />
    <br />
    </itemtemplate>

    Let us know if this helps?

    Ken
    Microsoft MVP [ASP.NET]

    <%@ Page Language="VB" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">

    Function CreateDataSource() As Data.DataTable
    Dim dt As New Data.DataTable
    Dim dr As Data.DataRow
    dt.Columns.Add(New Data.DataColumn _
    ("IntegerValue", GetType(Int32)))
    dt.Columns.Add(New Data.DataColumn _
    ("StringValue", GetType(String)))
    dt.Columns.Add(New Data.DataColumn _
    ("CurrencyValue", GetType(Double)))
    dt.Columns.Add(New Data.DataColumn _
    ("Boolean", GetType(Boolean)))
    Dim i As Integer
    For i = 0 To 5
    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

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArgs)
    If Not IsPostBack Then
    dg3.DataSource = CreateDataSource()
    dg3.DataBind()
    End If
    End Sub
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Check a checkbox</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:datagrid id="dg3" runat="server" autogeneratecolumns="False">
    <columns>
    <asp:templatecolumn headertext="Boolean">
    <itemtemplate>
    <asp:checkbox id="CheckBox1" runat="server"
    checked='<%# DataBinder.Eval(Container, "DataItem.Boolean") %>' />
    <br />
    </itemtemplate>
    </asp:templatecolumn>
    <asp:templatecolumn headertext="StringValue">
    <itemtemplate>
    <asp:label runat="server"
    text='<%# DataBinder.Eval(Container, "DataItem.StringValue") %>'>
    </asp:label>
    </itemtemplate>
    </asp:templatecolumn>
    </columns>
    </asp:datagrid>
    </div>
    </form>
    </body>
    </html>


    "ClearConcepts" <> wrote in message
    news:...
    >
    > Hi,
    >
    > I am using checkbox in template column of datagrid in aspx page.
    >
    > I am fetching boolean field from database and binding to this template
    > column.
    >
    > If the values is "true" checkbox should checkd. otherwise it should be
    > unchecked.
    >
    > How to do this?
    >
    > Please suggest me
    >
    > --
    > Regards
    > ClearConcepts
    >
    >
     
    Ken Cox [Microsoft MVP], Jun 25, 2006
    #2
    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. tshad
    Replies:
    0
    Views:
    552
    tshad
    Apr 15, 2005
  2. Vikram

    disable checkbox list checkbox

    Vikram, Jan 25, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    1,414
    ThunderMusic
    Jan 25, 2006
  3. Garg
    Replies:
    1
    Views:
    370
    Oliver Wong
    May 11, 2006
  4. YMPN
    Replies:
    2
    Views:
    299
    =?Utf-8?B?RWFnbGVSZWRASGlnaEZseWluZ0JpcmRzLmNvbQ==
    Jan 25, 2007
  5. Renjini
    Replies:
    2
    Views:
    339
    Ben Radford
    Jan 28, 2006
Loading...

Share This Page