欢迎光临
我们一直在努力

创建自滚动的DataGrid-.NET教程,数据库应用

建站超值云服务器,限时71元/月

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

<script language="<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/programme/java/index.html" target="_blank" rel="nofollow" >java</a>script"> <!-- function __dopostback(eventtarget, eventargument) { var theform = document.form1; theform.__eventtarget.value = eventtarget; theform.__eventargument.value = eventargument; theform.submit(); } // --> </script><br />

这段脚本中__dopostback函数有两个参数:第一个eventtarget是触发postback的控件的uniqueid;第二参数eventargument是一个对象,包含postback的额外信息。因此我们使用uniqueid来作为锚点的值。

源代码如下:

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

datagridautoscroll.aspx

<%@ page language="vb" autoeventwireup="false" codebehind="datagridautoscroll.<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>x.vb&#8221;<br /> inherits=&#8221;<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>xweb.datagridautoscroll&#8221;%><br /> <!doctype html public "-//w3c//dtd html 4.0 transitional//en"><br /> <html><br /> <head><br /> <title runat="server" id="mengxianhui"></title><br /> <meta name="generator" content="microsoft visual studio.net 7.0"><br /> <meta name="code_language" content="visual basic 7.0"><br /> <meta name="vs_defaultclientscript" content="javascript"><br /> <meta name="vs_targetschema" content="http://schemas.microsoft.com/intellisense/ie5"><br /> </head><br /> <body></p> <form id="form1" method="post" runat="server"> <<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:datagrid id=&#8221;datagrid1&#8243; runat=&#8221;server&#8221; bordercolor=&#8221;#cc9966&#8243; borderstyle=&#8221;none&#8221;<br /> borderwidth=&#8221;1px&#8221; backcolor=&#8221;white&#8221; cellpadding=&#8221;4&#8243;><br /> <selecteditemstyle font-bold="true" forecolor="#663399" backcolor="#ffcc66"></selecteditemstyle><br /> <itemstyle forecolor="#330099" backcolor="white"></itemstyle><br /> <headerstyle font-bold="true" forecolor="#ffffcc" backcolor="#990000"></headerstyle> <footerstyle forecolor="#330099" backcolor="#ffffcc"></footerstyle> <columns> <<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:editcommandcolumn buttontype=&#8221;linkbutton&#8221; updatetext=&#8221;update&#8221;<br /> canceltext=&#8221;cancel&#8221; edittext=&#8221;edit&#8221;></<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:editcommandcolumn><br /> </columns> <pagerstyle horizontalalign="center" forecolor="#330099" backcolor="#ffffcc"></pagerstyle> </<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:datagrid><br /> </form> <p> </body><br /> </html><br />

代码:datagridautoscroll.aspx.vb

imports system<br /> imports system.web<br /> imports system.web.ui.webcontrols<br /> imports system.collections<br /> imports system.data<br /> imports system.data.sqlclient</p> <p>public class datagridautoscroll<br /> inherits system.web.ui.page<br /> protected withevents datagrid1 as system.web.ui.webcontrols.datagrid<br /> protected mengxianhui as new htmlgenericcontrol()</p> <p>#region &#8221; web form designer generated code &#8221;<br /> <system.diagnostics.debuggerstepthrough()> private sub initializecomponent()<br /> end sub</p> <p> private sub page_init(byval sender as system.object, byval e as system.eventargs) handles mybase.init<br /> initializecomponent()<br /> end sub</p> <p>#end region</p> <p> private sub page_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load<br /> mengxianhui.innertext = &#8220;【孟宪会之精彩世界】之.net开发者园地&#8221;<br /> if not page.ispostback then<br /> datagrid1.datasource = createdatasource()<br /> datagrid1.databind()<br /> else<br /> dim startupscript as string<br /> startupscript = &#8220;<script language=javascript>location.href=#" _ & request.form("__eventtarget") & ";</script>&#8221;<br /> me.registerstartupscript(me.uniqueid &#038; &#8220;startup&#8221;, startupscript)<br /> end if</p> <p> end sub</p> <p> function createdatasource() as icollection</p> <p> dim dt as datatable<br /> dim dr as datarow<br /> dim i as integer</p> <p> 创建 datatable<br /> dt = new datatable()<br /> dt.columns.add(new datacolumn(&#8220;<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/programme/vc/str/index.html" target="_blank" rel="nofollow" >字符</a>型值&#8221;, gettype(string)))<br /> dt.columns.add(new datacolumn(&#8220;布尔型值&#8221;, gettype(boolean)))<br /> dt.columns.add(new datacolumn(&#8220;货币型值&#8221;, gettype(double)))</p> <p> 示例数据<br /> for i = 1 to 150<br /> dr = dt.newrow()<br /> dr(0) = &#8220;item &#8221; + i.tostring()<br /> if (i mod 2 <> 0) then<br /> dr(1) = true<br /> else<br /> dr(1) = false<br /> end if<br /> dr(2) = 1.23 * (i + 1)<br /> 向datatable添加 row<br /> dt.rows.add(dr)<br /> next</p> <p> 返回datatable的dataview<br /> createdatasource = new dataview(dt)</p> <p> end function</p> <p> private sub datagrid1_itemdatabound(byval sender as object, _<br /> byval e as system.web.ui.webcontrols.datagriditemeventargs) handles datagrid1.itemdatabound<br /> select case e.item.itemtype<br /> case listitemtype.item, listitemtype.alternatingitem<br /> dim editbutton as linkbutton = new linkbutton()<br /> editbutton = ctype(e.item.cells(0).controls(0), linkbutton)<br /> editbutton.attributes.add(&#8220;name&#8221;, &#8220;#&#8221; &#038; editbutton.uniqueid)</p> <p> case listitemtype.edititem<br /> dim updatebutton as linkbutton = new linkbutton()<br /> updatebutton = ctype(e.item.cells(0).controls(0), linkbutton)<br /> updatebutton.attributes.add(&#8220;name&#8221;, &#8220;#&#8221; &#038; updatebutton.uniqueid)<br /> end select<br /> end sub</p> <p> private sub datagrid1_editcommand(byval source as object, _<br /> byval e as system.web.ui.webcontrols.datagridcommandeventargs) handles datagrid1.editcommand<br /> datagrid1.edititemindex = e.item.itemindex<br /> datagrid1.datasource = createdatasource()<br /> datagrid1.databind()<br /> end sub</p> <p> private sub datagrid1_cancelcommand(byval source as object, _<br /> byval e as system.web.ui.webcontrols.datagridcommandeventargs) handles datagrid1.cancelcommand<br /> datagrid1.edititemindex = -1<br /> datagrid1.datasource = createdatasource()<br /> datagrid1.databind()<br /> end sub</p> <p> private sub datagrid1_updatecommand(byval source as object, _<br /> byval e as system.web.ui.webcontrols.datagridcommandeventargs) handles datagrid1.updatecommand<br /> datagrid1.edititemindex = -1<br /> datagrid1.datasource = createdatasource()<br /> datagrid1.databind()<br /> end sub<br /> end class<br />

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 创建自滚动的DataGrid-.NET教程,数据库应用
分享到: 更多 (0)