How do I Access a Datalist Item Variable via ASPX?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MStonePC
    New Member
    • Apr 2010
    • 1

    How do I Access a Datalist Item Variable via ASPX?

    Here's my .aspx code.

    Code:
                            <asp:DataList ID="DataList1" runat="server" RepeatLayout="Flow" RepeatDirection="Horizontal">
    		                    <ItemTemplate>
    			                    <tr class="<%# Eval("TRClass") %>">
    			                        <td>
    			                            <div class="member_dir">
    			                                <img class="mb_logo" src="<%# Eval("Logo") %>" />
    			                                <div class="mb_info">
    			                                    <div class="_company"> <span id="ctl00_cphContent_RadGrid1_ctl01_ctl04_UserNameLabel"> <%# Eval("Company") %> </span> </div>
    			                                    <div class="_address"> <%# Eval("Address") %> <br /> <%# Eval("City") %>, <%# Eval("State") %> <%# Eval("Zip") %></div>
                                                    <div class="_phone"> <%# Eval("Phone") %> </div>
                                                    <% if (Eval("Website") == "")
                                                       { 
    												// Do Nothing
    												} else { %>
                                                    <div class="_link"> <a href="<%# Eval("Website") %>" target="_blank"> Visit Web Site </a> </div>                                            	<% } %>
                                               </div>
                                            </div>
    			                        </td>
    			                    </tr>
    		                    </ItemTemplate>
    	                    </asp:DataList>

    I need to make it so that the "Visit web site" link doesn't show up, if there is no url present.. But
    <% if (Eval("Website" ) == "") does not work..
    I get the following error:

    "Databindin g methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."

    I'm more of a php guy, so this is a little over my head.. IF anyone can provide any info, it'll be greatly appreciated.
    Last edited by Frinavale; Apr 7 '10, 07:51 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • CroCrew
    Recognized Expert Contributor
    • Jan 2008
    • 564

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Thanks,
    CroCrew~

    Comment

    • StevieMars
      New Member
      • Mar 2010
      • 21

      #3
      I think your best bet is to make the link an asp.net control rather than a standard HTML link. You can then databind the Visible attribute to say something like:

      <%# Visible='<%# Eval("Website") == "" %>

      That should set the visibility of the link to true or false.

      Steve

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        I'm not sure if StevieMars's suggestion is going to work...actually I'm pretty sure that it will not work because I remember trying to set the visibility of an ASP.NET control based on the data that the parent control was being bound to and had lots of issues. It had to do with how decaritive binding works in ASP.NET

        Databinding in ASP.NET is not done using function calls in ASP.NET. It's done using a parser and so there are many limitations to the declaritive databinding (this is what you're using here).

        I would recommend that you use the IIF or IF method to accomplish what you're looking for:
        Code:
        <div class="_link" class="<%# IIF(Eval("DisplayWebsite","displayClass","noShowClass") %>">
          <a href="<%# Eval("Website") %>" target="_blank"> Visit Web Site </a>   </div>
        You would have to add a field to your data source though: DisplayWebsite, which would be a Boolean value used to indicate whether or not to display the website link. The above code will execute the IIF() method...if it's true then it set's the css class to "displayCla ss" otherwise it sets the css class to "noShowClas s".

        The displayClass and noShowClass css classes would be something like:
        Code:
        .displayClass{
          display:block;
        }
        .noShowClass{
          display:none;
        }
        -Frinny

        Comment

        Working...