从gridview行检索数据到asp.net表单

时间:2013-06-26 03:48:37

标签: asp.net vb.net gridview

我想通过点击ImageButton(预订)从gridview中检索一行: 这是我的网格视图的代码:

    <asp:GridView ID="GridView1" runat="server" Height="150px" Width="284px" 
                         CssClass="tb" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">

                         <Columns>
                          <asp:TemplateField HeaderText=" Booking">
                         <ItemTemplate>
                              <asp:ImageButton ID="booking" runat="server"   HeaderText="Booking" ImageUrl="booking_icon.ico" PostBackUrl="form.aspx"/>
                         </ItemTemplate>
                         </asp:TemplateField>

                            <asp:BoundField DataField="Trade" HeaderText="Trade" SortExpression="Trade" />
                             <asp:BoundField DataField="CARRIER" HeaderText="CARRIER" 
                                 SortExpression="CARRIER" />
                            </Columns>
 </asp:GridView>

应该从gridview行提交的form.aspx页面的文本框:

<asp:TextBox ID="trade" runat="server" CssClass="input , focus"></asp:TextBox>
<asp:TextBox ID="carrier" runat="server" CssClass="input , focus"></asp:TextBox>

1 个答案:

答案 0 :(得分:0)

为图像按钮添加GridView RowCommand事件和命令参数,您可以在其中传递id或某些内容以确定当前行。

  <asp:GridView  onrowcommand="gvRowCommand" ID="GridView1" runat="server" Height="150px" Width="284px" 
                     CssClass="tb" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
 <ItemTemplate>
    <asp:ImageButton  CommandArgument='<%# Eval("SomeId") %>' ID="booking" runat="server"   HeaderText="Booking" ImageUrl="booking_icon.ico" />
</ItemTemplate>

C#

   protected void gvRowCommand(object sender, GridViewCommandEventArgs e)
   {
      var someId = e.CommandArgument;
   }

VB.Net

 Protected Sub gvRowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv.RowCommand

   Dim someId As Integer = Convert.ToInt32(e.CommandArgument)

End Sub

您可以参考msdn获取更多信息:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx