Broken <a> tag in asp.net - asp.net

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>

Related

ASP content display issue

I moved my site to another hosting, but the language menu <a> tags between the content of the database does not print between the label. But inside the href parameter it draws the same query.
When I examined the output of the code:
Output code picture
Below is my code in that line:
<ul class="headerLanguage"><li><%=Session("lang")%><img alt="" title="" src="/images/icon/08.png" /><ul><%
Set a = SQL.Execute("SELECT kisa FROM diller WHERE kisa<>'"& Session("lang") &"'")
Do while not a.Eof %><li><%=a("kisa")%></li><% a.MoveNext:Loop
a.Close
Set a = Nothing %></ul></li></ul>
When I delete the query inside the href parameter, the other query runs and the content is appearing.
My application is Classic ASP, but I did not have a problem with my old site, why was this so? How can I solve?
I'm grateful for your help.
I've encountered this problem before. I'm not sure exactly why it happens (hopefully someone else can explain), but sometimes if you reference data directly from a recordset multiple times it can return an empty value. The solution that worked for me was to assign the data to a variable first and reference the variable instead. Try this:
<ul class="headerLanguage">
<li>
<a href="/?lang=<%=Session("lang")%>"><%=Session("lang")%>
<img alt="" title="" src="/images/icon/08.png" />
</a>
<ul><%
Dim a, kisa
Set a = SQL.Execute("SELECT kisa FROM diller WHERE kisa<>'"& Session("lang") &"'")
Do while not a.Eof
kisa = a("kisa") %>
<li><%=kisa%></li><%
a.MoveNext:Loop
a.Close
Set a = Nothing
%></ul>
</li>
</ul>

When I try to use to redirect to another screen through master page this error happening

When I click on link from master page I got this issue .How can I fix this issue
</a><a href="vacations.aspx">
<div class='<%= vacDetailMenu %>' style="cursor: pointer;">
Vacation History
</div>
This is the code which I used.
This is the issue:
"Index and length must refer to a location within the string.
Parameter name: length"
Can you please share your more code .. .
this is a error which comes when you put string length more than Real length,
string muString="Hello";
string substring = myString.Substring(0, 6);
and
</a><a href="vacations.aspx">
is not Proper Anchor Link , You should use it like
SomeText

Request value from eval query string passed in anchor tag

I passed three values as query string in an anchor tags as follows.
<a href='Booking.aspx?date=<%# Eval(" + calendar.Text + ")%>&hour=<%# Eval(" + i + ")%>&Id=<%# Eval(" + c.Id + ")%>'>Booking</a></td>";
When i go to Booking.aspx the link look likes this.
http://localhost:50115/AltaDeReserva.aspx?date=%3C%# Eval(08/07/2014)%>&hour=<%# Eval(17)%>&Id=<%# Eval(8)%>
But i donĀ“t know how to request the values, i tried this but it does not work.
String date= Request.QueryString["date"].ToString();
Thank you in advance..
Please try with the below code snippet.
<a href='<%# string.Format("Booking.aspx?date={0}", Eval("date")) %>' />
//OR
<a href='<%# "Booking.aspx?date=" + Eval("date") %>' />
If above code is not worked for you then elaborate your scenario.

Response.write doesn't display the image

Writing html using Response.Write in asp.net it doesn't display the image. I check my image path code and simple put it in the page and it works fine. Why it doesn't display the image when writing using response.write code.
Following is my image path code
<img alt="" src="<%= VirtualPathUtility.ToAbsolute("~/Content/images/txt2.png")%>" border="0"/>
This is Response.write code
<% Response.Write(valueHelp); %>
ValueHelp is a string which contain the image code which i have mention above.
Any idea why it is not working?
Thanks in advance
<%= %> is to be used within the mark-up (i.e. the HTML part of your code) and not in the code-behind.
My guess (without seeing the code) is that you are actually sending <%= VirtualPathUtility.ToAbsolute("~/Content/images/txt2.png")%> to the browser as part of a static string.
So instead of it being picked up by the server and rendered into the correct path, it is simply being sent as part of the HTML to the browser (the browser not knowing what on earth it means, therefore it will not show the image you expect).
Try something like this when you are creating the valueHelp string
valueHelp = "<img alt='' src='" + VirtualPathUtility.ToAbsolute("~/Content/images/txt2.png") + "' border='0'/>";
Try this
<% Response.Write("<img alt='' src='" +
VirtualPathUtility.ToAbsolute("~/Content/images/txt2.png") +
"' border='0'/>" ); %>

#Eval if statement in repeater

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

Resources