<%@ Page Language="C#" AutoEventWireup="true" Codebehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>
<!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">
<head runat="server">
<title>未命名頁面</title>
<style type="text/css">
.hide{display:none;}
</style>
<script type="text/javascript"> //选中所有行
function SelectAll(chkAll)
{
var gridview = document.getElementById("GridView1");
if (gridview)
{
//获取到GridView1中的所有input标签
var inputs = gridview.getElementsByTagName("input");
for(var i=0;i<inputs.length;i++)
{
if (inputs[i].type=="checkbox")
{
//设置所有checkbox的选中状态与chkAll一致
inputs[i].checked = chkAll.checked;
}
}
}
}
//给选中行换背景色
function checkRow(chkRow)
{
var row = chkRow.parentNode.parentNode;
if(row)
{
if (chkRow.checked)
row.style.backgroundColor="#7799CC";
else
row.style.backgroundColor="#FFFFFF";
}
}
</script>
</head>
<body>
<form />
</asp:GridView>
</div>
</form>
</body>
</html>
-----------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) {
DataTable table = new DataTable();
table.Columns.Add(new DataColumn("ID"));
table.Columns.Add(new DataColumn("Name"));
table.Columns.Add(new DataColumn("UsuallyResults"));
table.Columns.Add(new DataColumn("ExamResults"));
DataRow row = table.NewRow();
table.Rows.Add(row);
GridView1.DataSource = table; GridView1.DataBind();
}
}
protected void lbtnAddRow_Click(object sender, EventArgs e)
{
DataTable table = GetGridViewData();
DataTable table1 = new DataTable();
table1.Columns.Add(new DataColumn("ID"));
table1.Columns.Add(new DataColumn("Name"));
table1.Columns.Add(new DataColumn("UsuallyResults"));
table1.Columns.Add(new DataColumn("ExamResults"));
DataRow newRow1 = table1.NewRow();
newRow1["ID"] = Guid.NewGuid().ToString();
table1.Rows.Add(newRow1);
// foreach (DataRow dr in table1.Rows)
// {
for(int i=0;i<table.Rows.Count;i++)
{
DataRow sourseRow = table1.NewRow();
sourseRow["ID"] =table.Rows[i]["ID"].ToString();
sourseRow["Name"] = table.Rows[i]["name"].ToString(); ;
sourseRow["UsuallyResults"] = "2";
sourseRow["ExamResults"] ="1";
table1.Rows.Add(sourseRow);
}
// }
GridView1.DataSource = table1;
GridView1.DataBind();
}
private DataTable GetGridViewData()
{
DataTable table = new DataTable();
table.Columns.Add(new DataColumn("ID"));
table.Columns.Add(new DataColumn("Name"));
table.Columns.Add(new DataColumn("UsuallyResults"));
table.Columns.Add(new DataColumn("ExamResults"));
foreach (GridViewRow row in GridView1.Rows)
{
DataRow sourseRow = table.NewRow();
sourseRow["ID"] = row.Cells[0].Text;
sourseRow["Name"] = ((TextBox)row.Cells[3].FindControl("txtName")).Text;
sourseRow["UsuallyResults"] = ((TextBox)row.Cells[4].FindControl("txtUsuallyResults")).Text;
sourseRow["ExamResults"] = ((TextBox)row.Cells[5].FindControl("txtExamResults")).Text;
table.Rows.Add(sourseRow);
}
return table;
}
protected void btnDeleteRow_Click(object sender, EventArgs e)
{ DataTable table = GetGridViewData();
foreach (GridViewRow row in GridView1.Rows)
{
if (((HtmlInputCheckBox)row.Cells[2].FindControl("chkRow")).Checked)
{ foreach (DataRow dtRow in table.Rows)
{ if (dtRow["ID"].ToString() == row.Cells[0].Text)
{
table.Rows.Remove(dtRow);
break;
}
}
}
}
GridView1.DataSource = table; GridView1.DataBind();
}
}
}