How to use 'if' with Databinder.Eval(Container.DataItem)

Based on the value of a string field of Container.DataItem I want to either have the text be a hyperlink or not, but I cannot figure out  how to do it. I tried this  but  it did not work. I get an error 'Container not valid in this context'.

<asp:TemplateField HeaderText="TheHeaderText">


    <% if (DataBinder.Eval(Container.DataItem, "Type") == "SomeType")   { %>

        <asp:HyperLink runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"SomeProperty") %>' NavigateUrl='
http://someURL' />

    <% }    else    { %>

        <%# DataBinder.Eval(Container.DataItem,"SomeProperty") %>

    <% } %>



I am pretty sure this problem has been solved countless times but I cannot seem to find out how. Does anybody know how to do this?


10/11/2007 2:16:54 PM 72751 articles. 3 followers. Follow

7 Replies

Similar Articles

[PageSpeed] 55

You can use 

In C# 

 <asp:HyperLink runat="server" Text='<%# Eval("fieldname")=="SomeType"?"something":"something else" %>' NavigateUrl='http://someURL' />

In VB 

 <asp:HyperLink runat="server" Text='<%# iif(Eval("fieldname")="SomeType","something","something else") %>' NavigateUrl='http://someURL' />

You can also use a public function in your page that returns a string in the binding expression. In this case, all the logic goes into the function 

 <asp:HyperLink runat="server" Text='<%# myFunction(Eval("fieldname")) %>' NavigateUrl='http://someURL' /> 


Dont forget to click "Mark as Answer" on the post that helped you.
10/11/2007 2:23:50 PM

Thanks for the response. The thing is - I want there to either be a hyperlink or just text (no hyperlink) depending on the value of the  'Type' property.

10/11/2007 2:36:12 PM

In that case, just add both to your page (a hyperlink and a label), and bind the Visible property of each:

Visible=<%# Eval("type").ToString()=="value1" %>


Visible=<%# Eval("type").ToString()!="value1" %>


10/11/2007 2:43:01 PM

Are you talking about the Visible property of asp:TemplateField? I just tried that and I get an error - DataBinding expressions are only supported on objects that have a DataBinding event. Am I doing  something wrong?

10/11/2007 3:18:09 PM

I think he means the visible property of your hyperlink and label controls

Mark this post as the "Answer" if it was the answer.
10/11/2007 3:31:21 PM

Thanks all for your help. That did it.

10/11/2007 5:18:38 PM

I tried an error that I need to use a new keyword.  Is there a different check to see if the value is defined - not coming back from the DB as null?  Thanks.


11/14/2008 4:59:51 PM

Similar Artilces:

'System.Web.UI.Control' does not contain a definition for 'DataItem'
Hi, I am recieving this error: 'System.Web.UI.Control' does not contain a definition for 'DataItem' What iam trying to do is when a user clicks the hyperlink column in the datagrid then the ID of that user is send to another page and I need to run the Query or something and bind the Label on the page 2 with the name of the Person. So I am using the HTML view and writing something like this : '<%# DataBinder.Eval(Container.DataItem,"Name") %> ' here is my code behind file so how can i bind the Name of the person with the label: private void Page_Lo...

'System.Web.UI.Control' does not contain a definition for 'DataItem'
Hi, I am getting this error does anyone knows how to solve this :DHighOnCodingWanna get high! Show us the code that produces this error. Other than the exactness of the error, which explains what is exactly happening, we can't see why this is happening without seeing your code.David WierMCP/ - where to look first!Please Vote for ASPNet101 - 'Best Community Resource'!Control Grouper - easily control properties for multiple controls with one control!Calendar Express - The Best HTML Calendar Generator on the web!(Please 'Mark as Answer' when i...

