Username: Password:

创建自滚动的DataGrid-.NET教程,数据库应用
来源:作者: 发布时间:2007-12-25 13:34:35

在使用datagrid时,假如页面很长,可能需要用户自己来拉动滚动条,下面的例子实现了自动滚动的功能。其基本原理就是利用了linkbutton的锚点的功能,假如使用pushbutton,那还必须自己添加锚点。另外必须弄明白的是page的postback时的客户端脚本:

<script language="javascript"> <!-- 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来作为锚点的值。

源代码如下:

aspx" target="_blank">查看例子

datagridautoscroll.aspx

<%@ page language="vb" autoeventwireup="false" codebehind="datagridautoscroll.aspx.vb" inherits="aspxweb.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.net 7.0"> <meta name="code_language" content="visual basic 7.0"> <meta name="vs_defaultclientscript" content="javascript"> <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

喜欢本文,那就收藏到:

    Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 POCO网摘 添加到饭否 QQ书签 Digbuzz我挖网
相关评论  我也要评论
还没有关于此文章的相关评论!
  • 昵称: (为空则显示guest)
  • 评论分数: ★ ★ ★★★ ★★★★ ★★★★★
  • 评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
  • 导航
    赞助商
    文章类别
    订阅