Cannot use Response.Write in <head> section of aspx page? - asp.net

I'm trying to use the Response.Write() method to dynamically insert content in the < head > section of an aspx page. I need to inject a string value from a property on a code-behind object which is a link to my CSS file. However, it is not being processed properly at run time. The object is public on the class and is hydrated in the Page_Load() event. Down in the body of the page I can successfully inject other properties from the Corpoartion object with no problem at all.
Why does this not work in the < head > section?
This is the part that does not expand correctly:
<link href="<%= Corporation.PageStyleSheet %>" rel="stylesheet" type="text/css" />
Here is the entire < head > section:
<head runat="server">
<title></title>
<link href="<%= Corporation.PageStyleSheet %>" rel="stylesheet" type="text/css" />
<script language="JavaScript" type="text/JavaScript" src="cntv_menu.js"></script>
<script language="JavaScript" type="text/JavaScript" src="cntv_category.js"></script>
</head>
What is the reason that this will not expand properly?

You can't use <%= %> inside a runat="server" tag, which your <head> tag is.
You can either change it to <%# %> and DataBind to it in the code-behind, or you can make the link tag runat="server", give it an id and assign the attribute from the code behind.
See this answer, which goes into the details.

Use this:
this.myButton.Attributes.Add(attribute, value);
It worked for me :)

the best way to resolve this problem is using OnPreRender
Example:
First, define your tag:
<link href="~/css/your_default.css" type="text/css" runat="server" id="myCSS" />
And on the OnPreRender:
protected override void OnPreRender(EventArgs e){
base.OnPreRender(e);
myCSS.Attributes["href"] = "~/css/your_new.css";
}

If you write out the full line you should be ok:
<%
Response.write("<link href=\"" + Corporation.PageStyleSheet + "\" rel=\"stylesheet\" />");
%>
P.S. My syntax may not be completely right, sorry in advance.

Related

Why does urls get encoded in <link> tags within <head runat="server"> and how do I avoid it (asp.net)

