Page losing CSS Style after updating master page Controls - asp.net

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

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.

Preselecting Dropdown value in .Net

I have a webform whose fields change based on the value of a dropdown. Is there a way I could have a link to this page select one of these values when it loads.
For example, if the form was about traveling, the external "car" link would autoselect "car" in the dropdown.
You could use a QueryString parameter, such as ?preselect=car and then act on the value of that parameter in your code:
<%# Page Language="C#" AutoEventWireup="true" %>
<script runat="server">
private void page_load(Object sender, EventArgs e)
{
if (Request.QueryString.AllKeys.Contains("preselect"))
{
MyDropDownList.Items.Cast<ListItem>()
.Where(li => li.Value == Request.QueryString["preselect"].ToString())
.First().Selected = true;
}
}
</script>
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:DropDownList runat="server" ID="MyDropDownList">
<asp:ListItem Value="asdf">Something</asp:ListItem>
<asp:ListItem Value="car">Car</asp:ListItem>
</asp:DropDownList>
</form>
</body>
</html>

ASP.NET Page / MasterPage life cycle

Trying to understand how after MasterPage render, the content of Page already exists in HtmlTextOutput?
ASP.NET, first calling Page.Render, then Page renders it's children controls and MasterPage is in theory "child control" of page. If so, how then after MasterPage.Render completes it contains the output for all other child controls of Page? How Master.Render, "calls" render of page itself and it's child controls?
Example:
MasterPage:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="MAIN.master.cs" Inherits="Demo.MAIN" %>
<!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></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
ContentPlaceHolder - BEFORE
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
ContentPlaceHolder - AFTER
</div>
</form>
</body>
</html>
public partial class MAIN : System.Web.UI.MasterPage
{
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
//At this point HtmlTextWriter already contains the output of all page child controls...how it's happen?
}
Page:
<%# Page Title="" Language="C#" MasterPageFile="~/MAIN.Master" AutoEventWireup="true" CodeBehind="MAIN_PAGE.aspx.cs" Inherits="Demo.MAIN_PAGE" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
CONTENT
<TL:MyButton id="ServerControl" Text="btn" runat="server"/>
</asp:Content>
public partial class MAIN_PAGE : MyPage
{
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
}
}

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.

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

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"...
^ ^

Resources