简体   繁体   中英

ListView string from aspx to codebehind

Hello I have following aspx code and when the button inside ItemTemplate is cliked i need to pass the Item.BookID into code behind and I am unsure how to do it as there can be multiple items in the view. Thank you help would be much appreciated.

<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>

You could pass the id in the button attributes?

<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"];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM