关于GridView中自定义分页、单选、多选的简单应用
2006-06-30 17:23 蓝之风 阅读(487) 评论(0) 编辑 收藏 举报
关于VS2005中GridView的自定义分页,单选、多选的简单应用。
前台代码;
前台代码;
1 简单示例,代码如下,
2 ExempGridView.aspx的代码:
3 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExempGridView.aspx.cs" Inherits="gridview_ExempGridView" %>
4 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5 <html xmlns="http://www.w3.org/1999/xhtml">
6 <head runat="server">
7 <title></title>
8 <script language="javascript" type="text/javascript">
9 // 全部选中
10 function QuanXuan_Click()
11 {
12 if (document.form1.checkboxname.length)
13 {
14 for (var i=0;i<document.form1.checkboxname.length;i++)
15 {
16 document.form1.checkboxname[i].checked = true;
17 }
18 }
19 else
20 {
21 document.form1.checkboxname.checked = true;
22 }
23 }
24
25 // 取消选中
26 function QuXiao_Click()
27 {
28 if (document.form1.checkboxname.length)
29 {
30 for (var i=0;i<document.form1.checkboxname.length;i++)
31 {
32 document.form1.checkboxname[i].checked = false;
33 }
34 }
35 else
36 {
37 document.form1.checkboxname.checked = false;
38 }
39 }
40
41 // 判断没有选中的返回false
42 function slcNo_click()
43 {
44 if (document.form1.checkboxname.length)
45 {
46 for (var i=0;i<document.form1.checkboxname.length;i++)
47 {
48 if(document.form1.checkboxname[i].checked)
49 {
50 return true;
51 }
52 }
53 }
54 else
55 {
56 if(document.form1.checkboxname.checked)
57 {
58 return true;
59 }
60 }
61 alert("请选择后再操作!");
62 return false;
63 }
64
65 // 改变行的颜色
66 if (!objbeforeItem)
67 {
68 var objbeforeItem=null;
69 var objbeforeItembackgroundColor=null;
70 }
71
72 function ItemOver(obj)
73 {
74 objbeforeItembackgroundColor=obj.style.backgroundColor;
75 obj.style.backgroundColor="#B9D1F3";
76 objbeforeItem=obj;
77 }
78
79 function ItemOut(obj)
80 {
81 if(objbeforeItem)
82 {
83 objbeforeItem.style.backgroundColor=objbeforeItembackgroundColor;
84 }
85 }
86 </script>
87 </head>
88 <body>
89 <form id="form1" runat="server">
90 <div>
91 <table cellpadding="0" cellspacing="0" border="0" width="60%" style="font-size: 11px">
92 <tr>
93 <td align="center">
94 <asp:GridView ID="GridView1" runat="server" Width="100%" CellPadding="4" ForeColor="#333333"
95 AutoGenerateColumns="False" AllowPaging="True" PageSize="12" BorderColor="Silver"
96 BorderStyle="Solid" BorderWidth="1px" OnRowDataBound="GridView1_RowDataBound">
97 <Columns>
98 <asp:BoundField HeaderText="编号" DataField="id" Visible="false" />
99 <asp:TemplateField HeaderText="多选">
100 <ItemTemplate>
101 <input type="checkbox" id="checkboxname" name="checkboxname" value='<%# DataBinder.Eval(Container.DataItem, "id")%>' />
102 </ItemTemplate>
103 </asp:TemplateField>
104 <asp:TemplateField HeaderText="单选">
105 <ItemTemplate>
106 <input type="radio" id="RadioName" name="RadioName" value='<%# DataBinder.Eval(Container.DataItem, "id")%>'/>
107 </ItemTemplate>
108 </asp:TemplateField>
109 <asp:BoundField HeaderText="姓名" DataField="name" />
110 <asp:BoundField HeaderText="身份证号" DataField="card" />
111 <asp:BoundField HeaderText="建立时间" DataField="createdate" />
112 </Columns>
113 <PagerSettings Visible="False" />
114 </asp:GridView>
115 </td>
116 </tr>
117 <tr>
118 <td align="center" style="height: 25px">
119 <asp:LinkButton ID="btnFirst" CommandArgument="first" OnClick="PagerButtonClick"
120 runat="server">首 页</asp:LinkButton>
121 <asp:LinkButton ID="btnPrev" CommandArgument="prev" OnClick="PagerButtonClick" runat="server">上一页</asp:LinkButton>
122 <asp:LinkButton ID="btnNext" CommandArgument="next" OnClick="PagerButtonClick" runat="server">下一页</asp:LinkButton>
123 <asp:LinkButton ID="btnLast" CommandArgument="last" OnClick="PagerButtonClick" runat="server">尾 页</asp:LinkButton>
124 <asp:Label ID="LblCurrentIndex" runat="server"></asp:Label>
125 <asp:Label ID="LblPageCount" runat="server"></asp:Label>
126 <asp:Label ID="LblRecordCount" runat="server"></asp:Label>
127 <asp:Label ID="LblNoRecord" runat="server" Text="记录为零" Visible="False"></asp:Label></td>
128 </tr>
129 <tr>
130 <td>
131 <input type="button" value="全部选中" onclick="QuanXuan_Click()" />
132 <input type="button" value="取消选中" onclick="QuXiao_Click()" />
133 <asp:Button ID="Button2" runat="server" Text="checkbox得到选择的行" OnClick="Button2_Click"></asp:Button>
134 <asp:Button id="Button1" runat="server" Text="radio得到选择的行" OnClick="Button1_Click"></asp:Button>
135 </td>
136 </tr>
137 </table>
138 </div>
139 </form>
140 </body>
141 </html>
2 ExempGridView.aspx的代码:
3 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExempGridView.aspx.cs" Inherits="gridview_ExempGridView" %>
4 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5 <html xmlns="http://www.w3.org/1999/xhtml">
6 <head runat="server">
7 <title></title>
8 <script language="javascript" type="text/javascript">
9 // 全部选中
10 function QuanXuan_Click()
11 {
12 if (document.form1.checkboxname.length)
13 {
14 for (var i=0;i<document.form1.checkboxname.length;i++)
15 {
16 document.form1.checkboxname[i].checked = true;
17 }
18 }
19 else
20 {
21 document.form1.checkboxname.checked = true;
22 }
23 }
24
25 // 取消选中
26 function QuXiao_Click()
27 {
28 if (document.form1.checkboxname.length)
29 {
30 for (var i=0;i<document.form1.checkboxname.length;i++)
31 {
32 document.form1.checkboxname[i].checked = false;
33 }
34 }
35 else
36 {
37 document.form1.checkboxname.checked = false;
38 }
39 }
40
41 // 判断没有选中的返回false
42 function slcNo_click()
43 {
44 if (document.form1.checkboxname.length)
45 {
46 for (var i=0;i<document.form1.checkboxname.length;i++)
47 {
48 if(document.form1.checkboxname[i].checked)
49 {
50 return true;
51 }
52 }
53 }
54 else
55 {
56 if(document.form1.checkboxname.checked)
57 {
58 return true;
59 }
60 }
61 alert("请选择后再操作!");
62 return false;
63 }
64
65 // 改变行的颜色
66 if (!objbeforeItem)
67 {
68 var objbeforeItem=null;
69 var objbeforeItembackgroundColor=null;
70 }
71
72 function ItemOver(obj)
73 {
74 objbeforeItembackgroundColor=obj.style.backgroundColor;
75 obj.style.backgroundColor="#B9D1F3";
76 objbeforeItem=obj;
77 }
78
79 function ItemOut(obj)
80 {
81 if(objbeforeItem)
82 {
83 objbeforeItem.style.backgroundColor=objbeforeItembackgroundColor;
84 }
85 }
86 </script>
87 </head>
88 <body>
89 <form id="form1" runat="server">
90 <div>
91 <table cellpadding="0" cellspacing="0" border="0" width="60%" style="font-size: 11px">
92 <tr>
93 <td align="center">
94 <asp:GridView ID="GridView1" runat="server" Width="100%" CellPadding="4" ForeColor="#333333"
95 AutoGenerateColumns="False" AllowPaging="True" PageSize="12" BorderColor="Silver"
96 BorderStyle="Solid" BorderWidth="1px" OnRowDataBound="GridView1_RowDataBound">
97 <Columns>
98 <asp:BoundField HeaderText="编号" DataField="id" Visible="false" />
99 <asp:TemplateField HeaderText="多选">
100 <ItemTemplate>
101 <input type="checkbox" id="checkboxname" name="checkboxname" value='<%# DataBinder.Eval(Container.DataItem, "id")%>' />
102 </ItemTemplate>
103 </asp:TemplateField>
104 <asp:TemplateField HeaderText="单选">
105 <ItemTemplate>
106 <input type="radio" id="RadioName" name="RadioName" value='<%# DataBinder.Eval(Container.DataItem, "id")%>'/>
107 </ItemTemplate>
108 </asp:TemplateField>
109 <asp:BoundField HeaderText="姓名" DataField="name" />
110 <asp:BoundField HeaderText="身份证号" DataField="card" />
111 <asp:BoundField HeaderText="建立时间" DataField="createdate" />
112 </Columns>
113 <PagerSettings Visible="False" />
114 </asp:GridView>
115 </td>
116 </tr>
117 <tr>
118 <td align="center" style="height: 25px">
119 <asp:LinkButton ID="btnFirst" CommandArgument="first" OnClick="PagerButtonClick"
120 runat="server">首 页</asp:LinkButton>
121 <asp:LinkButton ID="btnPrev" CommandArgument="prev" OnClick="PagerButtonClick" runat="server">上一页</asp:LinkButton>
122 <asp:LinkButton ID="btnNext" CommandArgument="next" OnClick="PagerButtonClick" runat="server">下一页</asp:LinkButton>
123 <asp:LinkButton ID="btnLast" CommandArgument="last" OnClick="PagerButtonClick" runat="server">尾 页</asp:LinkButton>
124 <asp:Label ID="LblCurrentIndex" runat="server"></asp:Label>
125 <asp:Label ID="LblPageCount" runat="server"></asp:Label>
126 <asp:Label ID="LblRecordCount" runat="server"></asp:Label>
127 <asp:Label ID="LblNoRecord" runat="server" Text="记录为零" Visible="False"></asp:Label></td>
128 </tr>
129 <tr>
130 <td>
131 <input type="button" value="全部选中" onclick="QuanXuan_Click()" />
132 <input type="button" value="取消选中" onclick="QuXiao_Click()" />
133 <asp:Button ID="Button2" runat="server" Text="checkbox得到选择的行" OnClick="Button2_Click"></asp:Button>
134 <asp:Button id="Button1" runat="server" Text="radio得到选择的行" OnClick="Button1_Click"></asp:Button>
135 </td>
136 </tr>
137 </table>
138 </div>
139 </form>
140 </body>
141 </html>
1//ExempGridView.aspx.cs的代码:
2using System;
3using System.Data;
4using System.Configuration;
5using System.Collections;
6using System.Web;
7using System.Web.Security;
8using System.Web.UI;
9using System.Web.UI.WebControls;
10using System.Web.UI.WebControls.WebParts;
11using System.Web.UI.HtmlControls;
12using System.Data.SqlClient;
13
14public partial class ExempGridView: System.Web.UI.Page
15{
16 protected void Page_Load(object sender, EventArgs e)
17 {
18 GridViewBind();
19 }
20
21 private void GridViewBind()
22 {
23 string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
24 string SqlStr = "SELECT * FROM test01";
25 DataSet ds = new DataSet();
26
27 try
28 {
29 SqlConnection conn = new SqlConnection(connStr);
30 if (conn.State.ToString() == "Closed") conn.Open();
31 SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
32 da.Fill(ds, "test01");
33 if (conn.State.ToString() == "Open") conn.Close();
34
35 GridView1.DataSource = ds.Tables[0].DefaultView;
36 GridView1.DataBind();
37
38 LblCurrentIndex.Text = "第 " + (GridView1.PageIndex + 1).ToString() + " 页";
39 LblPageCount.Text = "共 " + GridView1 .PageCount.ToString()+ " 页";
40 LblRecordCount.Text = "总共 "+ds.Tables[0].Rows.Count.ToString()+" 条";
41 if (ds.Tables[0].Rows.Count == 0)
42 {
43 btnFirst.Visible = false;
44 btnPrev.Visible = false;
45 btnNext.Visible = false;
46 btnLast.Visible = false;
47
48 LblCurrentIndex.Visible = false;
49 LblPageCount.Visible = false;
50 LblRecordCount.Visible = false;
51
52 LblNoRecord.Visible = true;
53 }
54 else if (GridView1.PageCount == 1)
55
61 }
62 catch(Exception ex)
63
67 }
68 protected void PagerButtonClick(object sender, EventArgs e)
69 {
70 string arg = ((LinkButton)sender).CommandArgument.ToString();
71 switch (arg)
72 {
73 case "prev":
74 if (GridView1.PageIndex > 0)
75 {
76 GridView1.PageIndex -= 1;
77 }
78 break;
79 case "next":
80 if (GridView1.PageIndex < (GridView1.PageCount - 1))
81 {
82 GridView1.PageIndex += 1;
83 }
84 break;
85 case "last":
86 GridView1.PageIndex = (GridView1.PageCount - 1);
87 break;
88 default:
89 GridView1.PageIndex = 0;
90 break;
91 }
92
93 GridViewBind();
94 }
95
96 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
97 {
98 e.Row.Attributes["onmouseover"] = "ItemOver(this)";
99 e.Row.Attributes["onmouseout"] = "ItemOut(this)";
100 }
101
102 protected void Button2_Click(object sender, EventArgs e)
103 {
104 string str="";
105 string []ckb=null;
106
107 str=Request.Form.Get("checkboxname");
108 ckb=str.Split(new char[]{','});
109
110 Response.Write("直接在页面中得到的值为:"+str+"<br>");
111
112 Response.Write("处理后存放在数组中,如下:<br>");
113 for(int i=0;i<ckb.Length;i++)
114 {
115 Response.Write("ckb["+i+"]的值为:"+ckb[i]+"<br>");
116 }
117 }
118 protected void Button1_Click(object sender, EventArgs e)
119 {
120 Response.Write(Request.Form.Get("RadioName"));
121 }
122}
123
2using System;
3using System.Data;
4using System.Configuration;
5using System.Collections;
6using System.Web;
7using System.Web.Security;
8using System.Web.UI;
9using System.Web.UI.WebControls;
10using System.Web.UI.WebControls.WebParts;
11using System.Web.UI.HtmlControls;
12using System.Data.SqlClient;
13
14public partial class ExempGridView: System.Web.UI.Page
15{
16 protected void Page_Load(object sender, EventArgs e)
17 {
18 GridViewBind();
19 }
20
21 private void GridViewBind()
22 {
23 string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
24 string SqlStr = "SELECT * FROM test01";
25 DataSet ds = new DataSet();
26
27 try
28 {
29 SqlConnection conn = new SqlConnection(connStr);
30 if (conn.State.ToString() == "Closed") conn.Open();
31 SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
32 da.Fill(ds, "test01");
33 if (conn.State.ToString() == "Open") conn.Close();
34
35 GridView1.DataSource = ds.Tables[0].DefaultView;
36 GridView1.DataBind();
37
38 LblCurrentIndex.Text = "第 " + (GridView1.PageIndex + 1).ToString() + " 页";
39 LblPageCount.Text = "共 " + GridView1 .PageCount.ToString()+ " 页";
40 LblRecordCount.Text = "总共 "+ds.Tables[0].Rows.Count.ToString()+" 条";
41 if (ds.Tables[0].Rows.Count == 0)
42 {
43 btnFirst.Visible = false;
44 btnPrev.Visible = false;
45 btnNext.Visible = false;
46 btnLast.Visible = false;
47
48 LblCurrentIndex.Visible = false;
49 LblPageCount.Visible = false;
50 LblRecordCount.Visible = false;
51
52 LblNoRecord.Visible = true;
53 }
54 else if (GridView1.PageCount == 1)
55
61 }
62 catch(Exception ex)
63
67 }
68 protected void PagerButtonClick(object sender, EventArgs e)
69 {
70 string arg = ((LinkButton)sender).CommandArgument.ToString();
71 switch (arg)
72 {
73 case "prev":
74 if (GridView1.PageIndex > 0)
75 {
76 GridView1.PageIndex -= 1;
77 }
78 break;
79 case "next":
80 if (GridView1.PageIndex < (GridView1.PageCount - 1))
81 {
82 GridView1.PageIndex += 1;
83 }
84 break;
85 case "last":
86 GridView1.PageIndex = (GridView1.PageCount - 1);
87 break;
88 default:
89 GridView1.PageIndex = 0;
90 break;
91 }
92
93 GridViewBind();
94 }
95
96 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
97 {
98 e.Row.Attributes["onmouseover"] = "ItemOver(this)";
99 e.Row.Attributes["onmouseout"] = "ItemOut(this)";
100 }
101
102 protected void Button2_Click(object sender, EventArgs e)
103 {
104 string str="";
105 string []ckb=null;
106
107 str=Request.Form.Get("checkboxname");
108 ckb=str.Split(new char[]{','});
109
110 Response.Write("直接在页面中得到的值为:"+str+"<br>");
111
112 Response.Write("处理后存放在数组中,如下:<br>");
113 for(int i=0;i<ckb.Length;i++)
114 {
115 Response.Write("ckb["+i+"]的值为:"+ckb[i]+"<br>");
116 }
117 }
118 protected void Button1_Click(object sender, EventArgs e)
119 {
120 Response.Write(Request.Form.Get("RadioName"));
121 }
122}
123
1--数据库中表的生成代码:
2 CREATE TABLE [dbo].[test01]
3(
4 [id] [decimal](18, 0) IDENTITY (1, 1) NOT NULL ,
5 [name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
6 [card] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
7 [createdate] [datetime] NULL
8 ) ON [PRIMARY]
9 GO
10
11 ALTER TABLE [dbo].[test01] ADD
12 CONSTRAINT [DF_test01_createdate] DEFAULT (getdate()) FOR [createdate],
13 CONSTRAINT [PK_test01] PRIMARY KEY CLUSTERED
14 (
15 [id]
16 ) ON [PRIMARY]
17 GO
18
2 CREATE TABLE [dbo].[test01]
3(
4 [id] [decimal](18, 0) IDENTITY (1, 1) NOT NULL ,
5 [name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
6 [card] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
7 [createdate] [datetime] NULL
8 ) ON [PRIMARY]
9 GO
10
11 ALTER TABLE [dbo].[test01] ADD
12 CONSTRAINT [DF_test01_createdate] DEFAULT (getdate()) FOR [createdate],
13 CONSTRAINT [PK_test01] PRIMARY KEY CLUSTERED
14 (
15 [id]
16 ) ON [PRIMARY]
17 GO
18
作者:蓝之风
出处:http://www.cnblogs.com/vaiyanzi/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。