按钮和文本框不会显示Visible = false

时间:2015-05-27 01:26:52

标签: c# asp.net .net formview

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

这是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,代码会说当前上下文中不知道文本框和按钮。

1 个答案:

答案 0 :(得分:0)

如果BookID具有值,则query可能不为null。您可能需要更改查询null的检查,如下所示:

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