Add a checkbox to the DataGrid and implement a single selection

Source: Internet
Author: User
The DataGrid was published for the first time and could not be written. Direct the original document to everyone to see forget!!

The principle is to use JS over all the checkbox to set all the checkbox is not selected, and then set the Click CheckBox to select

Here's the. aspx file

<%@ Page language= "C #" codebehind= "WebForm1.aspx.cs" autoeventwireup= "false" inherits= "Datagridcheck.webform1"% >
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content= "Microsoft Visual Studio. NET 7.1" name= "generator" >
<meta content= "C #" Name= "Code_language" >
<meta content= "JavaScript" name= "vs_defaultClientScript" >
<meta content= "http://schemas.microsoft.com/intellisense/ie5" name= "Vs_targetschema" >
<script language= "JavaScript" >
function Setcheckboxstate ()
{
var Dom=document.all;
var el=event.srcelement;
if (el.tagname== "INPUT" &&el.type.tolowercase () = = "checkbox")
{
for (i=0;i<dom.length;i++)
{
if (dom[i].tagname== "INPUT" &&dom[i].type.tolowercase () = = "checkbox")
{
Dom[i].checked=false;
}
}
}
el.checked=!el.checked;
}
</SCRIPT>
</HEAD>
<body ms_positioning= "GridLayout" >
<form id= "Form1" method= "POST" runat= "Server" >
<font face= "Song Body" >
<asp:datagrid id= "DG" style= "Z-INDEX:101; left:168px; Position:absolute; top:40px "runat=" Server "
Width= "440px" autogeneratecolumns= "False" >
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:checkbox id= "Chkexport" runat= "Server" ></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:boundcolumn datafield= "IntegerValue" ></asp:BoundColumn>
<asp:boundcolumn datafield= "StringValue" ></asp:BoundColumn>
<asp:boundcolumn datafield= "Currencyvalue" ></asp:BoundColumn>
</Columns>
</asp:datagrid><asp:button id= "Button1" style= "z-index:102; left:168px; Position:absolute; top:8px "runat=" Server "
text= "Show content" ></asp:button></FONT></form>
</body>
</HTML>
The following is the CS file

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;
Namespace Datagridcheck
{
<summary>
Summary description of the WebForm1.
</summary>
public class WebForm1:System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
Protected System.Web.UI.WebControls.DataGrid DG;

private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
if (! Page.IsPostBack)
{
Dg. Datasource= CreateDataSource ();
Dg. DataBind ();
}


}
ICollection CreateDataSource ()
{
DataTable dt = new DataTable ();
DataRow Dr;

Dt. Columns.Add (New DataColumn ("IntegerValue", typeof (Int32));
Dt. Columns.Add (New DataColumn ("StringValue", typeof (String));
Dt. Columns.Add (New DataColumn ("Currencyvalue", typeof (Double));

for (int i = 0; i < 9; i++)
{
Dr = dt. NewRow ();

Dr[0] = i;
DR[1] = "Item" + i.tostring ();
DR[2] = 1.23 * (i + 1);

Dt. Rows.Add (DR);
}

DataView dv = new DataView (DT);
return DV;
}


Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This.dg.ItemDataBound + = new System.Web.UI.WebControls.DataGridItemEventHandler (this.dg_itemdatabound);
This. Button1.Click + = new System.EventHandler (this. Button1_Click);
This. Load + = new System.EventHandler (this. Page_Load);

}
#endregion

private void Button1_Click (object sender, System.EventArgs e)
{
System.Web.UI.WebControls.CheckBox Chkexport;
foreach (DataGridItem dgitem in DG. Items)
{
chkexport= (checkbox) Dgitem.findcontrol ("Chkexport");
if (chkexport.checked)
{
Response.Write ("<script>alert" ("+dgitem.cells[2"). Text+ "and" +dgitem.cells[3]. text+ "') </script>");

}
}
}

private void Dg_itemdatabound (object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.item.itemindex<0) return;
if (E.item.itemtype = = ListItemType.Item | | e.item.itemtype = = listitemtype.alternatingitem)
{
System.Web.UI.WebControls.CheckBox Chkexport;
chkexport= (checkbox) E.item.findcontrol ("Chkexport");
CHKEXPORT.ATTRIBUTES.ADD ("OnClick", "Setcheckboxstate ()");
E.item.attributes.add ("onmouseover", "currentcolor=this.style.backgroundcolor;this.style.backgroundcolor=") 48d1cc ' ");
E.item.attributes.add ("onmouseout", "This.style.backgroundcolor=currentcolor");
}
}



}
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.