[ASP.NET]支持up,down以及pageup,pagedown,home,end,Enter键盘操作的DataGrid

一下代码可以实现弹出一个DataGrid窗口,该窗口支持up,down以及pageup,pagedown,home,end,Enter键盘操作,在按下Enter键后将选中的值返回初始窗口的TextBox1中。

![](http://dev.csdn.net/article/56/C:/Documents and Settings\Administrator\桌面\index.jpg)

webform1.aspx

1@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="ComplexTest.WebForm1" 
  1<html>
  2<head>
  3<title>WebForm1</title>
  4<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"/>
  5<meta content="C#" name="CODE_LANGUAGE"/>
  6<meta content="JavaScript" name="vs_defaultClientScript"/>
  7<meta content="  http://schemas.microsoft.com/intellisense/ie5  " name="vs_targetSchema"/>
  8<script language="javascript">   
  9var oldrow   
 10var currowno;   
 11var strReturn; //存储返回值   
 12var newColor='#C0C0C0';   
 13var oldColor;   
 14var MaxRowCount;   
 15function SelectOK(text)   
 16{   
 17eval("parent.opener.document.all.TextBox1.value = text");  //向弹出该窗体的窗口返回值   
 18parent.close();   
 19}   
 20function ChangePage(KeyCode)   
 21{   
 22if ( KeyCode == 33 ) {  // 定义 PageUp 键快捷功能   
 23document.all.PrevPage.click();  //调用服务器控件的相应功能   
 24}   
 25if ( KeyCode == 34 ) { // 定义 PageDown 键快捷功能   
 26document.all.NextPage.click();   
 27}   
 28if ( KeyCode == 36 ) { // 定义 Home 键快捷功能   
 29document.all.FirstPage.click();   
 30}   
 31if ( KeyCode == 35 ) { // 定义 End 键快捷功能   
 32document.all.LastPage.click();   
 33}   
 34if ( KeyCode == 38 ) { // ↑ 键快捷功能   
 35MoveUp();   
 36}   
 37if ( KeyCode == 40 ) { // ↓ 键快捷功能   
 38MoveDown();   
 39}   
 40if ( KeyCode == 37 ) { // ← 键快捷功能( 上一层 )   
 41//QueryParent();   
 42}   
 43if ( KeyCode == 39 ) { // → 键快捷功能( 下一层 )   
 44//QueryChild();   
 45}   
 46if ( KeyCode == 13 ) { // 回车键   
 47SelectOK(strReturn);   
 48}   
 49}   
 50function RowFocus(rowno,strValue,bFirst)   
 51{   
 52if (bFirst!=0){  //如果不是第一次调用该函数   
 53oldrow.style.backgroundColor=oldColor;  //将旧的一行设置回原来的颜色   
 54}   
 55strReturn = strValue;//strReturn中保存要返回的值   
 56oldrow=document.all('ROW' + rowno);//在oldrow中保存旧的行   
 57oldColor=document.all('ROW' + rowno).style.backgroundColor;//在oldColor中保存当前行的颜色   
 58document.all('ROW' + rowno).style.backgroundColor= newColor;//设置当前行的颜色为提醒色   
 59currowno=rowno;//在currowno中保存当前行号   
 60}   
 61function MoveUp() {   
 62if(currowno==0)//如果已经是最上一行则设置光标到最底一行   
 63currowno=MaxRowCount-1;   
 64else   
 65currowno=currowno-1;//否则上移一行   
 66document.all('ROW' + currowno).click();//调用该行的点击函数   
 67} 
 68
 69function MoveDown() {   
 70if(currowno==MaxRowCount-1)//如果已经是最底一行则设置光标到最底一行上一行   
 71currowno=0;   
 72else   
 73currowno=currowno+1;//否则下移一行   
 74document.all('ROW' + currowno).click();   
 75}   
 76</script>
 77<script event="onkeydown" for="document" language="JavaScript">   
 78if ( event.keyCode == 34 || event.keyCode == 33 || event.keyCode == 35 || event.keyCode == 36 || event.keyCode == 37 || event.keyCode == 38 || event.keyCode == 39 || event.keyCode == 40 || event.keyCode == 13 )   
 79{   
 80ChangePage(event.keyCode);//改变页面显示   
 81}   
 82if ( event.keyCode == 27 ) parent.close();//按下ESC键退出 
 83
 84</script>
 85<style>.hidetr { VISIBILITY: hidden }   
 86</style>
 87</head>
 88<body ms_positioning="GridLayout">
 89<form id="Form1" method="post" runat="server">
 90<font face="宋体" style="FONT-SIZE: x-small; FONT-FAMILY: 宋体">
 91<asp:datagrid allowpaging="True" autogeneratecolumns="False" backcolor="White" bordercolor="#3366CC" borderstyle="None" borderwidth="1px" cellpadding="4" datamember="Customers" datasource="```
 92# dataSet21 
 93```" font-size="X-Small" id="DataGrid1" runat="server" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" width="520px">
 94<footerstyle backcolor="#99CCCC" cssclass="hidetr" forecolor="#003399" wrap="False"></footerstyle>
 95<selecteditemstyle backcolor="#009999" font-bold="True" forecolor="#CCFF99" wrap="False"></selecteditemstyle>
 96<edititemstyle wrap="False"></edititemstyle>
 97<alternatingitemstyle wrap="False"></alternatingitemstyle>
 98<itemstyle backcolor="White" forecolor="#003399" wrap="False"></itemstyle>
 99<headerstyle backcolor="#003399" font-bold="True" forecolor="#CCCCFF" wrap="False"></headerstyle>
100<columns>
101<asp:boundcolumn datafield="CustomerID" headertext="用户编号" sortexpression="CustomerID">
102<headerstyle wrap="False"></headerstyle>
103<itemstyle wrap="False"></itemstyle>
104<footerstyle wrap="False"></footerstyle>
105</asp:boundcolumn>
106<asp:boundcolumn datafield="CompanyName" headertext="公司名称" sortexpression="CompanyName">
107<headerstyle wrap="False"></headerstyle>
108<itemstyle wrap="False"></itemstyle>
109<footerstyle wrap="False"></footerstyle>
110</asp:boundcolumn>
111<asp:boundcolumn datafield="ContactName" headertext="联系人" sortexpression="ContactName">
112<headerstyle wrap="False"></headerstyle>
113<itemstyle wrap="False"></itemstyle>
114<footerstyle wrap="False"></footerstyle>
115</asp:boundcolumn>
116<asp:boundcolumn datafield="Address" headertext="地址" sortexpression="Address">
117<headerstyle wrap="False"></headerstyle>
118<itemstyle wrap="False"></itemstyle>
119<footerstyle wrap="False"></footerstyle>
120</asp:boundcolumn>
121</columns>
122<pagerstyle backcolor="#99CCCC" forecolor="#003399" horizontalalign="Left" mode="NumericPages" wrap="False"></pagerstyle>
123</asp:datagrid><asp:label id="lblPageCount" runat="server" style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 288px" width="224px">Label</asp:label><asp:button id="PrevPage" runat="server" style="Z-INDEX: 103; LEFT: 352px; POSITION: absolute; TOP: 280px" text="&lt;" width="24px"></asp:button><asp:button id="NextPage" runat="server" style="Z-INDEX: 104; LEFT: 376px; POSITION: absolute; TOP: 280px" text="&gt;" width="25px"></asp:button>
124<asp:button id="FirstPage" runat="server" style="Z-INDEX: 105; LEFT: 400px; POSITION: absolute; TOP: 280px" text="|&lt;" width="24px"></asp:button>
125<asp:button id="LastPage" runat="server" style="Z-INDEX: 106; LEFT: 424px; POSITION: absolute; TOP: 280px" text="&gt;|" width="28px"></asp:button>
126<asp:textbox id="txtPageIndex" runat="server" style="Z-INDEX: 107; LEFT: 256px; POSITION: absolute; TOP: 280px" width="40px"></asp:textbox>
127<asp:button id="GotoPage" runat="server" style="Z-INDEX: 108; LEFT: 304px; POSITION: absolute; TOP: 280px" text="转到"></asp:button>
128<asp:regularexpressionvalidator controltovalidate="txtPageIndex" errormessage="需要输入整数" id="RegularExpressionValidator1" runat="server" style="Z-INDEX: 109; LEFT: 256px; POSITION: absolute; TOP: 312px" validationexpression="\d"></asp:regularexpressionvalidator></font></form>
129</body>
130</html>

Webform1.aspx.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 ComplexTest
{
///

1<summary>   
2/// WebForm1 的摘要说明。   
3/// </summary>

public class WebForm1 : System.Web.UI.Page
{
protected System.Data.SqlClient.SqlConnection sqlConnection1;
protected System.Web.UI.WebControls.Button PrevPage;
protected System.Web.UI.WebControls.Button NextPage;
protected System.Data.SqlClient.SqlCommand sqlSelectCommand2;
protected System.Data.SqlClient.SqlCommand sqlInsertCommand2;
protected System.Data.SqlClient.SqlCommand sqlUpdateCommand2;
protected System.Data.SqlClient.SqlCommand sqlDeleteCommand2;
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
protected ComplexTest.DataSet2 dataSet21;
protected System.Web.UI.WebControls.Button FirstPage;
protected System.Web.UI.WebControls.Button LastPage;
protected System.Web.UI.WebControls.Label lblPageCount;
protected System.Web.UI.WebControls.Button GotoPage;
protected System.Web.UI.WebControls.TextBox txtPageIndex;
protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
MyDataBind();
}
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

///

1<summary>   
2/// 设计器支持所需的方法 - 不要使用代码编辑器修改   
3/// 此方法的内容。   
4/// </summary>

private void InitializeComponent()
{
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlSelectCommand2 = new System.Data.SqlClient.SqlCommand();
this.sqlInsertCommand2 = new System.Data.SqlClient.SqlCommand();
this.sqlUpdateCommand2 = new System.Data.SqlClient.SqlCommand();
this.sqlDeleteCommand2 = new System.Data.SqlClient.SqlCommand();
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.dataSet21 = new ComplexTest.DataSet2();
((System.ComponentModel.ISupportInitialize)(this.dataSet21)).BeginInit();
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.PrevPage.Click += new System.EventHandler(this.Button1_Click);
this.NextPage.Click += new System.EventHandler(this.Button2_Click);
this.FirstPage.Click += new System.EventHandler(this.btnFirst_Click);
this.LastPage.Click += new System.EventHandler(this.btnLast_Click);
this.GotoPage.Click += new System.EventHandler(this.GotoPage_Click);
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=SOHO;packet size=4096;user id=sa;data source=SOHO;persist security" +
" info=False;initial catalog=Northwind";
//
// sqlSelectCommand2
//
this.sqlSelectCommand2.CommandText = "SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region," +
" PostalCode, Country, Phone, Fax FROM Customers";
this.sqlSelectCommand2.Connection = this.sqlConnection1;
//
// sqlInsertCommand2
//
this.sqlInsertCommand2.CommandText = @"INSERT INTO Customers(CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax) VALUES (@CustomerID, @CompanyName, @ContactName, @ContactTitle, @Address, @City, @Region, @PostalCode, @Country, @Phone, @Fax); SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax FROM Customers WHERE (CustomerID = @CustomerID)";
this.sqlInsertCommand2.Connection = this.sqlConnection1;
this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CustomerID", System.Data.SqlDbType.NVarChar, 5, "CustomerID"));
this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CompanyName", System.Data.SqlDbType.NVarChar, 40, "CompanyName"));
this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ContactName", System.Data.SqlDbType.NVarChar, 30, "ContactName"));
this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ContactTitle", System.Data.SqlDbType.NVarChar, 30, "ContactTitle"));
this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Address", System.Data.SqlDbType.NVarChar, 60, "Address"));
this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@City", System.Data.SqlDbType.NVarChar, 15, "City"));
this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Region", System.Data.SqlDbType.NVarChar, 15, "Region"));
this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@PostalCode", System.Data.SqlDbType.NVarChar, 10, "PostalCode"));
this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Country", System.Data.SqlDbType.NVarChar, 15, "Country"));
this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Phone", System.Data.SqlDbType.NVarChar, 24, "Phone"));
this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Fax", System.Data.SqlDbType.NVarChar, 24, "Fax"));
//
// sqlUpdateCommand2
//
this.sqlUpdateCommand2.CommandText = @"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName, ContactName = @ContactName, ContactTitle = @ContactTitle, Address = @Address, City = @City, Region = @Region, PostalCode = @PostalCode, Country = @Country, Phone = @Phone, Fax = @Fax WHERE (CustomerID = @Original_CustomerID) AND (Address = @Original_Address OR @Original_Address IS NULL AND Address IS NULL) AND (City = @Original_City OR @Original_City IS NULL AND City IS NULL) AND (CompanyName = @Original_CompanyName) AND (ContactName = @Original_ContactName OR @Original_ContactName IS NULL AND ContactName IS NULL) AND (ContactTitle = @Original_ContactTitle OR @Original_ContactTitle IS NULL AND ContactTitle IS NULL) AND (Country = @Original_Country OR @Original_Country IS NULL AND Country IS NULL) AND (Fax = @Original_Fax OR @Original_Fax IS NULL AND Fax IS NULL) AND (Phone = @Original_Phone OR @Original_Phone IS NULL AND Phone IS NULL) AND (PostalCode = @Original_PostalCode OR @Original_PostalCode IS NULL AND PostalCode IS NULL) AND (Region = @Original_Region OR @Original_Region IS NULL AND Region IS NULL); SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax FROM Customers WHERE (CustomerID = @CustomerID)";
this.sqlUpdateCommand2.Connection = this.sqlConnection1;
this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CustomerID", System.Data.SqlDbType.NVarChar, 5, "CustomerID"));
this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CompanyName", System.Data.SqlDbType.NVarChar, 40, "CompanyName"));
this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ContactName", System.Data.SqlDbType.NVarChar, 30, "ContactName"));
this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ContactTitle", System.Data.SqlDbType.NVarChar, 30, "ContactTitle"));
this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Address", System.Data.SqlDbType.NVarChar, 60, "Address"));
this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@City", System.Data.SqlDbType.NVarChar, 15, "City"));
this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Region", System.Data.SqlDbType.NVarChar, 15, "Region"));
this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@PostalCode", System.Data.SqlDbType.NVarChar, 10, "PostalCode"));
this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Country", System.Data.SqlDbType.NVarChar, 15, "Country"));
this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Phone", System.Data.SqlDbType.NVarChar, 24, "Phone"));
this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Fax", System.Data.SqlDbType.NVarChar, 24, "Fax"));
this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CustomerID", System.Data.SqlDbType.NVarChar, 5, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CustomerID", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Address", System.Data.SqlDbType.NVarChar, 60, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Address", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_City", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "City", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CompanyName", System.Data.SqlDbType.NVarChar, 40, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CompanyName", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ContactName", System.Data.SqlDbType.NVarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ContactName", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ContactTitle", System.Data.SqlDbType.NVarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ContactTitle", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Country", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Country", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Fax", System.Data.SqlDbType.NVarChar, 24, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Fax", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Phone", System.Data.SqlDbType.NVarChar, 24, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Phone", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_PostalCode", System.Data.SqlDbType.NVarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "PostalCode", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Region", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Region", System.Data.DataRowVersion.Original, null));
//
// sqlDeleteCommand2
//
this.sqlDeleteCommand2.CommandText = @"DELETE FROM Customers WHERE (CustomerID = @Original_CustomerID) AND (Address = @Original_Address OR @Original_Address IS NULL AND Address IS NULL) AND (City = @Original_City OR @Original_City IS NULL AND City IS NULL) AND (CompanyName = @Original_CompanyName) AND (ContactName = @Original_ContactName OR @Original_ContactName IS NULL AND ContactName IS NULL) AND (ContactTitle = @Original_ContactTitle OR @Original_ContactTitle IS NULL AND ContactTitle IS NULL) AND (Country = @Original_Country OR @Original_Country IS NULL AND Country IS NULL) AND (Fax = @Original_Fax OR @Original_Fax IS NULL AND Fax IS NULL) AND (Phone = @Original_Phone OR @Original_Phone IS NULL AND Phone IS NULL) AND (PostalCode = @Original_PostalCode OR @Original_PostalCode IS NULL AND PostalCode IS NULL) AND (Region = @Original_Region OR @Original_Region IS NULL AND Region IS NULL)";
this.sqlDeleteCommand2.Connection = this.sqlConnection1;
this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CustomerID", System.Data.SqlDbType.NVarChar, 5, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CustomerID", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Address", System.Data.SqlDbType.NVarChar, 60, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Address", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_City", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "City", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CompanyName", System.Data.SqlDbType.NVarChar, 40, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CompanyName", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ContactName", System.Data.SqlDbType.NVarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ContactName", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ContactTitle", System.Data.SqlDbType.NVarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ContactTitle", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Country", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Country", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Fax", System.Data.SqlDbType.NVarChar, 24, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Fax", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Phone", System.Data.SqlDbType.NVarChar, 24, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Phone", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_PostalCode", System.Data.SqlDbType.NVarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "PostalCode", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Region", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Region", System.Data.DataRowVersion.Original, null));
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand2;
this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand2;
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand2;
this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "Customers", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("CustomerID", "CustomerID"),
new System.Data.Common.DataColumnMapping("CompanyName", "CompanyName"),
new System.Data.Common.DataColumnMapping("ContactName", "ContactName"),
new System.Data.Common.DataColumnMapping("ContactTitle", "ContactTitle"),
new System.Data.Common.DataColumnMapping("Address", "Address"),
new System.Data.Common.DataColumnMapping("City", "City"),
new System.Data.Common.DataColumnMapping("Region", "Region"),
new System.Data.Common.DataColumnMapping("PostalCode", "PostalCode"),
new System.Data.Common.DataColumnMapping("Country", "Country"),
new System.Data.Common.DataColumnMapping("Phone", "Phone"),
new System.Data.Common.DataColumnMapping("Fax", "Fax")})});
this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand2;
//
// dataSet21
//
this.dataSet21.DataSetName = "DataSet2";
this.dataSet21.Locale = new System.Globalization.CultureInfo("zh-CN");
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)(this.dataSet21)).EndInit();

}
#endregion

private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if((e.Item.ItemType==ListItemType.Item)||(e.Item.ItemType==ListItemType.AlternatingItem))
{
string strField=e.Item.Cells[0].Text;//指定要返回的字段,目前是返回第0行,可以改成其他的字段
e.Item.Attributes.Add("onDblClick","SelectOK('"+strField+"')");//双击时也可以返回选中结果
e.Item.Attributes.Add("onClick","RowFocus("+e.Item.ItemIndex+",'"+strField+"',1)");//单击时执行选中行
e.Item.Attributes.Add("id","ROW"+e.Item.ItemIndex);//给表格中的每一行起个名字以方便客户端代码调用
if(e.Item.ItemIndex==0) //设置初始化时的Javascript语句
{
CreateJavascript(strField,DataGrid1.PageSize);//初始化客户端代码
}
//e.Item.Attributes.Add("onMouseOver","RowFocus("+e.Item.ItemIndex+",'"+e.Item.Cells[0].Text +"',1)");
}

}
private void CreateJavascript(string ColValue,int PageSize)
{
//画面初始化Javascript语句,将凸现行设置为首行,并设置页面行数
String scriptString = "

1<script language="javascript">";   
2scriptString += "MaxRowCount ="+PageSize+";"; //对客户端代码设置最大的行数   
3scriptString += "RowFocus(0,'"+ColValue+"',0)";//设置初始化时将光标定位到第一行   
4scriptString += "</script>

";
if(!this.IsStartupScriptRegistered("Startup"))
this.RegisterStartupScript("Startup", scriptString);

}
private void Button1_Click(object sender, System.EventArgs e)
{
if(DataGrid1.PageCount>0)
{
if(DataGrid1.CurrentPageIndex>0)
{
DataGrid1.CurrentPageIndex = DataGrid1.CurrentPageIndex-1;
MyDataBind();
}
}
}
private void Button2_Click(object sender, System.EventArgs e)
{
if(DataGrid1.PageCount>0)
{
if(DataGrid1.CurrentPageIndex

 1<datagrid1.pagecount-1) btnfirst_click(object="" datagrid1.currentpageindex="DataGrid1.CurrentPageIndex+1;" e)="" if(datagrid1.pagecount="" mydatabind();="" private="" sender,="" system.eventargs="" void="" {="" }="">0)   
 2{   
 3DataGrid1.CurrentPageIndex = 0;   
 4MyDataBind();   
 5}   
 6}   
 7private void btnLast_Click(object sender, System.EventArgs e)   
 8{   
 9if(DataGrid1.PageCount&gt;0)   
10{   
11DataGrid1.CurrentPageIndex =DataGrid1.PageCount-1;   
12MyDataBind();   
13}   
14}   
15private void MyDataBind()   
16{   
17sqlDataAdapter1.Fill(dataSet21);   
18DataGrid1.DataBind();   
19if(DataGrid1.PageCount&gt;0)   
20{   
21int PageIndex = DataGrid1.CurrentPageIndex+1;   
22lblPageCount.Text = "共"+DataGrid1.PageCount+"页,当前为第"+PageIndex.ToString()+"页";   
23txtPageIndex.Text = PageIndex.ToString();   
24}   
25} 
26
27private void GotoPage_Click(object sender, System.EventArgs e)   
28{   
29if(DataGrid1.PageCount&gt;0)   
30{   
31if(txtPageIndex.Text.Length&gt;0)   
32{   
33int i=Convert.ToInt16(txtPageIndex.Text)-1;   
34if((i<datagrid1.pagecount)&&(i>=0))   
35{   
36DataGrid1.CurrentPageIndex=i;   
37MyDataBind();   
38}   
39}   
40}   
41}   
42}   
43}   
44  
45---  
46  
47index.aspx   

@ Page language="c#" Codebehind="index.aspx.cs" AutoEventWireup="false" Inherits="ComplexTest.index"

 1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
 2
 3<html>
 4<head>
 5<title>index</title>
 6<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"/>
 7<meta content="C#" name="CODE_LANGUAGE"/>
 8<meta content="JavaScript" name="vs_defaultClientScript"/>
 9<meta content="  http://schemas.microsoft.com/intellisense/ie5  " name="vs_targetSchema"/>
10<script language="javascript">   
11function Search() //选择发货单位   
12{   
13var strURL;   
14strURL="WebForm1.aspx";   
15window.open(strURL,"SELECT_ONE_BASE","TOOLBAR=NO,MENUBAR=NO,RESIZABLE=YES,TOP=0,LEFT=0,WIDTH=720PT,HEIGHT=460PT");   
16}   
17</script>
18<script event="onkeydown" for="document" language="JavaScript">   
19if ( event.keyCode == 27 )   
20{   
21window.opener='anyone';window.close();   
22} 
23
24</script>
25<style>.flattext { FONT-SIZE: x-small; BORDER-TOP-STYLE: none; BORDER-BOTTOM: black 1px solid; FONT-FAMILY: 宋体; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none }   
26.seltext { FONT-SIZE: x-small; BORDER-TOP-STYLE: none; BORDER-BOTTOM: black 1px solid; FONT-FAMILY: 宋体; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BACKGROUND-COLOR: #ffcc66 }   
27</style>
28</head>
29<body ms_positioning="GridLayout">
30<form id="Form1" method="post" runat="server">
31<font face="宋体" style="BORDER-RIGHT: 1px; BORDER-TOP: 1px; FONT-SIZE: x-small; BORDER-LEFT: 1px; BORDER-BOTTOM: black 1px solid; FONT-FAMILY: 宋体">
32<asp:textbox backcolor="#FFE0C0" cssclass="flattext" height="20px" id="TextBox1" readonly="True" runat="server" style="Z-INDEX: 101; LEFT: 88px; POSITION: absolute; TOP: 64px"></asp:textbox><input onclick="Search()" style="Z-INDEX: 102; LEFT: 240px; WIDTH: 21px; POSITION: absolute; TOP: 64px; HEIGHT: 20px" type="button" value="..."/>
33<asp:label id="Label1" runat="server" style="Z-INDEX: 103; LEFT: 40px; POSITION: absolute; TOP: 72px">请选择</asp:label></font></form>
34</body>
35</html>  
36  
37---</datagrid1.pagecount)&&(i></datagrid1.pagecount-1)>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus