<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<script language="C#" runat="server">
ICollection CreateDataSource()
{
DataView dbv = (DataView)SqlDataSource.Select(DataSourceSelectArguments.Empty);
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
for (int i = 0; i < dbv.Table.Rows.Count; i++)
{
dr = dt.NewRow();
dr[0] = dbv.Table.Rows[i][0];
dt.Rows.Add(dr);
}
return new DataView(dt);
}
void Page_Load(Object sender, EventArgs e)
{
if (!IsPostBack)
{
dataGrid1.DataSource = CreateDataSource();
dataGrid1.DataBind();
}
}
ArrayList Seleccionados()
{
ArrayList lista = new ArrayList();
DataGridItemCollection items = dataGrid1.Items;
for (int i = 0; i < items.Count; i++)
{
CheckBox cb = (CheckBox)items[i].FindControl("checkboxSelect");
if ((cb != null) && cb.Checked)
lista.Add( items[i].Cells[0].Text );
}
return lista;
}
void SubmitBtn_Click(Object sender, EventArgs e)
{
ArrayList lista = Seleccionados();
string text = "Seleccionados:<br />";
for (int i = 0; i < lista.Count; i++)
text += (lista[i] + "<br />");
Mensaje.Text = text;
}
</script>
<head runat="server">
<title>CheckBox</title>
</head>
<body>
<asp:SqlDataSource
id="SqlDataSource"
runat="server"
ConnectionString="<%$ ConnectionStrings:ApplicationServices%>"
SelectCommand="SELECT descripcion FROM datosGrid">
</asp:SqlDataSource>
<form id="Formulario" runat="server">
<h3>Seleccionar Datos</h3>
<table cellpadding="5">
<tr valign="top">
<td>
<b>Datos</b>
<asp:DataGrid id="dataGrid1"
BorderWidth="1"
AutoGenerateColumns="false"
runat="server">
<HeaderStyle BackColor="#006699" />
<Columns>
<asp:BoundColumn
HeaderText="Item"
DataField="StringValue"/>
<asp:TemplateColumn HeaderText="Seleccion" >
<ItemTemplate>
<asp:CheckBox ID="checkboxSelect" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</td>
</tr>
</table>
<asp:Button id="BotonEnviar"
Text="Verificar"
OnClick="SubmitBtn_Click"
runat="server"/>
</form>
<asp:label id="Mensaje" runat="server"/>
</body>
</html>