.NET开发

本类阅读TOP10

·NHibernate快速指南(翻译)
·vs.net 2005中文版下载地址收藏
·【小技巧】一个判断session是否过期的小技巧
·VB/ASP 调用 SQL Server 的存储过程
·?dos下编译.net程序找不到csc.exe文件
·通过Web Services上传和下载文件
·学习笔记(补)《.NET框架程序设计(修订版)》--目录
·VB.NET实现DirectDraw9 (2) 动画
·VB.NET实现DirectDraw9 (1) 托管的DDraw
·建站框架规范书之——文件命名

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
Creating a Scrollable DataGrid...

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

Place a DataGrid, or any other control or set of controls, within a scrollable region on your .NET web forms.


By: John Kilgo Date: November 23, 2003 Download the code. Printer Friendly Version

There are many times when it is inconvenient or unattractive to have a datagrid or other set of controls longer than the displayable page in the browser. I would prefer the user not to have to scroll the entire page if possible.

There is a way of accomplishing this that does not have anything at all to do with .NET. It actually has to do with using a style attribute within a <DIV> tag. While this is not a .NET technique, it may prove to be very useful to you in your .NET programming. The tag takes the form:

<DIV style="OVERFLOW-Y:scroll; WIDTH:760px; HEIGHT:570px">

The Width and Height styles allow you to determine the size of the scrollable region. The 760px and 570px shown above are just what I used for the example program. You can make them whatever you need them to be. The OVERFLOW-Y:scroll tells the browser to scroll the div vertically. All you have to do is place a table of objects, or a single object such as a datagrid within the DIV and it will be scrollable. You will be able to see this for yourself if you run the example program available at the bottom of this page, and/or download the sample code.

The example .aspx file (ScrollingGrid.aspx), which demonstrates the technique is shown below.

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="ScrollingGrid.aspx.vb" Inherits="DotNetJohn.ScrollingGrid"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>ScrollingGrid</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<table border="1" bgcolor="#EEEEEE" style="WIDTH: 780px; HEIGHT: 580px">
  <tr>
    <td align="center">Northwind Customers</td>
  <tr>
    <td>
    <div style="OVERFLOW-Y:scroll; WIDTH:760px; HEIGHT:570px">
      <table bgcolor="#FFFFFF">
        <tr>
          <td>
            <asp:DataGrid ID="dtgCusts" Runat="server"
                          BorderColor="#999999" BorderStyle="None"
                          BorderWidth="1px" BackColor="White"
                          CellPadding="3" GridLines="Vertical">
              <AlternatingItemStyle BackColor="#DCDCDC"></AlternatingItemStyle>
              <ItemStyle ForeColor="Black" BackColor="#EEEEEE"></ItemStyle>
              <HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#000084"></HeaderStyle>
            </asp:DataGrid>
          </td>
        </tr>
      </table>
    </div>
    </td>
  </tr>
</table>
</form>
</body>
</HTML>

The .aspx.vb file, which just binds the datagrid to a datareader from the Northwind Customers table is shown below.

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Public Class ScrollingGrid
  Inherits System.Web.UI.Page

'-- Web Form Designer Generated Code Omitted --

  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim strSql As String = "Select CustomerID, CompanyName, ContactName, Phone, Fax From Customers"
    Dim objConn As New SqlConnection(ConfigurationSettings.AppSettings("NorthwindConnection"))
    Dim objCmd As New SqlCommand(strSql, objConn)
    Try
      objConn.Open()
      dtgCusts.DataSource = objCmd.ExecuteReader()
      dtgCusts.DataBind()
    Catch ex As SqlException
      Response.Write(ex.ToString)
    Finally
      objCmd.Dispose()
      objConn.Dispose()
    End Try
  End Sub

End Class

I hope you will find this technique useful in your .NET programming.

You may run the program here.
You may download the code here.




相关文章

相关软件