ASP.NET inline coding: variable name is not replaced with value - asp.net

I have an ASP.NET page .In the page load i set the value of a public variable.and in the inline coding part,I Am loading a CSS which is the folder with the name which is available in the public variable.My HTML markup is as follows
<%# Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeFile="MyPage.aspx.cs" Theme="GridView" Inherits="GUI.MyPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>MyPage</title>
<link href="../Vendors/<%=vendorName%>/css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<%=vendorName %> <!-- here value is printed correctly -->
...
</body>
and in my code behind
public partial class MyPage: MyCommonClass
{
public string vendorName = "";
protected void Page_Load(object sender, EventArgs e)
{
vendorName = "ACLL";
}
}
But when i run the page, the <%=VEndorId%> is not replaced with the value in it .But in the Body,It is printing properly.But in the head it is not coming.I checked the ViewSource and find the source HTML as follows
<link href="../Vendors/<%=vendorName%>/Lib/css/tradein.css" rel="stylesheet" type="text/css" />

The two options are:
<link href="<%= string.Format("../Vendors/{0}/css/style.css", vendorName) %>" type="text/css" rel="Stylesheet" /> // as Greco stated
and
<style>
#import url("../Vendors/<%=vendorName%>/css/style.css");
</style>

Add the runat="server" tag to the link element.

Similar question and good answer here.
The solution is to move the quotes around the inline code into the code
<link href=<%="'../Vendors/" + vendorName + "/css/style.css'"%> rel="stylesheet"...
^ ^

Related

error in aspx page. You can only have one <title> element within the <head> element

net site with master and child pages. I am generating title via .cs code file on each child page. It works fine on the desktop. However, when I try to browse via mobile, it throws the above error. Below is code for master page.
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="xyz.SiteMaster" %>
<!DOCTYPE html>
<html lang="en">
<head runat="server">
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="css/styles.css" type="text/css" >
</head>
<body>
<form runat="server">
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
</form>
</body>
</html>
****child page code behind ***
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HtmlTitle title = new HtmlTitle();
title.Text = "child page title";
Header.Controls.Add(title);
}
}
*** child page ****
<%# Page Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="childpage.aspx.cs" Inherits="xyz._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
</asp:Content>
what is the best way to handle this. Appreciate your thoughts on this.

ASP.NET can't set page title from content page

I have this page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Charts : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Page.Header.Title = "Some title";
}
}
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Charts.aspx.cs" Inherits="Charts" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<ul>
<li>ReportMissionType</li>
</ul>
</asp:Content>
and in MasterPage.master there is title tag defined
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Title</title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
<meta name="keywords" content="" />
<meta name="description" content="" />
</head>
but still when I try to set title from content page (Above)
Page.Header.Title = "Some title";
it gives me error which says that
Page.Header.Title Object reference not set to an instance of an object.
why can't i set title from the content page ?
In order to access children of the head element, you need to set the runat="server" attribute on the head element.
<head runat="server">
<title>Title</title>
...
</head>
According to the documentation, Page.Header:
Gets the document header for the page if the head element is defined with a runat=server in the page declaration.
In order to have elements accessible server-side as controls in WebForms, they need to run at the server:
<head runat="server">

Page losing CSS Style after updating master page Controls

ASP.3.5 No AJAX
Master page has a external style sheet. It has 2 dropdownlists in top section.
When a new item is inserted, a value is added to each of the dropdownlists. The newly added value has to be selected in the dropdownlists.
From my content page I update the dropdownlists on the master page. The dropdownlists are getting updated correctly, but I am losing the CSS styles.
Here's my Masterpage.aspx code
<%# Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>My Project</title>
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
Here' my content page code:
protected void formview_ItemInserted(object sender, FormViewInsertedEventArgs e)
{
if (e.Exception == null)
{
//Force the dropdownlistboxes in the master page also selects the newly inserted First
DropDownList ddlFirst = Master.FindControl("ddlFirst") as DropDownList;
if (ddlFirst != null)
{
ddlFirst.DataBind();
Response.Write(ddlFirst.SelectedValue = Session["sFirstID"].ToString());
}
DropDownList ddlSecond = Master.FindControl("ddlSecond") as DropDownList;
if (ddlSecond != null)
{
ddlSecond.DataBind();
Response.Write(ddlSecond.SelectedValue = Session["sSecondID"].ToString());
}
}
}
What am I doing wrong?
Try this out.
<link rel="Stylesheet" href="StyleSheet.css" />

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.

ASP.NET 3.5: Master Page stylesheet variablized?

I'm new to ASP.NET coming from a PHP and ColdFusion background and I have a pretty simple question:
Within my Master Page, how can I make one of my CSS file links a variable, so when the appropriate page (Home for example) comes in, it contains the variable with the correct CSS file to use?
<!-- Custom CSS Files -->
<link href="<Page Specific CSS Variable>" rel="stylesheet" type="text/css" />
<link href="../../Content/Styles/Site.css" rel="stylesheet" type="text/css" />
</head>
Would this be the answer? Home.aspx
<asp:Content ID="Content1" ContentPlaceHolderID="PageStylesheet" runat="server">
<link href="../../Content/Styles/Home.css" rel="stylesheet" type="text/css" />
</asp:Content>
And then in Master Page:
<!-- Custom CSS Files -->
<asp:ContentPlaceHolder ID="PageStylesheet" runat="server" />
Is that right?
One solution would be to add a property to the master page that you can set from each web content form. So for example:
MasterPage.Master
<!-- Custom CSS Files -->
<link href="<%=this.PageSpecificCSSURL%>" rel="stylesheet" type="text/css" />
<link href="../../Content/Styles/Site.css" rel="stylesheet" type="text/css" />
</head>
MasterPage.Master.cs
private string mPageSpecificCSSURL = string.Empty;
public string PageSpecificCSSURL
{
get
{
return mPageSpecificCSSURL;
}
set
{
mPageSpecificCSSURL = value;
}
}
WebForm1.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
((SiteMaster) this.Master).PageSpecificCSSURL = "mypage.css";
}
Use a content section in the page header in the master page. That's its purpose; for CSS and/or script and whatnot that are specific to a content page. The content will be merged with the content of the master page, resulting in a full head section. All content placeholders can be put pretty much anywhere; title, head, foot, whatever you like, and you can nest master pages. They are just templates; you don't have to restrict yourself to BODY content.
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="REO.master.cs" Inherits="REO.REO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>REO CMS</title>
<link rel="Stylesheet" href="REO.css" type="text/css" />
<script src="jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="jqueryui-1.8%20jquery-ui.min.js" type="text/javascript"></script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="head" runat="server"></asp:ContentPlaceHolder>
</head>
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="AddCaseContact.aspx.cs"
Inherits="REO.AddCaseContact" MasterPageFile="~/REO.Master" %>
<asp:Content ContentPlaceHolderID="head" runat="server">
<script type="text/javascript">
function showMessage(wTitle, msg) {
var $dialog = $('<div></div>')
.html(msg)
.dialog({
autoOpen: false,
title: wTitle,
modal: true,
height: 300,
buttons: { "Ok": function() { $(this).dialog("close"); } }
});
$dialog.dialog('open');
}
</script>
</asp:Content>
<asp:Content ContentPlaceHolderID="MainContent" runat="server">
With you're help, the answer is within my edits. Perfect.

Resources