• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 621
  • Last Modified:

Conditional logic in DataList?

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
song2siren
Asked:
song2siren
  • 10
  • 8
1 Solution
 
tusharashahCommented:
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
 
song2sirenAuthor Commented:
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
 
tusharashahCommented:
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
song2sirenAuthor Commented:
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
 
tusharashahCommented:
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
 
tusharashahCommented:
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
 
song2sirenAuthor Commented:
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
 
tusharashahCommented:
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
 
song2sirenAuthor Commented:
Unfortunately this generates the error: Name 'bool' is not declared.

Thanks again
0
 
tusharashahCommented:
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
 
song2sirenAuthor Commented:
Looks like this disables the HyperLink, but the button still displays.

Thanks again
0
 
tusharashahCommented:
Where/how is the button?
0
 
song2sirenAuthor Commented:
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
 
tusharashahCommented:
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
 
song2sirenAuthor Commented:
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
 
tusharashahCommented:
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
 
song2sirenAuthor Commented:
Many thanks for your comments - points well deserved!
0
 
tusharashahCommented:
Nice to have you going song2siren!

Thanks for points :)

-tushar
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 10
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now