2

I want to use a value that is pulled from the SQL within the if statement. Ideally i want to do the equivalent of

<% If DataBinder.Eval(Container, "DataItem.BookID") == 1 Then%>

Is there a way to do this with the correct syntax?

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Sophie
  • 795
  • 3
  • 9
  • 13
  • Explain what Eval(Container, "DataItem.BookID") is? – Ebad Masood Jun 20 '12 at 10:55
  • DataBinder.Eval(Container, "DataItem.BookID") throws an error as Container is not declared. however the value of BookID is the value i need to compare to 1. – Sophie Jun 20 '12 at 10:57

3 Answers3

2

This is how you put conditions in aspx file. Just a rough sample base on what I understand:

<%# System.Convert.ToInt32((DataBinder.Eval(Container.DataItem, "BookID")!="") ? DataBinder.Eval(Container.DataItem, "BookID"):0) %>

Make sure you have int in BookID not any other type.

Explaining Further:

In case you want to have an if else condition:

<%# If DataBinder.Eval(Container.DataItem, "DATAFIELD") <> "" Then

   Response.Write("something")

End If %> // This is invalid 

The above statement can be properly written in aspx file as this:

<%# DataBinder.Eval(Container.DataItem, "DataField").Equals("")?"":"Something"%>
Ebad Masood
  • 2,389
  • 28
  • 46
0

I'm not sure if this can be done or not the way you are requesting it.

As you may or may not know, the typical way to do this is to have a control in your markup, like so

<asp:listView ID="SophiesListView" ...
..
<ItemTemplate>
<asp:HyperLink ID="hlGlossary" title="click here for more information" target="_blank" runat="server" />
</ItemTemplate>
</asp:listView />

Then, in the codebehind, find your listview / repeater / datagrid or what have you and choose ItemDataBound. Inside this event, do something like this:

If e.Item.DataItem("vehicleType") IsNot DBNull.Value AndAlso e.Item.DataItem("vehicleType") = "JETSKI" Then
    DirectCast(e.Item.FindControl("hlGlossary"), HyperLink).NavigateUrl = "Glossary.aspx#JETSKI"
    DirectCast(e.Item.FindControl("hlGlossary"), HyperLink).Text = "?"
End If
Paul G
  • 23
  • 4
-1

To keep your page logic as simple as possible your best bet is to data bind to the Visible property of controls. For example, if you want to only show some html if the BookID == 1 then create a new property on your data source like this

public bool Show
{
     get
     {
         return BookID == 1;
     }
}

and in your page you'd have

<asp:Placeholder runat="server" Visible='<%# Eval("Show") %>'>
    ...html goes here...
</asp:Placeholder>
graham mendick
  • 1,839
  • 1
  • 17
  • 15