从aspx到codebehind的ListView字符串

时间:2015-05-26 23:54:10

标签: c# asp.net listview listviewitem

您好我有以下aspx代码,当ItemTemplate内的按钮被cliked时,我需要将Item.BookID传递给后面的代码,我不确定如何做到这一点,因为视图中可能有多个项目。谢谢你的帮助将不胜感激。

<asp:ListView runat="server" ID="UserDetailBooks" DefaultMode="ReadOnly" ItemType="WebApplication1.Models.Borrowed" SelectMethod="GetBorrow" DeleteMethod="ReturnBook">

    <EmptyDataTemplate>
        <h3>No borrowed books!</h3>
    </EmptyDataTemplate>
    <LayoutTemplate>

        <div style="margin-left: auto; margin-right: auto; width: 50%;">
            <h4>Books in possesion:</h4>
            <table style="border-spacing: 2px;">

                <tr id="groupPlaceholder" runat="server">
                </tr>
            </table>
        </div>
    </LayoutTemplate>

    <GroupTemplate>
        <tr>

            <td id="itemPlaceholder" runat="server"></td>
        </tr>

    </GroupTemplate>

    <ItemTemplate>
        <td><asp:Button runat="server" Text="Return" OnClick="ReturnBook" />
            <a href="BookDetail.aspx?BookID=<%#:Item.BookID %>"><%#:Item.BookTitle %></a>      
        </td>
    </ItemTemplate>

</asp:ListView>

1 个答案:

答案 0 :(得分:0)

你可以在按钮属性中传递id吗?

<asp:Button runat="server" Text="Return" BookID="<%#:Item.BookID %>" OnClick="ReturnBook" />
<a href="BookDetail.aspx?BookID=<%#:Item.BookID %>"><%#:Item.BookTitle %></a> 

void ReturnBook(object sender, EventArgs e) {
 Button b = sender;
 string BookId = b.Attributes["BookID"];
}