问题描述
搜索的时候没在这搜到答案,但看到有人提出了同样的问题,已结贴不能回复,我就把我在别处找到的文章转过来吧,希望能帮到小忙。“好久没有搞ASP.Net了,发现有好多的东西都忘记了,现在在这里记录一下,以后用的时候可以拿出来看看关于Silverlight的记录现在实在是没有时间了,上个项目刚搞完,这个项目马上就来了,还得天天加班,一点空闲都没有,反正得抽点事件来整理一下废话不说了,整理一下首先在GridView中必须加上“选择”的按钮,然后在Page_Load中添加:protectedvoidpage_load(objectsender,eventargse){gvtest.selectedindexchanged+=neweventhandler(gridview1_selectedindexchanged);}最后在点击绑定的GridView项的时候就可以触发SelectedIndexChanged事件了。这里我把“选择”按钮设置成了TemplateField,然后让它的Text绑定Email字段,数据库的其余字段也可以绑定到GridView上,但是在这个程序里我把他们的可见属性都设置成了False。页面代码:body><formid="form1"runat="server"><divstyle="overflow-y:auto;width:100%;height:100%"><asp:gridviewid="gvtest"runat="server"autogeneratecolumns="false"datakeynames="userid"datasourceid="sqldatasource1"showheader="false"><columns><asp:boundfielddatafield="userid"headertext="userid"insertvisible="false"readonly="true"sortexpression="userid"visible="false"/><asp:boundfielddatafield="email"headertext="email"sortexpression="email"visible="false"/><asp:boundfielddatafield="passwd"headertext="passwd"sortexpression="passwd"visible="false"/><asp:boundfielddatafield="lastlogin"headertext="lastlogin"sortexpression="lastlogin"visible="false"/><asp:templatefieldshowheader="false"><itemtemplate><asp:linkbuttonid="linkbutton1"runat="server"causesvalidation="false"commandname="select"text='<%#bind("email","{0}")%>'></asp:linkbutton></itemtemplate></asp:templatefield></columns></asp:gridview><br/><br/><asp:labelid="lblregioncaption"runat="server"text="label"></asp:label><br/><asp:sqldatasourceid="sqldatasource1"runat="server"connectionstring="<%$connectionstrings:vmessagecenterconnectionstring%>"selectcommand="select[userid],[email],[passwd],[lastlogin]from[users]"></asp:sqldatasource></div></form>//////////////////////////////////////////////////////////////////////////////SelectIndexChenged事件的代码:protectedvoidgridview1_selectedindexchanged(objectsender,eventargse){intindex=gridview1.selectedindex;stringid=gridview1.selecteddatakey.values["userid"].tostring();lblregioncaption.text=id;gridviewrowrow=gridview1.selectedrow;}”//FROM:http://biancheng.dnbcw.info/javascript/362440.html,作者:cpcpc
解决方案
解决方案二:
protectedvoidpage_load(objectsender,eventargse){gvtest.selectedindexchanged+=neweventhandler(gridview1_selectedindexchanged);}=》protectedvoidpage_load(objectsender,eventargse){if(!IsPostBack){gvtest.selectedindexchanged+=neweventhandler(gridview1_selectedindexchanged);}}
解决方案三:
referhere: