473,383 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

How do I Access a Datalist Item Variable via ASPX?

Here's my .aspx code.

Expand|Select|Wrap|Line Numbers
  1.                         <asp:DataList ID="DataList1" runat="server" RepeatLayout="Flow" RepeatDirection="Horizontal">
  2.                             <ItemTemplate>
  3.                                 <tr class="<%# Eval("TRClass") %>">
  4.                                     <td>
  5.                                         <div class="member_dir">
  6.                                             <img class="mb_logo" src="<%# Eval("Logo") %>" />
  7.                                             <div class="mb_info">
  8.                                                 <div class="_company"> <span id="ctl00_cphContent_RadGrid1_ctl01_ctl04_UserNameLabel"> <%# Eval("Company") %> </span> </div>
  9.                                                 <div class="_address"> <%# Eval("Address") %> <br /> <%# Eval("City") %>, <%# Eval("State") %> <%# Eval("Zip") %></div>
  10.                                                 <div class="_phone"> <%# Eval("Phone") %> </div>
  11.                                                 <% if (Eval("Website") == "")
  12.                                                    { 
  13.                                                 // Do Nothing
  14.                                                 } else { %>
  15.                                                 <div class="_link"> <a href="<%# Eval("Website") %>" target="_blank"> Visit Web Site </a> </div>                                                <% } %>
  16.                                            </div>
  17.                                         </div>
  18.                                     </td>
  19.                                 </tr>
  20.                             </ItemTemplate>
  21.                         </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:

"Databinding 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.
Apr 6 '10 #1
3 2918
CroCrew
564 Expert 512MB
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~
Apr 7 '10 #2
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
Apr 7 '10 #3
Frinavale
9,735 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. <div class="_link" class="<%# IIF(Eval("DisplayWebsite","displayClass","noShowClass") %>">
  2.   <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 "displayClass" otherwise it sets the css class to "noShowClass".

The displayClass and noShowClass css classes would be something like:
Expand|Select|Wrap|Line Numbers
  1. .displayClass{
  2.   display:block;
  3. }
  4. .noShowClass{
  5.   display:none;
  6. }
-Frinny
Apr 7 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Kaung Htut O via DotNetMonster.com | last post by:
Hi to all Is there a way to access Datalist Footer's Control properties from outside of datalist event. Pls note that I mean not a datagrid, only for datalist. Thanks Kaung Htut Oo
1
by: Machi | last post by:
let say i have a web form with a button on it ... when i click the button, it will pop up one small window to allow me to save a pdf file, everything is done using code behind as below ... however,...
3
by: Stimp | last post by:
I have a datalist that outputs a link to a picture in each ItemTemplate: e.g. <asp:DataList id="DataList1" ...etc> <ItemTemplate> <a href='Pic.aspx?pic=somenumber'>picture</a> </ItemTemplate>...
0
by: Vadivel Kumar | last post by:
I have a datalist item, lets say a my usercontrol... each and every time user clicks on the usercontrol i have to call an event which will display some alertboxes(javascript) based on the item...
2
by: Adam Knight | last post by:
Hi all, I am wanting to display a data lists item index inline in a script. Meaning not from code behind but <%# %> ect. Obviously i can display a dataitem by using: <%#...
5
by: c676228 | last post by:
Hi, I guess I am confused. In aspx script, I mean (you won't use Codebehind="enrollinfo.aspx.vb", but mix code with html and code together) You can access user control's property directly. Since I...
9
by: LamSoft | last post by:
Class B { public B() {} } Class A : B { public static string ABC = "myABC"; public A() {} }
4
by: sgogula | last post by:
Hi, Could someone help me how to access a JSP hidden variable in Java Script? Here is my code: <script type="text/javascript"> var mnuItems = hdnItem] </script>
4
by: Peter | last post by:
I have a DataList and each item in the datalist has an imge, I want to change the datalist item image on the client side when user clicks a button. How would I do that with JavaScript, does anyone...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.