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。每个列表视图项中都有一个标签和 imgbutton。我想在 imgbutton 点击​​事件上传输标签值。这里的问题是我无法识别列表视图项的行索引。

4

1 回答 1

0

You can make use of CommandName and CommandArgument of your ImageButton within your ListView. You may than access the according item from within your ItemCommand event.

<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") %>' />

And within your codeBehind file

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;

 }
}
于 2012-09-17T13:25:33.483 回答