(I have tested this with a vanilla asp.net site running from the webdev server and it is a problem here also):
I have the following markup in my .master file
<!DOCTYPE html>
<html>
<head runat="server">
<link href="/Styles/Site.css" rel="stylesheet" type="text/css" />
<link rel="alternate" type="application/rss+xml" title="rss" href="/Pages/Static/Feed.aspx?type=rss&lang=en" />
</head>
the rendered html comes out like this:
<!DOCTYPE html>
<html>
<head>
<link href="/Styles/Site.css" rel="stylesheet" type="text/css" />
<link rel="alternate" type="application/rss+xml" title="asdsad" href="/Pages/Static/Feed.aspx?type=rss&lang=en" />
</head>
(the rss link "&" has been encoded to "&")
however if i change the markup to
<!DOCTYPE html>
<html>
<head>
<link href="/Styles/Site.css" rel="stylesheet" type="text/css" />
<link rel="alternate" type="application/rss+xml" title="rss" href="/Pages/Static/Feed.aspx?type=rss&lang=en" />
</head>
(no runat="server" on the head tag)
then the resulting html comes out as expected:
<!DOCTYPE html>
<html>
<head runat="server">
<link href="/Styles/Site.css" rel="stylesheet" type="text/css" />
<link rel="alternate" type="application/rss+xml" title="rss" href="/Pages/Static/Feed.aspx?type=rss&lang=en" />
</head>
Clearly Asp.Net does something to encode the url. As it happens, I really need the head tag to be runat="server" and I would also like to be able to have "&" in link-urls within it is there some trick I can use to have my cake and eat it too?
Yours
Andreas
This issue happened to me before, I couldn't find out the reason for this, I ended up putting a literal inside the head, and filled the html from the code behind.
html:
<head runat="server">
<asp:Literal runat="server" ID='litLinks' />
</head>
C# code:
protected void Page_Load(object sender, EventArgs e)
{
litLinks.Text = "<link rel='alternate' type='application/rss+xml' title='rss' href='/Pages/Static/Feed.aspx?type=rss&lang=en' />"
+ "<link href='/Styles/Site.css' rel='stylesheet' type='text/css' />";
}
Note: If you're going to downvote, explain why, because nothing I have said is wrong, unless you otherwise prove so.
You're meant to escape ampersands in url's since putting a non-escaped ampersand in a url, the browser expects there to be something encoded. By escaping it, your telling the browser exactly what it is, an ampersand.
It doesn't break your links and is valid.
Also if you wanted to pass an actual ampersand in a url that isn't defining a new querystring paramter, then you would url encode the ampersand to be '%26'
Edit: Since you're probably using this in some really weird way. Here's the why, it's correct for ASP.Net to HTML Encode the ampersand for the HTML document.
When the browser issues a request for the URL, it doesn't send a request for the HTML encoded URL, it sends a request for the non-HTML encoded URL.
If for some reason you're accessing the value server-side or something, then you can do something like:
var url = new Uri(HttpUtility.HtmlDecode(#"http://www.google.com/somepage.aspx?key1=value1&key2=value2"));
var query = HttpUtility.ParseQueryString(url.Query);
var result = query["key2"];
Console.WriteLine(result);
So you decode the HTML version of the link first, parse it as a Uri, get the querystring from it and key your key/value collection.

Why does the asp.net head control mangle link elements?

Suppose I have the following markup in a standard ASP.NET 2.0 web form:
<head runat="server">
<title>My Snazzy Page</title>
<link type="text/css" href="<%= PathUtilities.AssetPath %>/css/page.css" rel="stylesheet" />
<script type="text/javascript" src="<%=PathUtilities.AssetPath %>/lib/jquery/1.4.2/jquery.min.js"></script>
</head>
What's odd is that this renders the <link> element literally, with the embedded code brackets, while it interpolates the output of the same code into the script tag. In other words, the browser sees this:
<head><title>My Snazzy Page
</title><link type="text/css" href="<%= PathUtilities.AssetPath %>/css/page.css" rel="stylesheet" />
<script type="text/javascript" src="/rmt/lib/jquery/1.4.2/jquery.min.js"></script>
</head>
Obviously the problem disappears if I remove the runat="server" from the head element.
Well what you're doing is (no offence) a bit silly, that is - having a <head> server-side element, with a nested <link> client-side element with server-side href attribute
You are dynamically rendering the href value from server code anyway, so a better solution would be to dynamically render the link tag from the server altogether.
Example (code-behind of page)
// Define an HtmlLink control.
HtmlLink myHtmlLink = new HtmlLink();
myHtmlLink.Href = "/css/page.css";
myHtmlLink.Attributes.Add("rel", "stylesheet");
myHtmlLink.Attributes.Add("type", "text/css");
// Add the HtmlLink to the Head section of the page.
Page.Header.Controls.Add(myHtmlLink);
Your ASPX then becomes much neater:
<head runat="server">
<title>My Snazzy Page</title>
<!-- CSS/JS included dynamically -->
</head>

conditional javascript in .ascx

I have a javascript src that i need to add to some of the pages in a site.
for example
<script type="text/javascript" src="http:abcxyz.com/zzz"></script>
I want to add this conditionally on a .ascx page - if the Request.ServerVariables["SCRIPT_NAME"] ends with certain criteria.
The ascx language is vb, and there is no code behind.
Thanks
If you make it so the tag is runat=server, you should be able to conditionally add the code as script:
<head runat="server">
<% If Request.ServerVariables("SCRIPT_NAME") = "value" Then %>
<script type="text/javascript" src="whatever.js"></script>
<% Else %>
<script type="text/javascript" src="whatever_else.js"></script>
<% End If %>
</head>
No code behind, but still code in front right?
This will probably do it...
<%= If(Request.ServerVariables("SCRIPT_NAME").EndsWith("<criteria>"), "<script type='text/javascript' src='http:abcxyz.com/zzz'></script>", "")%>

JQuery Datepicker issue - asp.net

I have used JQuery within my asp.net page. JQuery is working fine. I could see the calendar and can pickup the date. The problem is that when the page is postbacked the value is lost. Am i missing some code? Does anyone of you have the idea?
Below is what i have done -
1) Included the files -
<script src="../scripts/date.js" type="text/javascript"></script>
<script src="../scripts/jquery.datePicker.js" type="text/javascript"></script>
<link href="../css/DatePicker.css" rel="stylesheet" type="text/css" />
<link href="../css/DateCalendar.css" rel="stylesheet" type="text/css" />
2) Linked with the textboxes -
jQuery(function($){
Date.format = 'mm/dd/yyyy';
$("#<%=txtAssignDate.ClientID%>").datePicker({startDate:'01/01/1996'});
$("#<%=txtCloseFileDate.ClientID%>").datePicker({startDate:'01/01/1996'});
$("#<%=txtInspectionDt.ClientID%>").datePicker({startDate:'01/01/1996'});
});
If the datepicker is working fine and selecting a value, you probably forgot to tell the text field to retain it's value on postback. If you're using Visual Studio/Visual Web Developer, one of the properties of the textfield object is "EnableViewState", which should be set to true.
Here is my test code:
aspx:
<script type="text/javascript" src="/js/jquery-1.3.2.js"></script>
<script src="/js/date.js" type="text/javascript"></script>
<script src="/js/jquery.datePicker.js" type="text/javascript"></script>
<link href="/css/datePicker.css" rel="stylesheet" type="text/css" />
<form id="form1" runat="server">
Date: <asp:TextBox ID="txtDate" runat="server" /><br />
<asp:Button ID="btnSubmit" Text="Click" runat="server" OnClick="btnClick" />
</form>
C#:
protected void btnClick(object sender, EventArgs e)
{}
and it's working perfectly. Please check if you are running some code on postback which is resetting the field.
I found the reason. The problem was because of masking. I have also used the JQuery masking. I found the dates are saved in database but while displaying the dates within text fields it was wipe off the values having single digit because of masking mm/dd/yyyy. For ex. 09/01/2009.

