如何访问gridview特定的行数据

时间:2016-08-17 08:24:19

标签: asp.net gridview

我在asp.net页面中有一个gridview控件,其中一些值在boundfield中,一些在模板字段中。我想在单击选择按钮时绑定特定记录,相应的值应绑定到页面上的不同控件(如我有文本框,单选按钮,下拉列表等)。 这是我的代码:

<asp:GridView ID="gvrooms" OnRowCommand="gvrooms_RowCommand"  OnRowDataBound="gvrooms_RowDataBound1" runat="server"  AutoGenerateColumns="false" >
      <Columns>
          <asp:TemplateField>
                <ItemTemplate>
                     <asp:HiddenField ID="hfID" Value='<%#Eval("RoomID") %>' runat="server" Visible="false" />
                     <asp:HiddenField ID="hfexbed" Value='<%# Eval("ExtraBed") %>' runat="server" Visible="false" />
                     <asp:HiddenField ID="hfnoofexbed" Value='<%# Eval("NoOfExtraBed") %>' runat="server" Visible ="false" />
                     <asp:HiddenField ID="hfdesc"  Value='<%# Eval("Description") %>' runat="server" Visible="false" />
             </ItemTemplate>
        </asp:TemplateField>
       <asp:BoundField HeaderText="Room No" DataField="RoomNo" />
     <asp:BoundField HeaderText="Room Type" DataField="RoomType" />
      <asp:BoundField HeaderText="Floor No" DataField="FloorNo" />
       <asp:BoundField HeaderText="Room Status" DataField="RoomStatus" />
        <asp:BoundField HeaderText="Capacity" DataField="Capacity" />
        <asp:BoundField HeaderText="Room Price" DataField="RoomPrice" />
         <asp:TemplateField>
              <ItemTemplate>
                   <asp:LinkButton Text="Select" ID="lnkSelect" CommandName="Select" runat="server"></asp:LinkButton>
              </ItemTemplate>
        </asp:TemplateField> 
 </Columns>
</asp:GridView>

1 个答案:

答案 0 :(得分:0)

 <asp:TemplateField>
              <ItemTemplate>
                   <asp:LinkButton Text="Select" ID="lnkSelect" CommandName="Select" runat="server" 
OnClick="lnkSelect_Click"></asp:LinkButton>
                  </ItemTemplate>
        </asp:TemplateField> 

将BoundField转换为TemplateFiled和On按钮选择单击“查找控件”

     protected void lnkSelect_Click(object sender, EventArgs e)
     {
       GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;

       Label lbroomprice= (Label)clickedRow.FindControl("lbroomprice");
       string RoomPrice = lbroomprice.Text;
     }