My <% %> not working in div. Why? - asp.net

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.

Related

How to avoid asp:UpdatePanel duplicate my contents?

I'm creating a webpage that refreshes every 5 seconds to show how much order is left. The problem is after it's refreshed, my contents starts to duplicate.
Here, the brief of my problems :
I'm showing :
Order Offline : 1 / Order Online : 0.
After Refresh, it will be :
Order Offline : 11 / Order Online : 00.
I refresh the page with with asp:UpdatePanel Triggers and asp:Timer event
I've tried googling for the answer and mostly I found answers by setting the ViewState to Disabled and also I've tried to change UpdateMode to Conditional
Here is my code's snippets :
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="ScriptManager1" />
<asp:Timer ID="Timer1" runat="server" Interval="5000"></asp:Timer>
<div class="row">
<div class="col-sm-2">
<asp:ImageButton ImageUrl="image.png" Width="170" ID="ClsBtn" runat="server" />
</div>
<div class="col-sm-4" style="text-align: right;">
<span id="date" style="font-size:10px; font-weight: 900;"></span>
<span id="time" style="font-size:10px; font-weight: 900;"></span>
</div>
<div class="col-sm-4" style="text-align: right;">
<asp:UpdatePanel ID="upTQ" runat="server">
<Triggers><asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" /></Triggers>
<ContentTemplate>
<span style="font-size: 15px; font-weight: 900;">OFFLINE : <asp:PlaceHolder runat="server" ID="phTotalQueue" /> / ONLINE : <asp:PlaceHolder runat="server" ID="phTotalQueueM" /></span>
</ContentTemplate>
</asp:UpdatePanel>
</div>
and this is my code behind :
phTotalQueue.Controls.Add(New LiteralControl("" & dtTO.Rows(0).Item("TotalOrderOffline") & ""))
phTotalQueueM.Controls.Add(New LiteralControl("" & dtTO2.Rows(0).Item("TotalOrderOnline") & ""))
I expect the output to be Order Offline : 1 / Order Online : 0. with no duplicate contents.
Let's first understand what happens: Your page is generated on the server, sent out to the browser, where it is displayed and then, each time it is being refreshed the server is requested and at that time, a LiteralControl is added to phTotalQueue and phTotalQueueM, respectively. Each time this runs, the controls already existent in phTotalQueue and phTotalQueueM, respectively will remain there, displaying the older values, but a new such control is being generated, displaying the new value besides the older values. I would like to kindly advise you to make sure you have an identifiable LiteralControl in your UpdatePanels, having an ID:
<asp:UpdatePanel ID="upTQ" runat="server">
<Triggers><asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" /></Triggers>
<ContentTemplate>
<span style="font-size: 15px; font-weight: 900;">OFFLINE : <asp:PlaceHolder runat="server" ID="phTotalQueue"><asp:Literal ID="phTotalQueueText" runat="server"></asp:Literal>
</asp:PlaceHolder> / ONLINE : <asp:PlaceHolder runat="server" ID="phTotalQueueM"><asp:Literal ID="phTotalQueueMText" runat="server"></asp:Literal></asp:PlaceHolder></span>
</ContentTemplate>
</asp:UpdatePanel>
Now, since you have identifiable LiteralControl instances, let's change the code-behind:
phTotalQueueText.Text = "" & dtTO.Rows(0).Item("TotalOrderOffline") & ""
and
phTotalQueueMText.Text = "" & dtTO2.Rows(0).Item("TotalOrderOnline") & ""
Alternatively you can remove the old controls from your UpdatePanels, or find them and modify their Text attribute, but those are hacky ways and I recommend the usage of identifiable tags.

<%# Eval("") %> in href prefixes relative path in aspx page

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;
}

How is my page creating two different types of checkbox?

