Checkbox in template coloumn

C

ClearConcepts

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
 
K

Ken Cox [Microsoft MVP]

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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top