I have an .aspx in which there is one anchor tag whose href property is set by the server side code i.e. DataTable.
My site url is : [xxx/Pages/Home.aspx] and suppose the href from the DataTable is bound http://www.google.com then the link redirects to [xxx/Pages/http//www.google.com] instead of http://www.google.com . Somehow it prefixes relative url of page.
My ascx file is :
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td>
<asp:ListView ID="Feed" runat="server">
<ItemTemplate>
<div class="Main">
<div class="Ttile">
<a href="<%# Eval("Link") %>" target="_blank" title="<%# Eval("Title") %>" ><%# Eval("Title") %></a>
</div>
</td>
</tr>
</table>
I want to redirect the user to http://www.google.com when user clicks on the link but the anchor tag redirects to http://xxx/Pages/http//www.google.com
If I put <%# Eval("Link") %> outside the anchor tag then it displays the proper url like : http://www.google.com. It means the data in "Link" column is perfect
How should I tackle this issue?
try this one
<%# RemoveServerUri(Convert.ToString(DataBinder.Eval(Container.DataItem, "Link")))
C#
public string RemoveServerUri(string link)
{
link = link.Replace("xxx/Pages/", "");
return link;
}
Related
I have bound a DataSet to a ListView. In the ListView ItemTemplate, if a row value is empty, I do not want it, or the <td> element it is enclosed in to display.
In my code, the first row value will display. However, when I try to use the If statement on the second <td> element, that will not display.
<asp:ListView ID="ListView1" runat="server" GroupPlaceholderID="groupPlaceHolder1" ItemPlaceholderID="itemPlaceHolder1">
<LayoutTemplate>
<table>
<asp:PlaceHolder runat="server" ID="groupPlaceHolder1"> </asp:PlaceHolder>
</table>
</LayoutTemplate>
<GroupTemplate>
<tr>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder1"> </asp:PlaceHolder>
</tr>
</GroupTemplate>
<ItemTemplate>
<td>
<%# Eval("textItem1") %>
</td>
<% if (!String.IsNullOrEmpty(textItem2){ %>
<td>
<%# Eval("textItem2") %>
</td>
<%} %>
</ItemTemplate>
</asp:ListView>
That If statement works in an aspx page if its NOT being used in a ListView, Repeater, or GridView (or does it?). I need to be able to check if that string row value is empty and not display it or the <td> element it is enclosed in. I am not against a solution that uses code-behind. Is there another way to do this then my attempt?
I used a pretty simple method solution..
In the above code, I swapped out this...
<% if (!String.IsNullOrEmpty(textItem2){ %>
<td>
<%# Eval("textItem2") %>
</td>
<%} %>
For this...
<%# writeText(Eval("textItem2").ToString())%>
And placed this method in the code behind...
public string writeText(string kiss)
{
if (!String.IsNullOrEmpty(kiss))
return "<td> " + kiss + "</td>";
else
return null;
}
We currently have an application which we have a repeater
<asp:Repeater ID="rptOfficials" runat="server">
<ItemTemplate>
<tr>
<td>
<p class="officials">
<%#Eval("OffPosition") %> <%#Eval("FullName") %>
</p>
<p class="officials">
<%#Eval("Phone") %>
</p>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
The Phone field is not always supplied, when not there a blank paragraph tag is generated, we're thinking of changing
<p class="officials">
<%#Eval("Phone") %>
</p>
to either:
<p class="officials">
<asp:label id="lblPhone" runat="server"/>
</p>
or:
<p class="officials">
<%#GetPhone("OffId") %>
</p>
and then have either the label return the phone number with the paragraph tags wrapped around it, or have the function return it. Is there a better way to get around this?
It depends how your Binding model is composed but I suggest adding the logic in your property (Phone) on your page your (view)model (yes you can and should use ViewModels in web forms as well).
That way you keep logic outside the view.
public string Phone {
get {
return this.phone ?? string.Empty;
}
}
That way your logic stays nicely inside your view model and your view just takes care of binding it.
If you don't want to generate the -tag, you can add a boolean property to your model.
public bool DisplayPhone{
get {
return this.Phone == string.Empty;
}
}
You then need to bind it to the visible property as Halcyon mentioned. (note you need a runat="server" for this)
<p runat="server" class="officials" visible='<% #Eval("DisplayPhone") %>'>
<% #Eval("Phone") %>
</p>
I would not recommend using "display: none" since it will keep the space of the p tag. Instead, make the p tag a server tag and set its visibility to false.
<p runat="server" class="officials" visible='<% !string.IsNullOrEmpty(#Eval("Phone")) %>'>
<% #Eval("Phone") %>
</p>
Instead of using
<p class="officials">
<%#GetPhone("OffId") %>
</p>
I prefer generate <p class="officials">foo</p> inside GetPhone(), then you can return nothing when Phone is empty.
You can add small logic like the following -
<%# string.IsNullOrWhiteSpace(Eval("Phone").ToString()) ?
"" : ("<p class=\"officials\">" + Eval("Phone") + "</p>")%>
However, if you want to add more logic, I would like to suggest to use Repeater.ItemDataBound Event
try with this
<p class="officials" <%#string.IsNullOrEmpty(Eval("Phone").ToString())? "style='display:none;':''" %> >
<%#Eval("Phone") %>
</p>
I'm trying to upload a default thumbnail image if user hasn't uploaded any thumbnail for their news article. Can you please help?
I tried at first, but it didn't work:
<%#Eval("Thumbnail")!=null ? Eval("Thumbnail"):"~/Images/test.jpg"%>" alt="<%#Eval("Title") %>"
I'm have the following code in a repeater:
<asp:Repeater ID="rptRotator" runat="server">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li>
<div class="widgetContent">
<img class="thumbNail" src="<%#Eval("Thumbnail") %>" alt="<%#Eval("Title") %>" />
<h4 style="width: 155px;">
<a href="/news/<%#Eval("PublicationDate","{0:yyyy/MM/dd}")%>/<%#Eval("UrlName") %>">
<%#Eval("Title") %></a></h4>
<div class="clear">
</div>
<span class="newsDate">
<%# Eval("PublicationDate", "{0:dd MMMM yyyy}")%></span>
<div class="widgetTextSummary">
<p>
<%#Eval("Summary").ToString().Substring(0,60)%>...</p>
</div>
</div>
</li>
</ItemTemplate>
<FooterTemplate>
</ul></FooterTemplate>
</asp:Repeater>
A simple way to do this is to create a public method that you can use to determine if the data item is null or not and set it accordingly. This way you wouldn't have to put all your code inline. You would then have something like this in vb .net in your code behind:
Public Function ProcessDataItem(myDataItemValue As Object) As String
If myDataItemValue Is Nothing Then Return "~/Images/test.jpg"
Return myDataItemValue.ToString()
End Function
And then call it in the repeater:
<%# ProcessDataItem(Eval("Title")) %>" alt="<%#Eval("Title") %>"
Also, I recommend using Container.DataItem instead of Eval as it creates less overhead. So in the end it would be something like this:
<%# ProcessDataItem(Container.DataItem("Title")) %>" alt="<%#Container.DataItem("Title") %>"
How about give your property Thumbail a default value?
Then when the Thumbnail hasn't been set to a non-null value the default image will be displayed, otherwise the users thumbnail will be shown.
One of the following should work:
<%# Eval("Thumbnail") ?? "/images/default.png" %>
<%# Eval("Thumbnail") == DBNull.Value ? "/images/default.png" : Eval("Thumbnail") %>
<asp:Image ID="imgProd" runat="server" src="<%#Eval('Thumbnail')"
onerror="this.onload = null; this.src='ImageurlORAnyImageHere.jpg';"/>
I want to have 4 links in the sidebar of master page which every other form (content page) in my web application inherits.
<table>
<tr>
<td width= "150px">
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
<asp:Menu runat="server" ID="MainMenu1" CssClass="MasterContent" StaticSubMenuIndent="30px">
<Items>
<asp:MenuItem Text="My Software" NavigateUrl="~/MySoftware.aspx"></asp:MenuItem>
<asp:MenuItem Text="Check Out" NavigateUrl="~/CheckOut.aspx"></asp:MenuItem>
<asp:MenuItem Text="View Shopping Cart" NavigateUrl="~/ShoppingCart.aspx"></asp:MenuItem>
<asp:MenuItem Text="Continue Shopping" NavigateUrl="~/Start.aspx"></asp:MenuItem>
</Items>
</asp:Menu>
</asp:ContentPlaceHolder>
</td>
<td width="900px">
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server">
</asp:ContentPlaceHolder>
</td>
</tr>
</table>
This is the content in the master page as you can see there are 4 menu items i tried to form as sidebar and i tried to inherit in home.aspx(one of the content pages) as follows:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Start.aspx.cs" Inherits="WebStore._Start"
MasterPageFile="~/Webstore.Master" %>
<asp:Content ID="StartSideBar" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
But unfortunately the sidebar is not at all getting displayed. I know i'm doing fundamentally wrong some where. My intention is to have web pages to have sidebar like this. It is a screenshot of my intended page.
Can some one guide me through this.
Thanks in anticipation
The way you have it set up, your home.aspx file is clobbering your menu links because it is defining the content of ContentPlaceholder1 to be empty (or something else at any rate), which is overriding the menu links you put in your MasterPage inside the same content holder. The way MasterPages and content pages work is that the MasterPage defines a location (a ContentPlaceholder) for the content page (e.g., home.aspx) to load content into. But you have put actual content you want to keep inside a ContentPlaceholder on your MasterPage--which means that any content page (again, like home.aspx) that defines content for ContentPlaceholder1 (like you do with StartSideBar) is going to override anything defined inside ContentPlaceholder1 on the MasterPage--in this case, your menu links.
If you want your menu links to remain constant on every content page, then you should move them out of ContentPlaceholder1 on the MasterPage and make them just markup on the MasterPage (in fact, you should probably remove ContentPlaceholder1 altogether). The easy way to do this is to comment out the ContentPlaceholder tag itself like this:
<table>
<tr>
<td width="150px">
<%--<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">--%>
<asp:Menu runat="server" ID="MainMenu1" CssClass="MasterContent" StaticSubMenuIndent="30px">
<Items>
<asp:MenuItem Text="My Software" NavigateUrl="~/MySoftware.aspx"></asp:MenuItem>
<asp:MenuItem Text="Check Out" NavigateUrl="~/CheckOut.aspx"></asp:MenuItem>
<asp:MenuItem Text="View Shopping Cart" NavigateUrl="~/ShoppingCart.aspx"></asp:MenuItem>
<asp:MenuItem Text="Continue Shopping" NavigateUrl="~/Start.aspx"></asp:MenuItem>
</Items>
</asp:Menu>
<%-- </asp:ContentPlaceHolder>--%>
</td>
<td width="900px">
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server">
</asp:ContentPlaceHolder>
</td>
</tr>
</table>
You will also have to remove the ContentPlaceholder reference from your content page as well, or you will get a compliation error (because you removed it from the Master, it can't exist in the content page--but that's okay, the MasterPage now has the links you want):
<%# Page Title="" Language="C#" MasterPageFile="~/Webstore.master"
AutoEventWireup="true" CodeFile="Start.aspx.cs" Inherits="Start" %>
<%--<asp:Content ID="SideBarStart" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>--%>
I've tried both snippets below. Nothing. I've tried <%#, <%=, and <%. Nothing. So I'm stuck.
<div style="background-color:Gray; color:white; text-align:center; width:100%;">
<asp:HyperLink ID="HyperLink1" Target="_blank" NavigateUrl='<%= Server.HtmlEncode(String.Format("~/ShowHistory.aspx?section={0}&jobnumber={1}", "APQP Header", "101244")) %>' runat="server">Show Updated History</asp:HyperLink>
<asp:HyperLink Target="_blank" NavigateUrl="~/ShowDeletedHistory.aspx" ID="HyperLink2" runat="server">Show Deleted History</asp:HyperLink></div>
<br />
<div style="background-color:Gray; color:white; text-align:center; width:100%;">
<asp:HyperLink ID="HyperLink1" Target="_blank" NavigateUrl='<%= String.Format("~/ShowHistory.aspx?section={0}&jobnumber={1}", "APQP Header", "101244") %>' runat="server">Show Updated History</asp:HyperLink>
<asp:HyperLink Target="_blank" NavigateUrl="~/ShowDeletedHistory.aspx" ID="HyperLink2" runat="server">Show Deleted History</asp:HyperLink></div>
<br />
Try <%# ... %> and call this.DataBind() (or Me.DataBind()) when your page loads.
Server controls cannot contain this type of tags. The reason is that "<%= %>" is basically equal to Response.Write, which itself executes after the page has gone through its lifecycle, when the response is already constructed. If you use it when setting a value of a server-side control property, this value has to be resolved when (or a little after) parsing the page markup. This is the reason you cannot use "<%= %>" in a server control.
If it was a normal html tag, it would work, but the virtual URL would not.
Is there a reason you're not setting the NavigationUrl in code? It would look much nicer to me.