按钮和文本框不会变为 Visible=false

Button and text box wont go Visible=false

您好,我有一个页面显示从数据库中检索到的一些信息。当我从数据库中获取空数据集时,我想隐藏一个文本框和一个按钮。

这是 aspx 代码

<asp:FormView ID="BookDetailView" runat="server" ItemType="WebApplication1.Models.Book" SelectMethod="GetBook" DeleteMethod="DeleteBook" RenderOuterTable="false">
    <EmptyDataTemplate>
        <h1>No book found!</h1>

    </EmptyDataTemplate>
    <ItemTemplate>
        <div style="margin-left: auto; margin-right: auto; width: 50%;">
            <div>


                <h1><b>
                    <asp:TextBox runat="server" BorderStyle="None" ReadOnly="true" Text="<%#:Item.Title %>" ID="bookTitleTextBox"></asp:TextBox></b></h1>

            </div>



            <br />
            <table style="margin-left: auto; margin-right: auto; width: 50%;">
                <tr>

                    <td>&nbsp;</td>
                    <td style="vertical-align: top; text-align: left;">
                        <span><b>Book ID: </b><asp:TextBox runat="server" BorderStyle="None" ReadOnly="true" Text="<%#:Item.BookID %>" ID="bookIDTextBox"></asp:TextBox></span>

                        <br />
                        <span><b>Author:</b>&nbsp;<%#:Item.Author %></span>



                        <br />
                        <span><b>Published:</b>&nbsp;<%#:Item.Published %></span>
                        <br />
                        <span><b>Is borrowed:</b>&nbsp;<asp:TextBox runat="server" BorderStyle="None" ReadOnly="true" Text="<%#:Item.Borrowed %>" ID="isBorrowedTextBox"></asp:TextBox></span>
                        <br />
                        <asp:Button runat="server" Text="Delete" OnClick="DeleteBook" />

                        <br />



                    </td>
                </tr>
            </table>


        </div>
    </ItemTemplate>

</asp:FormView>


<asp:TextBox runat="server" placeholder="Borrow to user ID" ID="inputUserBorrow"></asp:TextBox>
<asp:Button runat="server" ID="inputUserBorrowButton" Text="Borrow" Style="margin-left: 5px; margin-bottom: 10px;" OnClick="borrow" />
<br />

而这就是后面代码中的select方法

 public IQueryable<Book> GetBook([QueryString("BookID")] int? BookID)
    {
        var _db = new WebApplication1.Models.LibraryContext();
        IQueryable<Book> query = _db.Books;
        if (BookID.HasValue && BookID > 0)
        {

            query = query.Where(p => p.BookID == BookID);
        }
        else
        {
            query = null;


        }

        if (query == null) {
            inputUserBorrow.Visible = false;
            inputUserBorrowButton.Visible = false;
        }
        return query;
    }

感谢您的帮助

PS。如果我放入 ItemTemplate,代码会说文本框和按钮在当前上下文中是未知的。

因为如果 BookID 有值,query 可能不为 null。您可能需要像这样更改对查询 null 的检查:

    if (query == null || query.Count() == 0) {
        inputUserBorrow.Visible = false;
        inputUserBorrowButton.Visible = false;
    }