在使用datagrid时,如果页面很长,可能需要用户自己来拉动滚动条,下面的例子实现了自动滚动的功能。其基本原理就是利用了linkbutton的锚点的功能,如果使用pushbutton,那还必须自己添加锚点。另外必须弄明白的是page的postback时的客户端脚本:
这段脚本中__dopostback函数有两个参数:第一个eventtarget是触发postback的控件的uniqueid;第二参数eventargument是一个对象,包含postback的额外信息。因此我们使用uniqueid来作为锚点的值。
源代码如下:
aspx” target=”_blank”>查看例子
datagridautoscroll.aspx
inherits=”aspxweb.datagridautoscroll”%>
代码:datagridautoscroll.aspx.vb
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 ”
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 = “”
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