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

类别:.NET开发 点击:0 评论:0 推荐:
一下代码可以实现弹出一个DataGrid窗口,该窗口支持up,down以及pageup,pagedown,home,end,Enter键盘操作,在按下Enter键后将选中的值返回初始窗口的TextBox1中。



webform1.aspx

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="ComplexTest.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">
   var oldrow
   var currowno;
   var strReturn; //存储返回值
   var newColor='#C0C0C0';
   var oldColor;
   var MaxRowCount;
   function SelectOK(text)
   {
    eval("parent.opener.document.all.TextBox1.value = text");//向弹出该窗体的窗口返回值
    parent.close();
   }
   function ChangePage(KeyCode)
   {
    if ( KeyCode == 33 ) { // 定义 PageUp 键快捷功能
     document.all.PrevPage.click();//调用服务器控件的相应功能
    }
    if ( KeyCode == 34 ) { // 定义 PageDown 键快捷功能
     document.all.NextPage.click();
    }
    if ( KeyCode == 36 ) { // 定义 Home 键快捷功能
     document.all.FirstPage.click();
    }
    if ( KeyCode == 35 ) { // 定义 End 键快捷功能
     document.all.LastPage.click();
    }
    if ( KeyCode == 38 ) { // ↑ 键快捷功能
     MoveUp();
    }
    if ( KeyCode == 40 ) { // ↓ 键快捷功能
     MoveDown();
    }
    if ( KeyCode == 37 ) { // ← 键快捷功能( 上一层 )
     //QueryParent();
    }
    if ( KeyCode == 39 ) { // → 键快捷功能( 下一层 )
     //QueryChild();
    }
    if ( KeyCode == 13 ) { // 回车键
     SelectOK(strReturn);
    }
   }
   function RowFocus(rowno,strValue,bFirst)
   {
    if (bFirst!=0){//如果不是第一次调用该函数
     oldrow.style.backgroundColor=oldColor;//将旧的一行设置回原来的颜色
    }
    strReturn = strValue;//strReturn中保存要返回的值
    oldrow=document.all('ROW' + rowno);//在oldrow中保存旧的行
    oldColor=document.all('ROW' + rowno).style.backgroundColor;//在oldColor中保存当前行的颜色
    document.all('ROW' + rowno).style.backgroundColor= newColor;//设置当前行的颜色为提醒色
    currowno=rowno;//在currowno中保存当前行号
   }
     function MoveUp() {
      if(currowno==0)//如果已经是最上一行则设置光标到最底一行
       currowno=MaxRowCount-1;
      else
       currowno=currowno-1;//否则上移一行
    document.all('ROW' + currowno).click();//调用该行的点击函数
   }

   function MoveDown() {
    if(currowno==MaxRowCount-1)//如果已经是最底一行则设置光标到最底一行上一行
       currowno=0;
      else
       currowno=currowno+1;//否则下移一行
    document.all('ROW' + currowno).click();
   }
  </script>
  <script language="JavaScript" event="onkeydown" for="document">
    if ( 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 )
    {    
    ChangePage(event.keyCode);//改变页面显示
    }
    if ( event.keyCode == 27 ) parent.close();//按下ESC键退出

  </script>
  <STYLE>.hidetr { VISIBILITY: hidden }
  </STYLE>
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <FONT face="宋体" style="FONT-SIZE: x-small; FONT-FAMILY: 宋体">
    <asp:datagrid id=DataGrid1 style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server" Width="520px" DataSource="<%# dataSet21 %>" DataMember="Customers" AllowPaging="True" AutoGenerateColumns="False" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="4" Font-Size="X-Small" >
     <FooterStyle Wrap="False" ForeColor="#003399" CssClass="hidetr" BackColor="#99CCCC"></FooterStyle>
     <SelectedItemStyle Font-Bold="True" Wrap="False" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>
     <EditItemStyle Wrap="False"></EditItemStyle>
     <AlternatingItemStyle Wrap="False"></AlternatingItemStyle>
     <ItemStyle Wrap="False" ForeColor="#003399" BackColor="White"></ItemStyle>
     <HeaderStyle Font-Bold="True" Wrap="False" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle>
     <Columns>
      <asp:BoundColumn DataField="CustomerID" SortExpression="CustomerID" HeaderText="用户编号">
       <HeaderStyle Wrap="False"></HeaderStyle>
       <ItemStyle Wrap="False"></ItemStyle>
       <FooterStyle Wrap="False"></FooterStyle>
      </asp:BoundColumn>
      <asp:BoundColumn DataField="CompanyName" SortExpression="CompanyName" HeaderText="公司名称">
       <HeaderStyle Wrap="False"></HeaderStyle>
       <ItemStyle Wrap="False"></ItemStyle>
       <FooterStyle Wrap="False"></FooterStyle>
      </asp:BoundColumn>
      <asp:BoundColumn DataField="ContactName" SortExpression="ContactName" HeaderText="联系人">
       <HeaderStyle Wrap="False"></HeaderStyle>
       <ItemStyle Wrap="False"></ItemStyle>
       <FooterStyle Wrap="False"></FooterStyle>
      </asp:BoundColumn>
      <asp:BoundColumn DataField="Address" SortExpression="Address" HeaderText="地址">
       <HeaderStyle Wrap="False"></HeaderStyle>
       <ItemStyle Wrap="False"></ItemStyle>
       <FooterStyle Wrap="False"></FooterStyle>
      </asp:BoundColumn>
     </Columns>
     <PagerStyle HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC" Wrap="False" Mode="NumericPages"></PagerStyle>
    </asp:datagrid><asp:label id="lblPageCount" style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 288px"
     runat="server" Width="224px">Label</asp:label><asp:button id="PrevPage" style="Z-INDEX: 103; LEFT: 352px; POSITION: absolute; TOP: 280px"
     runat="server" Text="<" Width="24px"></asp:button><asp:button id="NextPage" style="Z-INDEX: 104; LEFT: 376px; POSITION: absolute; TOP: 280px"
     runat="server" Text=">" Width="25px"></asp:button>
    <asp:Button id="FirstPage" style="Z-INDEX: 105; LEFT: 400px; POSITION: absolute; TOP: 280px"
     runat="server" Text="|<" Width="24px"></asp:Button>
    <asp:Button id="LastPage" style="Z-INDEX: 106; LEFT: 424px; POSITION: absolute; TOP: 280px"
     runat="server" Text=">|" Width="28px"></asp:Button>
    <asp:TextBox id="txtPageIndex" style="Z-INDEX: 107; LEFT: 256px; POSITION: absolute; TOP: 280px"
     runat="server" Width="40px"></asp:TextBox>
    <asp:Button id="GotoPage" style="Z-INDEX: 108; LEFT: 304px; POSITION: absolute; TOP: 280px"
     runat="server" Text="转到"></asp:Button>
    <asp:RegularExpressionValidator id="RegularExpressionValidator1" style="Z-INDEX: 109; LEFT: 256px; POSITION: absolute; TOP: 312px"
     runat="server" ErrorMessage="需要输入整数" ValidationExpression="\d" ControlToValidate="txtPageIndex"></asp:RegularExpressionValidator></FONT></form>
 </body>
