I have a repeater with a column that I needs to have the following expression, but not sure of the syntax.
If value="DispForm.aspx" then
Show "No Document"
Else
Show the real value.
I tried to use all expresion in one line, but not sure what am I missing here
<a href=" <%# DataBinder.Eval(Container.DataItem, "Path") %>">
<%#
if (DataBinder.Eval(Container.DataItem, "FileName") == "DispForm.aspx")
{
"No document";
}
else
{
DataBinder.Eval(Container.DataItem, "FileName");}%>
</a>
Error :
http://screencast.com/t/ZERZjzZxST
It would be much simpler to use the ItemDataBound event, add an id and runat="server" to the anchor element, then you can use e.Item.FindControl("anchorname") to get the htmlanchor element. You can then set it's text, href, visibility etc. in codebehind far easier than trying to get some of the more difficult databinding logic to work effectively.
I solved it my self like this: item databound events will be fired on the server side for each row, and this report returns 2000 rows. Not a good thing.
<a href="<%# DataBinder.Eval(Container.DataItem, "fileNameUrl") %>">
<%# DataBinder.Eval(Container.DataItem, "FileName").ToString() == "DispForm.aspx" ? "No document" : DataBinder.Eval(Container.DataItem, "FileName").ToString() %>
</a>
Related
I've got an asp:Image. I want this control to display, only if <%#Eval("Image")%> is not null.
I do not know how to write this conditional statement. What I'm trying to say is something like this (if the value of 'image' taken from the data structure is not null, then display the image. Otherwise, do not):
<%#Eval("Image")%> != 0 ? <asp:Image ID="image" runat="server"/>
I'm aware that this is not the syntax - consider it as pseudocode, as I have never had to write a conditional statement in the markup.
Any ideas? :)
You can bind the Visible property of your control to the expression and call DataBind() while the page is loading:
<asp:Image runat="server" id="image" Visible='<%#Eval("Image") != null %>' />
If you are not using server controls and want to show/hide simple markup, you can simply enclose it in an if statement:
<% if ( condition ) { %>
<img src='<%= linkToImageSource %>' />
<% } %>
I'm trying to create a simple hyperlink list with a RepeaterItem (I'm not particular to a RepeaterItem, so if there are better ways...).
I'm pretty much using the code from the MSDN documentation linked above, but I have a simple problem, I'm using the <% %> control wrong:
Parser Error Message: The server tag is not well formed.
<li><asp:HyperLink id="navListItem" runat="server"
NavigateUrl="<%# DataBinder.Eval(Container.DataItem, "Url") %>">
<%# DataBinder.Eval(Container.DataItem, "Text") %></asp:HyperLink></li>
Apparently I cannot use <% within another asp.net tag.
What would be the "correct" way to create a list such as:
<ul>
<li>Link Text 1</li>
<li>Link Text 2</li>
<li>Link Text 3</li>
</ul>
The Url & Link text I get from a resource file.
You could either remove the double quotes for the NavigateUrl property or use a single quotes there instead:
NavigateUrl=<%# DataBinder.Eval(Container.DataItem, "Url") %>
or
NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "Url") %>'
and this should work.
I've got a:
Data repeater
Control in the repeater
And I'd like to put some values into the controls. At the moment I have a control in it as:
<asp:Repeater runat="server" ID="ArchiveEntryRepeater">
snip
<asp:HyperLink ID="HyperLink1" ToolTip="Comment on Some Entry Title Here" runat="server" NavigateUrl="~/Blog/7/How-to-know-when-this/Comments">
<strong><%# DataBinder.Eval(Container.DataItem, "Comments")%></strong> Comments
</asp:HyperLink>
snip
</asp:Repeater>
Which works fine, but I want to put
<%# DataBinder.Eval(Container.DataItem, "Title")%>
Into the NavigateURL property of the Hyperlink. Just putting it in doesn't seem to work!
Try enclosing your NavigateURL in apostrophes instead of double quotes to not confuse the ASP.NET parser:
<asp:HyperLink runat="server" NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "Title")%>' [...]>[...]</asp:HyperLink>
This might be your issue.
You can always bind on the back end using the event itemdatabound. It has some advantages especially where the data is not a neat view or needs some type of major manipulation.
Another option would be to loose the control and replace it with an html anchor control like this:
<asp:Repeater>
<ItemTemplate>
<a id="HyperLink1" title="Comment on Some Entry Title Here" href='<%# DataBinder.Eval(Container.DataItem, "Title")%>'
style="font-weight: bold">'<%# DataBinder.Eval(Container.DataItem, "Comments")%>'</a>
</ItemTemplate>
</asp:Repeater>
Once the server renders a hyperlink to the client there is generally no reason to have it "runat='server'" . The purpose is to navigate to another page so the overhead of using an asp:HyperLink vs. an nchor is wasted.
Hi would like to add img source path dynamically as shown in the snippet below, but giving error. I know img_src_path adding syntax is correct. Unfortunately I don't know the solution, need help.
Environment:
ASP.net, c#
<% string a[0]="image/hello.jpg;"
{
String img_src_path= a[0].ToString();%>
<li><a href='"<%#img_src_path%>"'><img src='"<%#img_src_path%>"' alt="" title=""/></a></li>
<%}%>
/Shashi
The problem is that your image tag does not have a runat="server" attribute (you will also need to add an ID in order to do this).
You also need to change your server tags like this:
FROM:
<%# ... %>
TO:
<%= ... %>
Also, the proper way to do this in ASP.Net would be to use the Image server control.
<asp:Image id="Image1" runat="server"></asp:Image>
Then, you would set the NavigateUrl property.
replace your code with this:
"=" instead of "#"
the solution
<li><a href='"<%=img_src_path%>"'><img src='"<%=img_src_path%>"' alt="" title=""/></a></li>
and if you want to edit image src form code behind
solution 2:
<li><a href='"<%=img_src_path%>"'><img src='"<%=img_src_path%>"' id="myImage" runat="server" alt="" title=""/></a></li>
edit image source form codebehind:
myImage.src = "imagePage";
The basic error we are trying to workout is Server tags cannot contain <% ... %> constructs.
how would you go about adding logic to a datagrid item template? In my datagrid, i want to add a logic to it. that is, if the result for the data equals to "Yes", an "asp:label" control will be displayed; otherwise a "asp:imagebutton" control will be shown
<ItemTemplate1>
<% if DataBinder.Eval(Container.DataItem, "boflag").equals("Yes") then%>
<asp:Label id="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"boflag")%>'></asp:Label>
<% Else %>
<asp:imagebutton id="imgBtnUpdate" runat="server" NAME="Imagebutton3"
ImageUrl="no.gif"></asp:imagebutton>
<% end if %>
</ItemTemplate>
However, "<% if DataBinder.Eval(Container.DataItem,
"boflag").equals("Yes") then%> " this is not valid.
So, how can i get the data to compare the value.
Thank you
You should implement the items Data Bound Event in the code behind. Then show/hide/populate the controls there.
One other option you can do is to use a ternary operator to evaluate the boflag field and output accordingly. For example:
<%# DataBinder.Eval(Container.DataItem, "boflag").equals("Yes") ? DataBinder.Eval(Container.DataItem,"boflag") : "<input type=\"image\" src=\"\" />" %>
I'm not sure that you could add server controls through this method, but you could certainly add conditional HTML.