I'm trying to check a string value inside a repeater, and if it has value then write a link, but can't seem to get it to work. If there is a value in myUrl then I want to display the link.
<%if( %> <%#Eval("myURL").ToString().Length > 0 %>
<a title="myTitle" target="_blank" href="<%# Eval("myURL") %>">my link</a>
<% } %>
Can anyone please help?
try this code !!!
<%#Eval("myURL").ToString().Length > 0 ?
"<a title='myTitle' target='_blank' href='<%# Eval("myURL") %>'>my link</a>":""%>
I personally hate using conditional logic like that in the page.
There are two options that I think are better. You could have a Hyperlink control in the repeater - and set the visibility depending on if the myURL param is there.
visibility='<% #Eval("myURL").ToString().Length > 0 %>'
OR what you can do is have a method on your code behind that you call back to with the "myURL" param.
E.g.
public string CreateURL(string myURL){
if(!string.IsNullOrEmpty(myURL)){
return "<a ... ";
}
return string.Empty;
}
And call in ASPX
<%# CreateURL(Eval("myURL").ToString()) %>
NB this is untested code but this is the ways I usually do this sort of thing.
I would use the String.Format and include the HTML as part of the string. Admittedly, it's not the neatest piece of code ever written, but in my opinion it's the best option:
For example the below will output an anchor tag if the property Url exists, otherwise it will output a span.
<%# string.Format(Eval("Url") != null ? "{1}" : "<span>{1}</span>", Eval("Url"), Eval("Text")) %>">
Try adding a runat="server" and then add a script block for the (new) server-side visible property:
<a title="myTitle" target="_blank" href="<%# Eval("myURL") %>" runat="server" visible='<%#Eval("myURL").ToString().Length > 0 %>'>my link</a>
this will help
How do I run an if statement in aspx?
http://forums.asp.net/t/1254412.aspx/1
http://forums.asp.net/t/1161705.aspx
You can also call your public function inside code behind file:
<%# MyFunction(Eval("myURL").ToString().Length) %>
Related
I am attempting to pass a parameter from one file to another via the URL after a button is clicked. These are written with Express.js (index.ejs to items.ejs).
As it stands currently I am setting the URL parameter in a defined Javascript function:
function loadItems(page, subcategory) {
window.history.pushState(null, null, "?subcat=" + subcategory) //param set
$('#mainContent').load(page);
}
where subcategory is the changing variable.
From there I am trying to read this parameter during an ASP.NET function written in embedded code blocks.
<% if(items[i].subcategory === Request.QueryString["subcat"].Value) { %> //get param
<% if (items[i].status === "Supported") { %>
<tr class="success">
<td>Edit</td>
<td id="item name"><%= items[i].name%></td>
<td id="subcat name"><%= items[i].subcategory%></td>
<td id="item status"><%= items[i].status%></td>
<td id="item desc"><%= items[i].description%></td>
</tr>
However I am met with an error which states Request is not defined and a callback to the above if statement. It is my understanding that on the ASP.NET side of things, Request.QueryString is a part of System.Web.HttpContext.Current.
How would I go about including this into my code blocks so that I am able to pull the parameter from the URL? Or, if this is not the way to be looking at this problem, how should I go about it?
My advice would be to use code behind. Embedded code blocks are an old-school throwback from the asp days. But if you must, then you should be able to do something like this:
<%# Page Language="VB" %>
<script run=server>
Protected Function GetSubcat() As String
Return Request.QueryString["subcat"].Value
End Function
</script>
<form id="form1" runat="server">
Subcat value is <% =GetSubcat()%>.
</form>
Recently I'm working on a website which is contain of galleries. I store the information
of each album in a database and the I fetch them to create links containing field AlbumId
with Eval("AlbumId"). The code is exactly like below:
<a href="/ShowAlbum.aspx?AlbumId=" + <%# Eval("AlbumId") %>><%# Eval("Title") %></a>
which finally results in:
~/ShowAlbum.aspx?AlbumId=
The AlbumId is empty.
Does anyone knows what is the problem?
In href when you complete the double quote (") it means the value of the href has been ended here.
And after that if you append dynamic value using eval it will give you an error Invalid Token
You should put Eval between double quotes as mentioned below :
<%# Eval("Episode") %>
Just try in this way
<a href='<%# "ShowAlbum.aspx?AlbumId="+Eval("Albumid")%>'><% Eval("Title") %></a>
Let me know the output.
Eval(), and Bind() not suport for double quotes (" ") it support single Quotes for (' ')
so try this
<a href="/ShowAlbum.aspx?AlbumId=" + <%# Eval('AlbumId') %>><%# Eval('Title') %></a>
I have t following ASPX source :
<form name="AddComment" action="ViewArticle.aspx?ArticleID=<%=ArticleID %>" method="post" runat="server">
The problem is that when I click submit I got this url
http://localhost:61175/WebSite1/ViewArticle.aspx?ArticleID=%3C%=ArticleID%20%%3E
But I want get this URL :
http://localhost:61175/WebSite1/ViewArticle.aspx?ArticleID=1
If I delete the "runat="server"" command it works fine, but can this form work with the
variable and the runat server at the same time?
Maybe you can try setting the action just before the form markup like this (or in codebehind, at your choice)
<%
this.Form.Action = "ViewArticle.aspx?ArticleID=" + ArticleID.ToString();
%>
<form name="AddComment" method="post" runat="server">
In server side controls you can't do such things as <%= ActionID %> you can only bind <%# ActionID %>.
The upper solution won't work, becouse the form is not that form. You have to give a control ID + name than setup the post url. If it's not an option you can dinamicly search for form item in the control tree, so dynamic ID-s can be handled.
I have a ListView with many advanced controls and html tags. ListView is bound to collection of profiles when first profile in collection is current profile. current profile has few differences from other profiles ie. flash embed, js and some other stuff. I can access inside of my ListView Container.DataIndex property which gives me 0 as first item in index but i'm unable to use inline If statements like so
<% If Container.DataIndex = 0 Then %>
do stuff
<% EndIf %>
this is because i must place pound to access databound item but neither this
<%# If Container.DataIndex = 0 Then %>
do stuff
<% EndIf %>
How can i make inline If ?
Try this:
<% if (DataBinder.Eval(Container, "DataItemIndex")) { %>
do stuff
<% } else { %>
do other stuff
<%} %>
Here is a small summary of the inline aspx tags:
http://naspinski.net/post/inline-aspnet-tags-sorting-them-all-out-%283c25242c-3c253d2c-3c252c-3c252c-etc%29.aspx
But i would recommend to use ListView.ItemDataBound. It is less error-prone and more readable in codebehind.
Did you mean IIF? IIF - Returns one of two objects, depending on the evaluation of an expression.
Is there an easy way to comment out a loop which renders some html and has inline html without deleting anything? I am copying and pasting some code from another project to rebuild a new public front end from a working internal backend.
Below is an example of a sitation in which it would be nice...in asp.net MVC 2
<%
List<VehicleBodyTypeListItem> lstBodyTypes = (List<VehicleBodyTypeListItem>)ViewData["ddBodyType"];
foreach (VehicleBodyTypeListItem bodyType in lstBodyTypes)
{
%>
<a href="<%= Url.Action( "Search", new { BodyTypeID=bodyType.BodyTypeID, BodyType= Url.Encode( Html.WebLinkify( bodyType.BodyType))}) + (string)ViewData["httpCriteria"] %>">
<%= Html.Encode( String.Format( "{0} ({1})", bodyType.BodyType, bodyType.Count.ToString())) %> </a>
<br />
<%
}
%>
I have not completed the method that populates this list yet, and have about 5 more like it further down the page.
The keyboard shortcut is, if you select the section you want commented out is: CTRL + K + C will comment out code. CTRL + K + U will uncomment the code.
Comment a block of code by enclosing it in #* and *#
Do you mean adding comments to the code. If so you just need to add //. Like here:
<%
List<VehicleBodyTypeListItem> lstBodyTypes = (List<VehicleBodyTypeListItem>)ViewData["ddBodyType"];
foreach (VehicleBodyTypeListItem bodyType in lstBodyTypes) // Here there's a comment
{
%>
<a href="<%= Url.Action( "Search", new { BodyTypeID=bodyType.BodyTypeID, BodyType= Url.Encode( Html.WebLinkify( bodyType.BodyType))}) + (string)ViewData["httpCriteria"] %>">
<%= Html.Encode( String.Format( "{0} ({1})", bodyType.BodyType, bodyType.Count.ToString())) %> </a>
<br />
<%
}
%>