asp.net|[ASP.NET]GridView触发SelectedIndexChanged事件
首先在GridView中必须加上“选择”的按钮,然后在Page_Load中添加
view plain copy to clipboard print ?
- protected void Page_Load(object sender, EventArgs e)
- {
- gvTest.SelectedIndexChanged+=new EventHandler(GridView1_SelectedIndexChanged);
- }
这里我把“选择”按钮设置成了TemplateField,然后让它的Text绑定Email字段,数据库的其余字段也可以绑定到GridView上,但是在这个程序里我把他们的可见属性都设置成了False。
页面代码:
view plain copy to clipboard print ?
- "overflow-y: auto; width: 100%; height: 100%">
- "gvTest" runat="server" AutoGenerateColumns="False"
- DataKeyNames="UserId" DataSourceID="SqlDataSource1" ShowHeader ="false">
- "UserId" HeaderText="UserId" InsertVisible="False"
- ReadOnly="True" SortExpression="UserId" Visible="False" />
- "EMail" HeaderText="EMail" SortExpression="EMail"
- Visible="False" />
- "Passwd" HeaderText="Passwd" SortExpression="Passwd"
- Visible="False" />
- "LastLogin" HeaderText="LastLogin"
- SortExpression="LastLogin" Visible="False" />
- "False">
- "LinkButton1" runat="server" CausesValidation="False"
- CommandName="Select" Text=''>
- "lblRegionCaption" runat="server" Text="Label">
- "SqlDataSource1" runat="server"
- ConnectionString=""
- SelectCommand="SELECT [UserId], [EMail], [Passwd], [LastLogin] FROM [Users]">
【asp.net|[ASP.NET]GridView触发SelectedIndexChanged事件】SelectIndexChenged事件的代码:
view plain copy to clipboard print ?
- protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
- {
- int index = GridView1.SelectedIndex;
- string ID = GridView1.SelectedDataKey.Values["UserId"].ToString();
- lblRegionCaption.Text = ID;
- GridViewRow row = GridView1.SelectedRow;
- }
推荐阅读
- ASP.NET|ASP.NET Core应用开发思维导图
- asp.net|asp.net core中使用log4net
- Asp.net|System.Globalization.DateTimeFormatInfo.InvariantInfo
- asp.net|c#文件写入与获取post请求数据
- .NET+C#|掌握 ASP.NET 之路(自定义实体类简介(Microsoft))
- .NET+C#|ASP.NET 2.0中Theme、MasterPage和代码国际化
- 快速理解ASP.NET Core的认证与授权
- .NetCore|从零开始学习 asp.net core 3.1 web api 后端api基础框架(一)-环境介绍
- .NetCore|从零开始学习 asp.net core 3.1 web api 后端api基础框架(七)-添加一个查询单笔数据的方法
- 一些不错的ASP.NET 学习网站