简体   繁体   English

ListView字符串从aspx到codebehind

[英]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. 您好,我有以下aspx代码,当单击ItemTemplate的按钮时,我需要将Item.BookID传递到后面的代码中,由于该视图中可能有多个项目,因此我不确定如何执行此操作。 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? 您可以在按钮属性中传递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"];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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