DataGrid中加入CheckBox,並實現單選

來源:互聯網
上載者:User
datagrid 第一次發表,不會寫.直接把原檔案給大家看算了!!

原理就是用JS遍列所有CheckBox把所有CheckBox設為未被選中,然後在把單擊的CheckBox設為選中

以下是.ASPX檔案

<%@ 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="宋體">
<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="顯示內容"></asp:button></FONT></form>
</body>
</HTML>
以下是CS檔案

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>
/// 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)
{
// 在此處放置使用者代碼以初始化頁面
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;
}


#region Web Form設計器產生的程式碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調用是 ASP.NET Web Form設計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 設計器支援所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </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+"和"+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");
}
}



}
}



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。