创建自滚动的DataGrid

<script language='javascript' src='http://www.shiqiaotou.com/donetk/Header.js'></script>   在使用DataGrid时,如果页面很长,可能需要用户自己来拉动滚动条,下面的例子实现了自动滚动的功能。其基本原理就是利用了LinkButton的锚点的功能,如果使用PushButton,那还必须自己添加锚点。另外必须弄明白的是Page的PostBack时的客户端脚本:
  
  <script language="<a href="http://dev.21tx.com/web/javascript/" target="_blank">JavaScript</a>">
  <!--
   function __doPostBack(eventTarget, eventArgument) {
   var theform = document.Form1;
   theform.__EVENTTARGET.value = eventTarget;
   theform.__EVENTARGUMENT.value = eventArgument;
   theform.submit();
   }
  // -->
  </script>
  
  这段脚本中__doPostBack函数有两个参数:第一个eventTarget是触发PostBack的控件的UniqueID;第二参数eventArgument是一个对象,包含PostBack的额外信息。因此我们使用UniqueID来作为锚点的值。
  
  源代码如下:
  
  查看例子
  
  DataGridAutoScroll.ASPx
  
  <%@ Page Language="<a href="http://dev.21tx.com/language/vb/" target="_blank">VB</a>" AutoEventWireup="false" Codebehind="DataGridAutoScroll.aspx.vb"
   Inherits="aspx<a href="http://dev.21tx.com/web/" target="_blank">Web</a>.DataGridAutoScroll"%>
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  <HTML>
   <HEAD>
   <title runat="server" id="mengxianhui"></title>
   <meta name="GENERATOR" content="Microsoft Visual Studio<a href="http://dev.21tx.com/dotnet/" target="_blank">.net</a> 7.0">
   <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
   <meta name="vs_defaultClientScript" content="<a href="http://dev.21tx.com/java/" target="_blank">Java</a>Script">
   <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
   </HEAD>
   <body>
   <form id="Form1" method="post" runat="server">
   <asp:DataGrid id="DataGrid1" runat="server" BorderColor="#CC9966" BorderStyle="None"
   BorderWidth="1px" BackColor="White" CellPadding="4">
   <SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
   <ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
   <HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
   <FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
   <Columns>
   <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
   CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
   </Columns>
   <PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>
   </asp:DataGrid>
   </form>
   </body>
  </HTML>
  
  代码:DataGridAutoScroll.aspx.vb
  
  Imports System
  Imports System.Web
  Imports System.Web.UI.WebControls
  Imports System.Collections
  Imports System.Data
  Imports System.Data.SqlClient
  
  Public Class DataGridAutoScroll
   Inherits System.Web.UI.Page
   Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
   Protected mengxianhui As New HtmlGenericControl()
  
  #Region " Web Form Designer Generated Code "
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
   End Sub
  
   Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
   InitializeComponent()
   End Sub
  
  #End Region
  
   Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   mengxianhui.InnerText = "【孟宪会之精彩世界】之.NET开发者园地"
   If Not Page.IsPostBack Then
   DataGrid1.DataSource = CreateDataSource()
   DataGrid1.DataBind()
   Else
   Dim startUpScript As String
   startUpScript = "<script language=Javascript>location.href='#" _
   & Request.Form("__EVENTTARGET") & "';</script>"
   Me.RegisterStartupScript(Me.UniqueID & "StartUp", startUpScript)
   End If
  
   End Sub
  
   Function CreateDataSource() As ICollection
  
   Dim dt As DataTable
   Dim dr As DataRow
   Dim i As Integer
  
   '创建 DataTable
   dt = New DataTable()
   dt.Columns.Add(New DataColumn("字符型值", GetType(String)))
   dt.Columns.Add(New DataColumn("布尔型值", GetType(Boolean)))
   dt.Columns.Add(New DataColumn("货币型值", GetType(Double)))
  
   '示例数据
   For i = 1 To 150
   dr = dt.NewRow()
   dr(0) = "Item " + i.ToString()
   If (i Mod 2 <> 0) Then
   dr(1) = True
   Else
   dr(1) = False
   End If
   dr(2) = 1.23 * (i + 1)
   '向datatable添加 row
   dt.Rows.Add(dr)
   Next
  
   '返回DataTable的DataView
   CreateDataSource = New DataView(dt)
  
   End Function
  
   Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, _
   ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
   Select Case e.Item.ItemType
   Case ListItemType.Item, ListItemType.AlternatingItem
   Dim editButton As LinkButton = New LinkButton()
   editButton = CType(e.Item.Cells(0).Controls(0), LinkButton)
   editButton.Attributes.Add("name", "#" & editButton.UniqueID)
  
   Case ListItemType.EditItem
   Dim UpdateButton As LinkButton = New LinkButton()
   UpdateButton = CType(e.Item.Cells(0).Controls(0), LinkButton)
   UpdateButton.Attributes.Add("name", "#" & UpdateButton.UniqueID)
   End Select
   End Sub
  
   Private Sub DataGrid1_EditCommand(ByVal source As Object, _
   ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.EditCommand
   DataGrid1.EditItemIndex = e.Item.ItemIndex
   DataGrid1.DataSource = CreateDataSource()
   DataGrid1.DataBind()
   End Sub
  
   Private Sub DataGrid1_CancelCommand(ByVal source As Object, _
   ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.CancelCommand
   DataGrid1.EditItemIndex = -1
   DataGrid1.DataSource = CreateDataSource()
   DataGrid1.DataBind()
   End Sub
  
   Private Sub DataGrid1_UpdateCommand(ByVal source As Object, _
   ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.UpdateCommand
   DataGrid1.EditItemIndex = -1
   DataGrid1.DataSource = CreateDataSource()
   DataGrid1.DataBind()
   End Sub
  End Class<script language='javascript' src='http://www.shiqiaotou.com/donetk/Footer.js'></script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值