Getting 'System.Web.UI.Control' does not contain a definition for 'DataItem' error on controls when placed inside an update panel which is in a template.
Using the June CTP, I;ve got an updatepanel inside the itemtemplate of a datalist which is now suppopsed to be supported (it works). I also have some asp controls (imagebutton, label) which get their values dynamically (DataBinder.Eval(Container.DataItem, "fieldname").ToString(). This would work fine before putting the updatepanel in the datalist itemtemplate, I now get the error in the title when building ('System.Web.UI.Control' does not contain a definition for 'DataItem') for each control... How can I dynamically assign the values to the controls in the datalist when also using upd...

Using DataBinding 'Eval' expression in HyperLink 'NavigateURL' property. Appends loads of ASCII 'space' codes onto the end!
Hi,  Using ASP.NET 2.0, I am working with an ItemTemplate inside a DetailsView. <ItemTemplate> <asp:Label ID="lblPollURL" runat="server" Text='<%# Eval("URLPath") %>'></asp:Label> <asp:HyperLink ID="lnkPollURL" runat="server" NavigateUrl='<%# Eval("URLPath") %>'>Go to</asp:HyperLink> </ItemTemplate> The label renders just fine, and produces the result 'jonathanshomepage'. However, the HyperLink NavigateURL results in 'http://localhost:3205/website/jonathanshomepage%20%20%20%20%20%20%20%20%20%20%20%20' Of course, I would expect...

Repeater: nested controls cause error: 'object' does not contain a definition for 'DataItem'
I'm trying to use a Repeater control to create a series of other controls. The problem seems to be that when I try to access "Container.DataItem" the Container is now my nested control instead of the Repeater control. How do I get the Container.DataItem to point back to my Repeater instead of the nested control?   Sample code: <asp:Repeater ID="CardRepeater" runat="server"> <ItemTemplate>  <ComponentArt:Snap runat="server" ID="SnapCard" IsCollapsed="false"CurrentDockingIndex="0" Curre...

Name: haznen Email: haznenatyahoodotcom Product: Gran Paradiso Alpha 8 Summary: '''''''''''''''''''' Comments: '''''''''''''''''''''''''''''''''''' Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9a1) Gecko/20061204 UGES/ GranParadiso/3.0a1 From URL: Note to readers: Hendrix gives...

''null' or not an object' error using validation controls in user control
Wonder if anybody has come across this before.I’m using Atlas together with some user controls I’ve created for user input that has validation controls on the forms.  If I remove the validation controls then all works fine… however once they are enabled I get a very annoying javascript error saying “ ‘null’ is null or not an object “ Where do I even start debugging this? If somebody knows what im doing wrong or how I could fix this please let me know.   Thanks ...

'System.Windows.Forms.DataGridView' does not contain a definition for 'databind' and no extension method 'databind' accepting a first argument of type 'System.Windows.Forms.DataGridView' could be foun
 Error    1    'System.Windows.Forms.DataGridView' does not contain a definition for 'databind' and no extension method 'databind' accepting a first argument of type 'System.Windows.Forms.DataGridView' could be found (are you missing a using directive or an assembly reference?) how to solve this Are you using a web applictaion or windows application. If web aplictaion then you should not use 'System.Windows.Forms.DataGridView'  Try using system.web.ui.control.datagridVikram www.vikramlakhotia.comPlease ma...

Name: mario Email: ramar17atfastwebnetdotit Product: Gran Paradiso Alpha 2 Summary: ''''' Comments: K: Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9a2) Gecko/20070206 GranParadiso/3.0a2 ...

'System.Web.UI.IDataItemContainer.DataItem' is a 'property' but is used like a 'method'
On the line "<%# Container.DataItem("BlogID") %>", I get the error listed in the title of this post. I'm not sure why, I use this same for on other pages to get data from a database and I don't get this error.         <asp:GridView ID="GridView1"         runat="server"         AutoGenerateColumns="False"         DataKeyNames="BlogID"        D...

'TestApp.GroupRepeater' does not contain a definition for 'DataItem'
I created a control that implements templates similar to the Repeater, but whenever I load the page with the control, I get the error: 'TestApp.GroupRepeater' does not contain a definition for 'DataItem' GroupRepeater has various(5) templates declared as: [TemplateContainer(typeof(GroupRepeaterItem))] public ITemplate HeaderTemplate { get {return _HeaderTemplate;} set {_HeaderTemplate = value;} } GroupRepeaterItem does implement a public get/set property for DataItem. If I add a DataItem property to GroupRepeater, the page compilation error goes away, but when I bind...

how to put two column in a commandargument? CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Docid") %>'
Hi guys,I need help once again. I just want to ask how can i put two columns in  my CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Docid") %>'I wanna put two columns inside the commandargument which i'll be using for the deleting. The columns will be use as a filter in deleting. Here's the code of my gridview:   <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"              ...

Invalid use of '.', '!', or '()'. in query expression 'CType(e.Item.Cells(1).Controls(0), TextBox).Text'.
Hi ppl, hope u can solve my prob, i getting this error:<Code> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here If Not Page.IsPostBack Then BindData() End If End Sub Sub BindData() '1. Create a connection Const strConnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=c:\inetpub\wwwroot\IT2691\speedy.mdb;" Dim objConn As New OleDbConnection(strConnStr) objConn.Open() '2. Create a command object for the query Const strSQL As String = "SELECT * FROM Products"...

'System.Web.UI.WebControls.DataListItem.DataItem' is a 'property' but is used like a 'method'
 HI I have a datalist with OnItemDataBoundI get the following error when I try to get the DataItem 'System.Web.UI.WebControls.DataListItem.DataItem' is a 'property' but is used like a 'method' , any idea what's wrong with the line ?  public void Item_Bound(object sender, DataListItemEventArgs e)    {        Label originalPriceLabel;        if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem)        {    &n...