2017-01-29 2 views
0

ButtonFieldからSelectedDataKey関数の値を取得するにはどうすればよいですか?それはヌルに戻り続けます。今、私はforeachループを作成して実際にどの値が利用可能かを見ていますが、この方法は理想的ではありません。GridViewのSelectDataKey

これは、ASPコード

<asp:GridView ID="GridViewResults" AutoGenerateColumns="False" runat="server" HorizontalAlign="Center" CellPadding="10" CellSpacing="10" Width="1100px" SelectedRow="hotelId" GridLines="Horizontal" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" ForeColor="Black" DataKeyNames="hotelId" OnRowCommand="GridViewResults_RowCommand" > 
        <Columns> 
         <asp:ImageField DataImageUrlField="thumbNailUrl" DataImageUrlFormatString="https://media.expedia.com{0}" HeaderText="Image" /> 
         <asp:BoundField DataField="name" HeaderText="Hotel Name" /> 
         <asp:BoundField DataField="locationDescription" HeaderText="Location" /> 
         <asp:BoundField DataField="hotelRating" DataFormatString="{0}/5" HeaderText="Hotel Rating" /> 
         <asp:BoundField DataField="lowRate" DataFormatString="£{0}" HeaderText="Lowest Price" /> 
         <asp:ButtonField Text="View" DataTextField="hotelId" HeaderText="View Hotel" ButtonType="Button" DataTextFormatString="{0}" /> 
<%--      <asp:HyperLinkField DataNavigateUrlFields="hotelId" HeaderText="View Hotel" DataNavigateUrlFormatString="http://api.ean.com/ean-services/rs/hotel/v3/info?apikey=5qjdrl3pjpj8f8gj6u7b40ofjk&sig=117f9d785e142c4be4285799ab8608b4&cid=488640&minorRev=99&customerUserAgent=Mozilla%2F5.0&customerIpAddress=84.246.168.11&locale=en_US&currencyCode=GBP&hotelId={0}" Text="View" />--%> 
        </Columns> 
        </asp:GridView> 

これは、各ループのためのロジックであるが、私は、選択された値を取得します。

protected void GridViewResults_RowCommand(object sender, GridViewCommandEventArgs e) 
      { 
       foreach (GridViewRow row in GridViewResults.Rows) 
       { 
        string _hotelId = GridViewResults.DataKeys[row.RowIndex].Value.ToString(); 
        var searchListRequest = new RestRequest("/ean-services/rs/hotel/v3/info", Method.GET); 

         string hotelId = _hotelId; 
} 
} 

答えて

0

あなたButtonFieldCommandName="hotelId"を追加し、GridViewResults_RowCommand内で次の操作を行います。

 protected void GridViewResults_RowCommand(object sender, GridViewCommandEventArgs e) 
     { 
      if (e.CommandName == "hotelId") 
      { 
       int rowindex = Convert.ToInt32(e.CommandArgument); 
       int hotelId = Convert.ToInt32(GridViewResults.DataKeys[rowindex].Value); 
      } 
     } 

希望します。 よろしく!