</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
{
 /// <summary>
 /// WebForm1 的摘要说明。
 /// </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);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </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 = "<script language=javascript>";
   scriptString += "MaxRowCount ="+PageSize+";"; //对客户端代码设置最大的行数
   scriptString += "RowFocus(0,'"+ColValue+"',0)";//设置初始化时将光标定位到第一行
   scriptString += "</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<DataGrid1.PageCount-1)
    {
     DataGrid1.CurrentPageIndex =DataGrid1.CurrentPageIndex+1;
     MyDataBind();
    }
   }

  }
  private void btnFirst_Click(object sender, System.EventArgs e)
  {
   if(DataGrid1.PageCount>0)
   {
    DataGrid1.CurrentPageIndex = 0;
    MyDataBind();  
   }
  }
  private void btnLast_Click(object sender, System.EventArgs e)
  {
   if(DataGrid1.PageCount>0)
   {
    DataGrid1.CurrentPageIndex =DataGrid1.PageCount-1;
    MyDataBind(); 
   }
  }
  private void MyDataBind()
  {
   sqlDataAdapter1.Fill(dataSet21);
   DataGrid1.DataBind();
   if(DataGrid1.PageCount>0)
   {
    int PageIndex = DataGrid1.CurrentPageIndex+1;
    lblPageCount.Text = "共"+DataGrid1.PageCount+"页,当前为第"+PageIndex.ToString()+"页";
    txtPageIndex.Text = PageIndex.ToString();
   }
  }

  private void GotoPage_Click(object sender, System.EventArgs e)
  {
   if(DataGrid1.PageCount>0)
   {
    if(txtPageIndex.Text.Length>0)
    {
     int i=Convert.ToInt16(txtPageIndex.Text)-1;
     if((i<DataGrid1.PageCount)&&(i>=0))
     {
      DataGrid1.CurrentPageIndex=i;
      MyDataBind();
     }
    }
   }
  }
 }
}


index.aspx

<%@ Page language="c#" Codebehind="index.aspx.cs" AutoEventWireup="false" Inherits="ComplexTest.index" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>index</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 Search() //选择发货单位
   {
    var strURL;
    strURL="WebForm1.aspx";
    window.open(strURL,"SELECT_ONE_BASE","TOOLBAR=NO,MENUBAR=NO,RESIZABLE=YES,TOP=0,LEFT=0,WIDTH=720PT,HEIGHT=460PT");
   }
  </script>
  <script for="document" event="onkeydown" language="JavaScript">
    if ( event.keyCode == 27 )
    {
       window.opener='anyone';window.close();
     }

  </script>
  <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 }
 .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 }
  </STYLE>
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <FONT style="BORDER-RIGHT: 1px; BORDER-TOP: 1px; FONT-SIZE: x-small; BORDER-LEFT: 1px; BORDER-BOTTOM: black 1px solid; FONT-FAMILY: 宋体"
    face="宋体">
    <asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 88px; POSITION: absolute; TOP: 64px" runat="server"
     CssClass="flattext" ReadOnly="True" BackColor="#FFE0C0" Height="20px"></asp:TextBox><INPUT style="Z-INDEX: 102; LEFT: 240px; WIDTH: 21px; POSITION: absolute; TOP: 64px; HEIGHT: 20px"
     type="button" value="..." onClick="Search()">
    <asp:Label id="Label1" style="Z-INDEX: 103; LEFT: 40px; POSITION: absolute; TOP: 72px" runat="server">请选择</asp:Label></FONT></form>
 </body>
</HTML>

 

本文地址:http://com.8s8s.com/it/it42369.htm