controlling output state of checkbox using ASP.net and SQL server05

A

aspnoob

hello i just started off with asp.net , needed some help on how to display
the state of checkboxes from a SQL database.

my database table has the following format

seat.dbo ( db design)

( column 1 PK ) id : 1 2 3 4 5 6
(column 2 boolean) state : 1 , 0 , 1 , 0 , 1 , 0

1 stands for checked , 0 uncheck .


i wish to display this checkbox state in 2 rows of 3

o o o
o o o

e.g.

with the code querying the db for the status of the checkboxes before
displaying the output to the user.
if the checkbox 1 , then the user will not be able to uncheck the checkboxes.

( i guess this is more than enough for me , with this example i probably will
be able to figure out how the insert using the checkbox works)

anyone can help ?
 
G

Guest

Please find example below : ( i coded everything in page for clarity, use
code file or
code behind instead )

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs"
Inherits="Test" %>

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


<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
list.DataSource = GetRandomDataSource(10);
list.DataBind();
}
}

private Random random = new Random();
private System.Data.DataTable GetRandomDataSource(int count)
{
if (count < 0)
throw new ArgumentOutOfRangeException();

System.Data.DataTable table = new System.Data.DataTable();
System.Data.DataRow row = null;

table.Columns.Add("Id", typeof(int));
table.Columns.Add("Flag1", typeof(bool));
table.Columns.Add("Flag2", typeof(bool));
table.Columns.Add("Flag3", typeof(bool));

for (int i = 0; i < count; i++)
{
row = table.NewRow();

row[0] = i;
row[1] = this.random.NextDouble() >= 0.5d;
row[2] = this.random.NextDouble() >= 0.5d;
row[3] = this.random.NextDouble() >= 0.5d;

table.Rows.Add(row);
}

return table;
}
protected void list_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow gridRow = e.Row;
System.Data.DataRowView dataRow = (System.Data.DataRowView)
gridRow.DataItem;

if (gridRow.RowType == DataControlRowType.DataRow)
{
if ((bool)dataRow["Flag1"])
{
(gridRow.Cells[0].Controls[0] as WebControl).Enabled = true;
(gridRow.Cells[1].Controls[0] as WebControl).Enabled = true;
}
}
}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView runat="server" ID="list" AutoGenerateColumns="false"
OnRowDataBound="list_RowDataBound">
<Columns>
<asp:CheckBoxField DataField="Flag2"/>
<asp:CheckBoxField DataField="Flag3"/>
</Columns>
</asp:GridView>
</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

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top