2012-09-17 61 views
2

ASPX獲取指標項目

<asp:SqlDataSource ID="SqlDataSource5" runat="server" 
     ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
     SelectCommand="SELECT * FROM [Themes] WHERE ([Theme] = @Theme) ORDER BY [Price]"> 
     <SelectParameters> 
      <asp:Parameter DefaultValue="Dubai-and-Beyond" Name="Theme" Type="String" /> 
     </SelectParameters>         
</asp:SqlDataSource> 
<asp:ListView ID="theme5" runat="server" DataSourceID="SqlDataSource5"> 
     <LayoutTemplate> 
      <div id="itemPlaceHolderContainer" runat="server"> 
       <span id="itemPlaceHolder" runat="server"></span> 
      </div> 
     </LayoutTemplate> 
<ItemTemplate> 
    <div> 
     <asp:Image ID="Destinationimage" runat="server" 
     ImageUrl='<%# Eval("ID", "~/CMS/ThemesHandler.ashx?ID={0}")+"&img=1"%>' 
     AlternateText="Destination_Image" Height="140px" Width="179px" /> 
     <asp:Label ID="lblcountry" runat="server" Text='<%#Eval("Country") %>' /> 
    </div>                       
    <div class="hotel_name">                
     <asp:Label ID="lblcountry" runat="server" Text='<%#Eval("Country") %>' /></div> 
     <asp:ImageButton ID="imgbtn5" runat="server" ImageUrl="images/book_nw.png" 
      OnClick="imgbtn5_Click" AlternateText="get_quote"/></div> 
    </ItemTemplate> 
    </asp:ListView> 

代碼背後

protected void imgbtn5_Click(object sender, EventArgs e) 
{   
    ListViewItem item = theme5.Items[0]; 
    Label country = (Label)item.FindControl("lblcountry"); 
    string con = country.Text.ToString(); 
    Session["country"] = con.ToString(); 
    Response.Redirect("Get_Quote.aspx"); 
} 

這裏,Theme5是列表視圖。在每個listview項中都有一個Label和imgbutton。 我想在imgbutton點擊事件上傳輸標籤值。 這裏的問題是我無法識別列表視圖項目的行索引。

回答

0

您可以在ListView中使用ImageButton的CommandNameCommandArgument。您可以從ItemCommand事件中訪問相應的項目。

<asp:ImageButton ID="imgbtn5" runat="server" ImageUrl="images/book_nw.png" 
     OnClick="imgbtn5_Click" AlternateText="get_quote" 
     CommandName="YOUR_COMMAND_NAME" 
     CommandArgument='<%#Eval("ANY_COLUMN_OF_SOURCE") %>' /> 

而且你的代碼隱藏文件中

protected void theme5_OnItemCommand(object sender, ListViewCommandEventArgs e) 
{ 
    if (String.Equals(e.CommandName, "YOUR_COMMAND_NAME")) 
    { 
    string arg = e.CommandArgument; // do whatever you want 
    ListViewDataItem dataItem = (ListViewDataItem)e.Item; 

} 
}