关于.NET2.0中GridView的一些使用
<1>将某行的单列值以不同色标记显示
1
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
2
{
3
if (e.Row.RowType == DataControlRowType.DataRow)
4
{
5
if (e.Row.Cells[8].Text == "USA")
6
{
7
//e.Row.BackColor = System.Drawing.Color.Red;
8
e.Row.Cells[8].BackColor = System.Drawing.Color.Red;
9
}
10
}
11
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)2
{3
if (e.Row.RowType == DataControlRowType.DataRow)4
{5
if (e.Row.Cells[8].Text == "USA")6
{7
//e.Row.BackColor = System.Drawing.Color.Red;8
e.Row.Cells[8].BackColor = System.Drawing.Color.Red;9
}10
}11
}<2>实现将鼠标移到行上时,行显示为不同色,移走时,恢复原色
1
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
2
{
3
if (e.Row.RowType == DataControlRowType.DataRow )
4
{
5
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#C0C0FF';this.style.cursor='hand';");
6
//当鼠标移走时还原该行的背景色
7
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
8
}
9
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)2
{3
if (e.Row.RowType == DataControlRowType.DataRow )4
{5
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#C0C0FF';this.style.cursor='hand';");6
//当鼠标移走时还原该行的背景色7
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");8
}9
}<3>删除时,弹出确认框!
在GridView中加入模板列,在 ItemTemplate 中加入一个LinkButton1,在其OnClientClick属性中加入:return confirm("确认要删除吗?")即可!
<4>加入全选列,并在标头选择全选时,GridView中的行全部选中
在GridView中加入模板列,在 ItemTemplate 中加入一个CheckBox1,在 HeadTemplate中加入第二个CheckBox2,并输入"选择全部",在CheckBox2的CheckedChanged事件中加入如下代码:
1
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
2
{
3
int i;
4
if (((CheckBox)sender).Checked)
5
{
6
for (i = 0; i < GridView1.Rows.Count; i++)
7
{
8
((CheckBox)GridView1.Rows[i].FindControl("CheckBox1")).Checked = true;
9
}
10
}
11
else
12
{
13
for (i = 0; i < GridView1.Rows.Count; i++)
14
{
15
((CheckBox)GridView1.Rows[i].FindControl("CheckBox1")).Checked =false;
16
}
17
}
18
}
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)2
{3
int i;4
if (((CheckBox)sender).Checked)5
{6
for (i = 0; i < GridView1.Rows.Count; i++)7
{8
((CheckBox)GridView1.Rows[i].FindControl("CheckBox1")).Checked = true;9
}10
}11
else12
{13
for (i = 0; i < GridView1.Rows.Count; i++)14
{15
((CheckBox)GridView1.Rows[i].FindControl("CheckBox1")).Checked =false;16
}17
}18
} <5>从页面1中打开页面2,并在页面2中选中哪行时,将值传回给页面1显示
先新建一个HTML页面,加入以下代码
1
<head>
2
<script language=javascript>
3
function openpage(htmlurl)
4
{
5
var newwin=window.open(htmlurl,"newWin","toolbar=no,location=no,directories=no,status=no,scrollbars=yes,menubar=no,resizable=yes,top=100,left=200,width=650,height=300");
6
newwin.focus();
7
return false;
8
}
9
</script>
10
</head>
11
<body>
12
<input type=text id="name" />
13
在按钮中调用:
14
<input type=button value="调用" onclick="return openpage('GridViewClientClick.aspx');" />
15
</body>
16
</html>
17
<head>2
<script language=javascript>3
function openpage(htmlurl) 4
{5
var newwin=window.open(htmlurl,"newWin","toolbar=no,location=no,directories=no,status=no,scrollbars=yes,menubar=no,resizable=yes,top=100,left=200,width=650,height=300");6
newwin.focus();7
return false;8
}9
</script>10
</head>11
<body>12
<input type=text id="name" />13
在按钮中调用:14
<input type=button value="调用" onclick="return openpage('GridViewClientClick.aspx');" />15
</body>16
</html>17