Got a really weird thing happening on my page. I have some checkbox controls and some of them are rendering like this..
<span confirmmodified="true" class="aspNetDisabled"><input type="checkbox" disabled="disabled" name="ctl00$FormContents$TrackerDetails$OutOfScopeUSQS" id="ctl00_FormContents_TrackerDetails_OutOfScopeUSQS"><label for="ctl00_FormContents_TrackerDetails_OutOfScopeUSQS">Out of scope USQS</label></span>
But some of them are rendering like this...
<input type="checkbox" name="ctl00$FormContents$TrackerDetails$OutOfScopeSQA" id="ctl00_FormContents_TrackerDetails_OutOfScopeSQA"><label for="ctl00_FormContents_TrackerDetails_OutOfScopeSQA">Out of scope USQS</label>
See, no span? Cant work out what is going on I have looked at the page and control and doesnt seem like anything different is happening...
UPDATE: [this is the markup]
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="AuditStateTracker.ascx.cs"
Inherits="Dashboard.controls.AuditStateTracker" %>
<div class="grid_12 alpha">
<h2><asp:Literal runat="server" Text="<%$ Resources:LocalisedText, TrackerMigration%>" /></h2>
</div>
<div class="grid_1">
<asp:CheckBox ID="OutOfScopeUSQS" runat="server" Text="<%$ Resources:LocalisedText, OutOfScopeUSQS %>" />
</div>
<div class="grid_1" runat="server" ID="divOutOfScopeSQA">
<asp:CheckBox ID="OutOfScopeSQA" runat="server" Text="<%$ Resources:LocalisedText, OutOfScopeUSQS %>" />
</div>
<div class="grid_1">
<asp:CheckBox ID="OutOfScopeRS" runat="server" Text="<%$ Resources:LocalisedText, OutOfScopeRS %>" />
</div>
<div class="grid_1" runat="server" ID="divRingFencedSQA">
<asp:CheckBox ID="RingFencedSQA" runat="server" Text="<%$ Resources:LocalisedText, RingFencedSQA %>" />
</div>
<div class="grid_1">
<asp:CheckBox ID="RingFencedRS" runat="server" Text="<%$ Resources:LocalisedText, RingFencedRS %>" />
</div>
<div class="grid_1" runat="server" ID="divBusinessCase">
<asp:CheckBox ID="BusinessCase" runat="server" Text="<%$ Resources:LocalisedText, BusinessCase %>" />
</div>
<div class="grid_1" runat="server" ID="divDelisted" visible="false">
<asp:CheckBox ID="Delisted" runat="server" Text="<%$ Resources:LocalisedText, Delisted %>" />
</div>
The first checkbox is disabled, the second one isn't; that's how asp.net renders disabled checkboxes.
It's throwing that span on there so asp.net can decorate it with the css class aspNetDisabled. This would allow you to (optionally) style disabled controls differently.
Incidentally, this disabled class is actually configurable.
In earlier versions of ASP.NET Microsoft used to render the disabled = "disabled"attribute for your controls. However, since the HTML 4.01 specification, the disabled attribute is not valid anymore for each type of web control. It is still valid for input, but not anymore for span.
So what they've done is add a style class (in CSS) which is set to controls when they are disabled. This is also the case with any controls in a diabled control. By default, this style class is called `aspNetDisabled'.
You can actually use a different name for this class, but not for individual controls. The name of this style class can only be set as a static property on the root class WebControl, for instance at application start in your global.asax.cs.
protected void Application_Start(object sender, EventArgs e)
{
WebControl.DisabledCssClass = "disabled";
}
More annoying is that for checkboxes, ASP.NET renders a span around you checkbox with the defined style class on it.
<span disabled="disabled">
<input id="ctl00_UsecaseContent_ctl01_ctl01_bCVbox" type="checkbox" name="ctl00$UsecaseContent$ctl01$ctl01$bCVbox" checked="checked">
</span>
When you are using e.g. Bootstrap as your UI framework, and on top of that a checkbox specific framework, such as https://github.com/flatlogic/awesome-bootstrap-checkbox, this additional span can actually break the structure of your HTML checkbox control hierarchy.

How to display a default image if Eval ("Thumbnail") is null - asp .net

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';"/>

input losing src

If I use the following code without runat="server" the input's src works fine and I see the image coming through.
<div><input id="testButton" type="image" src="<%=TestButtonImageUrl %>" onserverclick="RedirectTest" /></div>
url is https://fpdbs.paypal.com/dynamicimageweb?cmd=_dynamic-image
But if I put the runat="server" in, for some reason, I get this for the url:
<div><input id="testButton" type="image" src="<%=TestButtonImageUrl %>" onserverclick="RedirectTest" runat="server" /></div>
url is http://localhost/%3C%=lButtonImageUrl%20%%3E
You cannot use the <%= %> syntax with server controls (including standard HTML elements with runat="server"). You have two choices:
Access the control in the code behind (as an HtmlInputControl) and assign the src attribute using the Attributes property: imageControl.Attributes["src"] = value;
Assign the attribute using the databinding syntax (src="<%# %>") and call imageControl.DataBind() from the code behind
Maybe I'm missing something. But runat server tag does not support code expression.
When you add runat="server to that html tag, Asp.Net converts it from string to HtmlControl - in this case of type HtmlInputImage. You can see this happen by adding:
<%= testButton.GetType() %>
Then the only thing you need to do is set the Src-property, which, contrary to other comments, you CAN do in inline aspx - no need for a code-behind file:
<%
testButton.Src = "/content/logo.png";
%>
<input id="testButton" type="image" runat="server" src="" onserverclick="RedirectTest" />
You need to set the Src-property BEFORE the actual input, which is a bit non-intuitive, the reason is that the code is run at render-time, so if the setting of Src-property is after the control, it is too late.
if jQuery is an option than you could try this:
<script type="text/javascript">
$(function() { $('#<%=testButton.ClientID %>').attr('src', '<%=TestButtonImageUrl %>'); });
</script>
...
<div><input id="testButton" runat="server" type="image" onserverclick="RedirectTest" /></div>
Update:
Another option is to create a HttpHandler with processing like this
public void ProcessRequest(HttpContext context)
{
var testButtonImageUrl = "https://fpdbs.paypal.com/dynamicimageweb?cmd=_dynamic-image";
context.Response.Redirect(testButtonImageUrl);
}
add in web.config path to handle image.img or whatever and update aspx
<div><input id="testButton" runat="server" type="image" src="image.img" onserverclick="RedirectTest" /></div>

Resources