Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Conditional logic in DataList?

Posted on 2004-10-21
18
Medium Priority
?
612 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
Comment
Question by:song2siren
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 8
18 Comments
 
LVL 18

Expert Comment

by:tusharashah
ID: 12369075
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
ID: 12369151
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
ID: 12369256
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:song2siren
ID: 12369407
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:
tusharashah earned 400 total points
ID: 12369572
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
ID: 12369595
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
ID: 12369784
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
ID: 12369941
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
ID: 12370012
Unfortunately this generates the error: Name 'bool' is not declared.

Thanks again
0
 
LVL 18

Expert Comment

by:tusharashah
ID: 12370063
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
ID: 12370105
Looks like this disables the HyperLink, but the button still displays.

Thanks again
0
 
LVL 18

Expert Comment

by:tusharashah
ID: 12370121
Where/how is the button?
0
 

Author Comment

by:song2siren
ID: 12370153
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
ID: 12370240
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
ID: 12370313
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
ID: 12370338
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
ID: 12370374
Many thanks for your comments - points well deserved!
0
 
LVL 18

Expert Comment

by:tusharashah
ID: 12370395
Nice to have you going song2siren!

Thanks for points :)

-tushar
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

636 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