sub gridview1_databound(byval sender as object, byval e as system.eventargs) dim row as gridviewrow dim selectedvalue as string = viewstate("selectedvalue") if selectedvalue is nothing then return end if ' 检测选择的行是哪个并重新选中它 for each row in gridview1.rows dim keyvalue as string = gridview1.datakeys(row.rowindex).value if (keyvalue = selectedvalue) then gridview1.selectedindex = row.rowindex end if next end sub
protected sub gridview1_selectedindexchanged(byval sender as object, byval e as system.eventargs) ' 保存选中行的索引 if (not gridview1.selectedindex = -1) then viewstate("selectedvalue") = gridview1.selectedvalue end if end sub
protected sub gridview1_pageindexchanging(byval sender as object, byval e as system.web.ui.webcontrols.gridviewpageeventargs) ' 重置选中索引 gridview1.selectedindex = -1 end sub
sub gridview1_sorting(byval sender as object, byval e as system.web.ui.webcontrols.gridviewsorteventargs) ' 重置选择索引 gridview1.selectedindex = -1 end sub