再新建一个ASPX页面,HTML页面处主要注意以下代码中的两个function--因其它是绑定数据的代码:
1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewClientClick.aspx.cs" Inherits="GridViewClientClick" %>
2
3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5
<html xmlns="http://www.w3.org/1999/xhtml" >
6
<head runat="server">
7
<script language=javascript>
8
function GridViewItemKeyDownEvent(d)
9
{
10
window.alert("事件类型: GridViewItemKeyDownEvent 作用对象: " + d);
11
}
12
function ReKey(k)
13
{
14
window.opener.document.getElementById('name').value=k;
15
window.close();
16
}
17
</script>
18
</head>
19
<body>
20
<form id="form1" runat="server">
21
<div>
22
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
23
SelectCommand="SELECT * FROM [Customers]"></asp:SqlDataSource>
24
25
</div>
26
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
27
DataKeyNames="CustomerID" DataSourceID="SqlDataSource1" OnRowCreated="GridView1_RowCreated"
28
OnRowDataBound="GridView1_RowDataBound">
29
<Columns>
30
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
31
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
32
<asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />
33
<asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" SortExpression="ContactTitle" />
34
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
35
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
36
<asp:BoundField DataField="Region" HeaderText="Region" SortExpression="Region" />
37
<asp:BoundField DataField="PostalCode" HeaderText="PostalCode" SortExpression="PostalCode" />
38
<asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
39
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
40
<asp:BoundField DataField="Fax" HeaderText="Fax" SortExpression="Fax" />
41
</Columns>
42
</asp:GridView>
43
</form>
44
</body>
45
</html>
46
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewClientClick.aspx.cs" Inherits="GridViewClientClick" %>2

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

5
<html xmlns="http://www.w3.org/1999/xhtml" >6
<head runat="server">7
<script language=javascript>8
function GridViewItemKeyDownEvent(d)9
{10
window.alert("事件类型: GridViewItemKeyDownEvent 作用对象: " + d); 11
}12
function ReKey(k)13
{14
window.opener.document.getElementById('name').value=k;15
window.close();16
}17
</script>18
</head>19
<body>20
<form id="form1" runat="server">21
<div>22
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"23
SelectCommand="SELECT * FROM [Customers]"></asp:SqlDataSource>24
25
</div>26
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"27
DataKeyNames="CustomerID" DataSourceID="SqlDataSource1" OnRowCreated="GridView1_RowCreated"28
OnRowDataBound="GridView1_RowDataBound">29
<Columns>30
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />31
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />32
<asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />33
<asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" SortExpression="ContactTitle" />34
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />35
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />36
<asp:BoundField DataField="Region" HeaderText="Region" SortExpression="Region" />37
<asp:BoundField DataField="PostalCode" HeaderText="PostalCode" SortExpression="PostalCode" />38
<asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />39
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />40
<asp:BoundField DataField="Fax" HeaderText="Fax" SortExpression="Fax" />41
</Columns>42
</asp:GridView>43
</form>44
</body>45
</html>46

aspx文件后台.cs文件中请加入:
1
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
2
{
3
if(e.Row.RowType==DataControlRowType.DataRow)
4
{
5
e.Row.Attributes.Add("ondblclick", "ReKey('" + e.Row.Cells[2].Text+"')");
6
//e.Row.Attributes["style"] = "Cursor:hand";
7
// //键盘事件
8
//e.Row.Attributes.Add("OnKeyDown", "GridViewItemKeyDownEvent('" + e.Row.Cells[1].Text + "')");
9
}
10
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)2
{3
if(e.Row.RowType==DataControlRowType.DataRow)4
{5
e.Row.Attributes.Add("ondblclick", "ReKey('" + e.Row.Cells[2].Text+"')");6
//e.Row.Attributes["style"] = "Cursor:hand"; 7
// //键盘事件8
//e.Row.Attributes.Add("OnKeyDown", "GridViewItemKeyDownEvent('" + e.Row.Cells[1].Text + "')");9
}10
}说明,在aspx页面中,您可以自己绑定一些数据,然后,传到第一个HTML页面中,此处的代码可能会因数据无法调用而无法运行...此代码中,有关于键盘操作的事件,只是简单的弹出按下的键。
以上是看了一些视频后收集到的,还有关于图片与GridView绑定,与CheckBoxList的绑定,会有单独一篇文章记录。

浙公网安备 33010602011771号