用心做好每一件事情!

ASP.NET GridView 获取选中行某一列的值

后台在RowCommand里面增加如下代码:

复制代码
 1  protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
 2     {
 3         if (e.CommandName == "ED")
 4         {
 5             this.txtValueUserID.Text = e.CommandArgument.ToString();
 6             Control c = (Control)e.CommandSource;
 7             GridViewRow g = (GridViewRow)c.NamingContainer;
 8             this.txtValueUserPwd.Text = g.Cells[1].Text;
 9             this.txtValueAddress.Text = g.Cells[2].Text;
10         }
11     }
复制代码

“ED”表示给Command命令指定的一个名字,可以随便写。

前台代码:

复制代码
 1   <asp:UpdatePanel ID="UpdatePanel1" runat="server">
 2             <ContentTemplate>
 3                 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" 
 4     Text="查询数据" Width="137px" />
 5                 <asp:Button ID="btnDelete" runat="server" Text="删除数据" Width="137px" 
 6     OnClick="btnDelete_Click" />
 7                 <asp:Button ID="btnUpdate" runat="server" Text="修改数据" Width="137px" />
 8                 <asp:GridView ID="gv" runat="server"
 9             Height="130px" Width="474px" 
10             OnRowDataBound="gv_RowDataBound" AutoGenerateColumns="False" 
11             Font-Size="Small" onrowcommand="gv_RowCommand">
12                     <RowStyle HorizontalAlign="Center" />
13                     <Columns>
14                         <asp:BoundField DataField="USERID" HeaderText="UserID" />
15                         <asp:BoundField DataField="USERPWD" HeaderText="UserPassword" />
16                         <asp:BoundField DataField="UserAddress" HeaderText="UserAddress" />
17                         <asp:TemplateField HeaderText="Edit">
18                             <ItemTemplate>
19                                 <asp:ImageButton ID="imagebtnEdit" runat="server" 
20                             ImageUrl="~/image/icon_edit.gif"  CommandName="ED" 
21                             CommandArgument ='<%# Eval("UserID") %>' />
22                                 &nbsp;
23                                 <asp:ImageButton ID="imagebtnDelete" runat="server" 
24                             ImageUrl="~/image/icon_delete.gif"   CommandName="DE" 
25                              OnClientClick="Javascript:return confirm('are you sure?');" 
26                             CommandArgument ='<%# Eval("UserID") %>' />
27                             </ItemTemplate>
28                             <ItemStyle HorizontalAlign="Center" />
29                         </asp:TemplateField>
30                     </Columns>
31                 </asp:GridView>
32                 <br />
33                 <table style="width: 100%;">
34                     <tr>
35                         <td>
36                             <asp:TextBox ID="txtValueUserID" runat="server"></asp:TextBox>
37                         </td>
38                         <td>
39                             <asp:TextBox ID="txtValueUserPwd" runat="server"></asp:TextBox>
40                         </td>
41                         <td>
42                             <asp:TextBox ID="txtValueAddress" runat="server"></asp:TextBox>
43                         </td>
44                     </tr>
45                 </table>
46             </ContentTemplate>
47         </asp:UpdatePanel>
复制代码

 

最终效果如图所示,点击后面的编辑按钮“E”的时候,下方的文本框可以得到对应的值:

posted @   无 影  阅读(23438)  评论(10编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
作者: Allen【QQ:96966 1314】 网名:无影 出处: http://www.cnblogs.com/allen0118/ 声明: 本文版权归作者和博客园共有!转载时必须保留此段声明,且在文章页面明显位置给出原文连接。
点击右上角即可分享
微信分享提示