Solved

Conditional logic in DataList?

Posted on 2004-10-21
594 Views
Last Modified: 2012-05-05
Hello

I've created the following function to add an 'out of stock' message to a DataList:

    Protected Function CheckInStock(InStock As Object) As String
   
        If InStock = False Then
            Return "<span class='Text'>Out Of Stock</span>"
        Else
            Return InStock.ToString()
        End If
   
    End Function

However, if InStock is false, I also need to disable the HyperLink and ImageButton controls below:

<asp:HyperLink NavigateUrl='<%# "AddToCart.aspx?productID=" & (Container.DataItem( "ID" ))%>' Runat="server"><asp:ImageButton ID="AddtoCart" ImageUrl="Images/addtocart.gif" runat=server /></asp:HyperLink>

I'm sure this is fairly simple but I'm really struggling. Any help would be much appreciated.

Thanks
0
Question by:song2siren
    18 Comments
     
    LVL 18

    Expert Comment

    by:tusharashah
    Just add ID of your HyperLink in your code like below:

    <asp:HyperLink id="HyperLink1" NavigateUrl='<%# "AddToCart.aspx?productID=" & (Container.DataItem( "ID" ))%>' Runat="server"><asp:ImageButton ID="AddtoCart" ImageUrl="Images/addtocart.gif" runat=server /></asp:HyperLink>

    & modify your function like:
    Protected Function CheckInStock(InStock As Object) As String
       
            If InStock = False Then
                HyperLink1.Enabled = false
                AddtoCart.Enabled = false
                Return "<span class='Text'>Out Of Stock</span>"
            Else
                Return InStock.ToString()
            End If
       
        End Function

    -tushar
    0
     

    Author Comment

    by:song2siren
    Hi tushar

    Yes, I've tried this but get the following exception: 'System.NullReferenceException: Object reference not set to an instance of an object'.

    It doesn't seem to like the HyperLink1.Enabled = false part of the function.

    Thanks again.
    0
     
    LVL 18

    Expert Comment

    by:tusharashah
    How exactly are you setting up HyperLink in your page? Drag & drop one Separate HyperLink and check out for that HyperLink.

    Also, You can modify your HyperLink & ImageButton like following:
    <asp:HyperLink id="HyperLink1" NavigateUrl='<%# "AddToCart.aspx?productID=" & (Container.DataItem( "ID" ))%>' Runat="server" ImageUrl="Images/addtocart.gif"></asp:HyperLink>

    -tushar
    0
     

    Author Comment

    by:song2siren
    The HyperLink is an part of the ItemTemplate of my DataList. The DataList is generated via a stored procedure using the following:

          Sub getResultsbySubject(Source as Object, e as EventArgs)
                myDataAdapter = New SqlDataAdapter("JPL_Pubs_SearchbySubject", myConnection)
                myDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure
                myDataAdapter.SelectCommand.Parameters.Add(New SqlParameter("@Search", SqlDbType.NVarChar, 255))
              myDataAdapter.SelectCommand.Parameters("@Search").Value = frmSubjects.selectedvalue
                resultDS = New DataSet()
                myDataAdapter.Fill(resultDS, "Results")
                dlResults.DataSource = resultDS.Tables("Results").DefaultView
                Session("resultDSDataTable") = resultDS.Tables("Results")
                BindDataList
                myDataAdapter.Dispose()
                myConnection.Close
          End Sub

          Sub BindDataList
                dlResults.DataBind()
              ' Display a message if no results are found
               If dlResults.Items.Count = 0 Then
                  ErrorMsg.Text = "No items matched your query."
                      else
                      ErrorMsg.Text = ""
               End If
          End Sub

    Thanks again
    0
     
    LVL 18

    Accepted Solution

    by:
    Well... if HyperLink is part of a DataList then you cannot just reference it like HyperLinke1.Enabled for that you need to write down differently.

    Now, where exactly are you checking for Stock? I believe you might have something coming out from DataSet. If InStock is coming out from your Database and is true/false then you can just setup your HyperLink like following:

    <asp:HyperLink id="HyperLink1" NavigateUrl='<%# "AddToCart.aspx?productID=" & (Container.DataItem( "ID" ))%>' Enabled='<%# Container.DataItem( "InStock" )%>' Runat="server" ImageUrl="Images/addtocart.gif"></asp:HyperLink>

    -tushar

    0
     
    LVL 18

    Expert Comment

    by:tusharashah
    Now if you want to check for Stock somewhere in code, then you can set up OnDataItemBound event for DataList and modify the HyperLink over there.
    ----------------------------------------------------------------------------------------------------------------------------------------
    private void dlResults_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
    {
          switch( e.Item.ItemType )
          {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                      int iStock = Convert.ToInt32( ((Label) e.Item.FindControl( "InStock" )).Text );            
                      if( iStock < 1 )
                         {
                            ((HyperLink) e.Item.FindControl( "hlPageNo" )).Enabled = false;
                      }
                      
                break ;
          }
    }
    ----------------------------------------------------------------------------------------------------------------------------------------

    -tushar
    0
     

    Author Comment

    by:song2siren
    Yes, the InStock comes from the database, but the 'addtocart' image is still displaying with the HyperLink set up as:

    <asp:HyperLink id="HyperLink1" NavigateUrl='<%# "AddToCart.aspx?productID=" & (Container.DataItem( "ID" ))%>' Enabled='<%# Container.DataItem( "InStock" )%>' Runat="server" ImageUrl="Images/addtocart.gif"></asp:HyperLink>

    Thanks again.
    0
     
    LVL 18

    Expert Comment

    by:tusharashah
    Try this way:

    <asp:HyperLink id="HyperLink1" NavigateUrl='<%# "AddToCart.aspx?productID=" & (Container.DataItem( "ID" ))%>' Enabled='<%# (bool)DataBinder.Eval(Container.DataItem, "InStock") %>' Runat="server" ImageUrl="Images/addtocart.gif"></asp:HyperLink>

    -tushar

    0
     

    Author Comment

    by:song2siren
    Unfortunately this generates the error: Name 'bool' is not declared.

    Thanks again
    0
     
    LVL 18

    Expert Comment

    by:tusharashah
    Just noticed that you are using VB.. try this way:

    <asp:HyperLink id="HyperLink1" NavigateUrl='<%# "AddToCart.aspx?productID=" & (Container.DataItem( "ID" ))%>' Enabled='<%# CType(DataBinder.Eval(Container.DataItem, "InStock"), Boolean) %>' Runat="server" ImageUrl="Images/addtocart.gif"></asp:HyperLink>

    -tushar
    0
     

    Author Comment

    by:song2siren
    Looks like this disables the HyperLink, but the button still displays.

    Thanks again
    0
     
    LVL 18

    Expert Comment

    by:tusharashah
    Where/how is the button?
    0
     

    Author Comment

    by:song2siren
    In the ImageUrl="Images/addtocart.gif part of the Hyperlink is still causing my button to display. When I disable the Hyperlink, I don't want the image to display either.

    Sorry for any confusion.
    0
     
    LVL 18

    Expert Comment

    by:tusharashah
    For that you'll have to write down OnItemDataBound event for DataList, like following:

    -------------------------------------------------------------------------------------------------------------------------------------
    Private Sub dlResults_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)
     Select e.Item.ItemType
     Case ListItemType.Item, ListItemType.AlternatingItem
       Dim _hl As HyperLink = CType(e.Item.FindControl("HyperLink1"), HyperLink)
       If Not _hl.Enabled Then
         CType(e.Item.FindControl("HyperLink1"), HyperLink).ImageUrl = ""
       End If
     End Select
    End Sub
    -------------------------------------------------------------------------------------------------------------------------------------

    -tushar
    0
     

    Author Comment

    by:song2siren
    This seems to work though:

    <asp:HyperLink ID="pubsLink" NavigateUrl='<%# "AddToCart.aspx?productID=" & (Container.DataItem( "ID" ))%>' Enabled='<%# CType(DataBinder.Eval(Container.DataItem, "InStock"), Boolean) %>' Runat="server" ><asp:ImageButton ID="AddtoCart" ImageUrl="Images/addtocart.gif" Visible='<%# Container.DataItem("InStock") %>' runat=server /></asp:HyperLink>

    I've just separated the image from the link and put the Instock value in the enabled property of the Hyperlink, and in the Visible property of the ImageButton. Not sure if there's any reason why I shouldn't do it like this?

    Thanks again.
    0
     
    LVL 18

    Expert Comment

    by:tusharashah
    You didn't needed an ImageButton for your purpose. But this seems easier! So, dosen't matter;) You can go ahead with this approach.

    -tuchar
    0
     

    Author Comment

    by:song2siren
    Many thanks for your comments - points well deserved!
    0
     
    LVL 18

    Expert Comment

    by:tusharashah
    Nice to have you going song2siren!

    Thanks for points :)

    -tushar
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

     Java Android Coding Bundle

    Whether you're an Apple user or Android addict, learning to code for the Android platform is an extremely valuable, in-demand skill. It all starts with Java, the language behind the apps and games that make Android the top platform it is today.

    Suggested Solutions

    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now