ASP.NET masterpages: how to insert markup in the head section inside the aspx?

I know I can access the head section of a page which uses a masterpage programmatically this way (in code behind):
This is only an example (I'd like to insert scripts and styles etc.):
this.Header.Title = "I just set the page's title";
Is there a simple way to do this in a declarative way on in the aspx file itself?
Sometimes it would be handy to insert a client script or a style declaration or a link to an external resource.
You can do this by using content regions in the head, in exactly the same way as you would in the body of the page. eg, In your masterpage:
<head>
<link type="text/css" rel="stylesheet" href="/styles/common1.css" />
<script type="text/javascript" src="/scripts/common1.js"></script>
<asp:contentplaceholder id="ExtraStylesAndScripts" runat="server" />
</head>
And then in the page itself just something like:
<asp:content contentplaceholderid="ExtraStylesAndScripts" runat="server">
<link type="text/css" rel="stylesheet" href="/styles/extra1.css" />
<link type="text/css" rel="stylesheet" href="/styles/extra2.css" />
<script type="text/javascript" src="/scripts/extra1.js"></script>
<script type="text/javascript" src="/scripts/extra2.js"></script>
</asp:content>
For stylesheet you can use this :
HtmlLink cssRef = new HtmlLink();
cssRef.Href = "addins/main.css";
cssRef.Attributes["rel"] = "stylesheet";
cssRef.Attributes["type"] = "text/css";
Page.Header.Controls.Add(cssRef);
For Meta Tags :
HtmlMeta metaTag = new HtmlMeta();
metaTag.Name = "author";
metaTag.Content = "ScarletGarden";
Page.Header.Controls.Add(metaTag);
But there is no way to add external script files to header element.
You can add inside body element by :
if (!ClientScript.IsClientScriptIncludeRegistered("myExternalScript"))
{
ClientScript.RegisterClientScriptInclude("myExternalScript", "js/myJSFile.js");
}
Hope this helps !
You can declare the page title in the content page declaration.
<%# Title="Page Title" Page Language="C#" AutoEventWireup="true" CodeFile="Subpage.aspx.cs" Inherits="Subpage" MasterPageFile="~/MasterPage.master" %>
I haven't tried this.
But you can put HEAD element inside html with the enclosed string in asp style markup.
e.g.
<%=myTitle%>

Resources