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”的时候,下方的文本框可以得到对应的值:

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

上一篇:C# 调用Lotus Notes发送邮件


下一篇:SQL Server 2008 R2英文版安装图解