using values within if conditions in mark up - asp.net

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?

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"%>

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

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>

Related

How to reference a .Net web control's selected value in NavigateURL

I have an ASP.Net page with a GridView. In one of the GridView cells there's a HyperLink control and its NavigateURL property is set like so:
NavigateUrl='<%# "~/telecom/SmartPhoneInventory.aspx?IMEI=" + Eval("IMEI") %>'
There's a RadioButtonList (rblDeviceType) on this page (not in the GridView) with four values. I want to add another querystring to the HyperLink's NavigateURL so that:
NavigateUrl='<%# "~/telecom/SmartPhoneInventory.aspx?IMEI=" + Eval("IMEI") + "&devicetype=" + rblDeviceType.SelectedValue %>'
This is of course not correct syntax. Is there a way to do this?
Try this:
In your html
<a href='<%= string.Format("~/telecom/SmartPhoneInventory.aspx?IMEI={0}&devicetype=", this.someValue, rblDeviceType.SelectedValue) %>'>
Hello World
</a>
or in your html:
<asp:HyperLink runat="server"
NavigateUrl='' ID="demoLink">
Hello World
</asp:HyperLink>
and then in your codebehind:
demoLink.NavigateUrl= string.Format("~/telecom/SmartPhoneInventory.aspx?IMEI={0}&devicetype=",this.someValue,rblDeviceType.SelectedValue)
Regarding
'someValue'
Which you present as Eval("IMEI") in your sample code since your code is not part of the Grid you will need to get this from either a control directly, session, viewstate or server side variable. Your code sample does not allow me to understand where is the original source of this value.
Try this in your code behind:
public partial class _Default : Page
{
public string someValue = "Hello World";
Using string.Format and <%= instead of <%#

Embedded Code in attribute of asp.net controls

I want bind the visibility of a panel to the selectedValue Or selectedIndex of a gridview, so i try this:
<asp:Panel ID="pnlPic" Visible='<% gvAllQuarries.SelectedIndex == -1 ? false:true %>' runat="server" >
or
<asp:Panel ID="pnlPic" Visible='<%= gvAllQuarries.SelectedIndex == -1 ? false:true %>' runat="server" >
but syntax is wrong and unknown for intelisense. how can i bind like this? is it possible?
Try putting an event handler on the SelectedIndexChanged event of the GridView; you can then show/hide the panel accordingly: (VB.NET)
Protected Sub gvAllQuarries_SelectedIndexChanged(sender As Object, e As EventArgs) Handles gvAllQuarries.SelectedIndexChanged
pnlPic.Visible = Not (gvAllQuarries.SelectedIndex = -1)
End Sub
Mess around with the logic to get it playing how you want, adapt to C# if need be.
As an alternative you can wrap the panel in a conditional block if you want it all inline:
VB.NET:
<% If gvAllQuarries.SelectedIndex <> 1 Then%>
<asp:Panel ID="Panel1" runat="server">
//put whatever inside the panel
</asp:Panel>
<% End If %>
C#:
<% if (gvAllQuarries.SelectedIndex != 1) { %>
<asp:Panel ID="Panel1" runat="server">
//put whatever inside the panel
</asp:Panel>
<% } %>
It's a bit messy, but the only way I can see of you getting it all inline without code-behind. The option you are trying Visible='<% gvAllQuarries.SelectedIndex == -1 ? false:true %>' will only work if you use data-binding syntax ('<%# %>'), and then you will need to explicitly call DataBind() on it from code-behind anyway.
Unfortunately using '<%= $>' will just output static text and not be evaluated into a boolean to apply to the visible property.
Adapted from this similar question.

Add text after expression <%$ ... %>

In my ASPX page I've this:
<h3>
<asp:HyperLink runat="server"
NavigateUrl="<%$ Resources:Path, Article%>"
Text='<%# Eval("title") %>' />
</h3>
For the NavigateUrl attribute I want to specify an ID like
NavigateUrl="<%$ Resources:Path, Article%>?id=4"
But when I do that the expression is not precessed by the ASP parser.
How can I do that?
Don't do this in markup. You have a server control here -- give it an ID (say, ID="NavigationLink", and then do something like this in your .cs file:
void Page_Load(object sender, EventArgs e)
{
// I'm guessing that "4" was just an example here, so fill that piece in with a function you can call to create the proper ID.
NavigationLink.NavigateUrl = Properties.Resources.Path + Properties.Resources.Article + "?id=4"
}
Edit: I'm assuming that when you say <%$ Resources:Path, Article%> that you're trying to reference the Path and Article entries in your resources file, but upon further reflection, it's hard to tell exactly what you're doing. Can you be more specific here?
You can define a protected function in the code behind class and call it from the markup. Like this:
<h3>
<asp:HyperLink runat="server"
NavigateUrl="<%# GetNavigateUrl(Eval("ID")) %>" <%-- Passing an ID as a parameter for example --%>
Text='<%# Eval("title") %>' />
</h3>
Code behind:
// Again, idObj is just an example. Any info from the data item can be passed here
protected string GetNavigateUrl(object idObj)
{
int id = (int)idObj;
string urlFromResources = // retrieving the url from resources
return urlFromResources + '?ID=' + id;
}
You could retrieve the resource values programmatically, in your code-behind, and set the NavigateUrl property from there.
Try String.Format...
<% =String.Format({0}?id={1}, Request.ApplicationPath, 4) %>
It looks like you're mixing some other language into this (javascript?).
Also, don't forget to give your server-side controls an ID.

Listview/DetailsView: Hide a null field

I imagine this is quite a common problem, but as yet I haven't found an elegant solution.
I have a number of instances where I have either a ListView or a DetailsView control that is bound to a SQL Server SProc. The problem I have is that there are numerous instances where, when a column is Null, I want to display something different in the UI. A typical example would be where I have a URL column that is rendered as a LinkButton (in the ListViews) or as a HyperLinkField (in the DetailsViews) - when a Null URL is returned, I'm rendering links with no src attribute. Ideally, I's want to display nothing in this field in such a scenario.
In each of these cases, when a null value is returned, how can I skip/alter the rendering of that item?
Thanks in advance.
Update: I haven't had chance to actually try these out, but all helpful suggestions. I think I like Ricks answer the best, but thanks again to the others...
Markup:
<asp:HyperLink id="whatever" runat="server"
NavigateURL='<%# Eval("url") %>' Visible='<%# IsVisible(Eval("url")) %>' />
Code behind:
protected bool IsVisible(object obj)
{
bool result = false;
string url = (string)obj;
if(!string.IsNullOrEmpty(url))
{
result = true;
}
return result;
}
Within a Template bind also to Visibility
<asp:HyperLink ... NavigateURL=<%# Eval("url") %> Visible=<%# Eval("url") != null %> />
Warning: not Tested, could also be
<asp:HyperLink ... NavigateURL=<%# Eval("url") %> Visible=<%# Eval("url") != DBNull.Value %> />
I suppose you could either create a method in your code behind that takes the value as a parameter and returns a link if it's not null. Or you could tap into the data bound event of the Listview, examine the value and hide the control if it's null.
Neither a very elegant solutions, but I guess that's up to you to decide. :)

Is it possible to use Data-Binding Expressions directly in markup to show/hide content?

I would like to do something like:
<%# if((bool)Eval("IsDisabled")){ %><span>Disabled</span><% }
else { %><span>Active</span><% } %>
but I don't think its possible.
There is a way to create method in codebehind which returns appropriate string and call it, but thats not an option.
You can use placeholders to hold the two versions of your markup and then use the Visible property to show the relevant one. Something like this... Note the use of ! before the call to IsDisabled in the second Visible property.
<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible='<%# IsDisabled((bool) Eval("IsDisabled")) %>'>
<span>Disabled</span>
</asp:PlaceHolder>
<asp:PlaceHolder ID="PlaceHolder2" runat="server" Visible='<%# !IsDisabled((bool) Eval("IsDisabled")) %>'>
<span>Active</span>
</asp:PlaceHolder>
The code behind IsDisabled method looks like this...
public bool IsDisabled (bool isDisabled)
{
return isDisabled;
}
Its not possible to use # eval in if statement,
You have some options to solve that:
You can put the condition of the if in a previous line then check on this variable in the if
example:
in code behind:
protected bool isDisabled;
in aspx:
<%# isDisabled=(bool)Eval("IsDisabled") %>
<% if(isDisabled) %>
Other way is to call a code behind method which return bool and check on it in the if.

Resources