Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am building an ASP.net raffle ticket system. I have a handfull of different raffles that will list details about them (type of raffle, prize won, date of drawling and prize of a ticket) below that i have a link formatted to look like a button that when clicked will take the user to a page to add this raffle to their cart. if the Raffle has had a winner, the winning ticket is displayed on the list of raffles, but the purchase button is still displayed, how can i hid this button. i have placed it in a place holder, but can't figure this next part out.

ASP.NET
 <asp:ListView ID="ListView1" runat="server" DataKeyNames="raffleID" DataSourceID="SqlDataSource1" GroupItemCount="3" ondatabound="ListView1_DataBound">

     <GroupTemplate>
         <tr id="itemPlaceholderContainer" runat="server">
             <td id="itemPlaceholder" runat="server"></td>
         </tr>
     </GroupTemplate>

     <ItemTemplate>
         <td runat="server" style="">

                 <div style="overflow: hidden;  height: 300px; width: 350px;  border-bottom-style:solid; border-bottom-color: black; border-bottom-width:1px; margin-right: 15px; margin-bottom:15px;">
                     <div style="height:70%;">
    <div style="float: left; height: 100%; width:30%;">
         <image src='images/raffles/<%# Eval("imagepath") %>'
              width="100px" height="100px" border="0">
    </div>
    <div style="float: left; height: 100%; width:65%;">

        <div>Type: <asp:Label ID="TypeLabel" runat="server" Text='<%# Eval("Type") %>' /></div>
        <div>Description: <asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' /></div>
        <div>Prize: <asp:Label ID="PrizeLabel" runat="server" Text='<%# Eval("Prize") %>' /></div>
        <div>Cost: $<asp:Label ID="CostLabel" runat="server" Text='<%# Eval("Cost") %>' />\Ticket 5 for price of 4</div>
        <div>Drawling Date: <asp:Label ID="Label1" runat="server" Text='<%# Eval("DrawlingDate") %>' /></div>
        <div>Winning Ticket: <asp:Label ID="Label2" runat="server" Text='<%# Eval("TicketID") %>' /></div>
    </div>
</div>
<div style="text-align:left;">
    <asp:PlaceHolder ID="purchasebutton" runat="server" Visible='true'>
        <a class="btn btn-primary btn-lg" href="purchasetickets.aspx?raffleid=<%# Eval("raffleID") %>&ticketcost=<%# Eval("Cost") %>">Purchase Tickets »</a>
    </asp:PlaceHolder>
 </div>
                 </div>

      </td>

     </ItemTemplate>
     <LayoutTemplate>
         <table runat="server">
             <tr runat="server">
                 <td runat="server">
                     <table id="groupPlaceholderContainer" runat="server" border="0" style="">
                         <tr id="groupPlaceholder" runat="server">
                         </tr>
                     </table>
                 </td>
             </tr>
             <tr runat="server">
                 <td runat="server" style=""></td>
             </tr>
         </table>
     </LayoutTemplate>

 </asp:ListView>


basically, if the value of Eval("TicketID") is dbbull (no winner picked) set the visible property of purchasebutton place older to true, otherwise (a 6 digit number repersenting the winning ticket), set the visible property of purchasebutton to false

What I have tried:

i have tried this, but it doesn't work

ASP.NET
<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible='<%# (Eval("ImageURL") == DBNull.Value ? "true" : "False") %>'>
    <a class="btn btn-primary btn-lg" href="purchasetickets.aspx?raffleid=<%# Eval("raffleID") %>&ticketcost=<%# Eval("Cost") %>">Purchase Tickets »</a>
</asp:PlaceHolder>
Posted

The easiest way to do this is to use a code-behind approach. In your 'ItemDataBound' event of the 'ListView', you can find the 'PlaceHolder' control and set its visibility accordingly -

C#
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        //Find your PlaceHolder control...
        PlaceHolder purchaseButtonPlaceholder = (PlaceHolder)e.Item.FindControl("purchasebutton");

        //Find the Label control that displays the TicketID, change Label2 to your actual control name...
        Label ticketIdLabel = (Label)e.Item.FindControl("Label2");

        //Check if the TicketID has returned 'dbbull' (no winner picked)...
        if (ticketIdLabel.Text.Trim().ToLower() == "dbbull")
        {
            //Show your purchase button...
            purchaseButtonPlaceholder.Visible = true;
        }
        else
        {
            //Hide your purchase button...
            purchaseButtonPlaceholder.Visible = false;
        }
    }
}


In your ASPX page, you need to attach this event handler to your 'ListView' -
ASPX
<asp:ListView ID="ListView1" runat="server" DataKeyNames="raffleID" DataSourceID="SqlDataSource1" GroupItemCount="3" OnItemDataBound="ListView1_ItemDataBound">
    <!-- Your current ListView markup code here... -->
</asp:ListView>
 
Share this answer
 
Comments
Dino the Sink 12-Mar-24 20:40pm    
Thank you, i will give it a try and let you know. I am using VB.net instead of C#, but i think i get the jist
Andre Oosthuizen 13-Mar-24 11:51am    
You're welcome. I missed the .NET part sorry, basics will be the same though.
You were so nearly there! :)

The Visible property is a bool, but you are trying to set it to a string.

Change the markup to:
ASP.NET
<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible='<%# Eval("ImageURL") == DBNull.Value ? true : false %>'>
Or, more simply:
ASP.NET
<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible='<%# Convert.IsDBNull(Eval("ImageURL")) %>'>
 
Share this answer
 
v2
Comments
Dino the Sink 22-Mar-24 2:12am    
the first example did not work. the complier didn't like the ? and the True and false declarations. The second example did work, The first response i got, helps a bit more. I am doing a few other operations now in the OnItemDataBound event now. But I can use this for other things!
Richard Deeming 22-Mar-24 4:50am    
That's odd; are you using C# or VB? What error message did you get?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900