Search code examples
c#asp.net.netformview

Button and text box wont go Visible=false


Hello I have a page which displays some information retrieved from the database. And I have a textbox and a button I want to hide when I get emptydataset from the database.

This is the aspx code

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

And this is the select method in the code behind

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

Thank you for your help

PS. If i put into the ItemTemplate the code will say that the textbox and the button are not know in the current context.


Solution

  • Because query may not be null if BookID has value. You may need to change the check for query null like this:

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