Style sheet not being applied?

Hi Expert,

When I have the following code::
Response.Write("<Script language=javascript>alert('Please select an invoice ')</Script>");
my style sheet disappears/is not applied.

Even when I have Response.Write("anything") on my page it's the same.

Why is this and how can I stop it from happening?
Who is Participating?
zeroxpConnect With a Mentor Commented:
first check the syntax of where you include your stylesheet. to use <style/> or <link/> you have to put that in the <head/> element.
and please note the response.write will write information right on top of the page, that is outside all element(if you have a look of the html source, you will find that is before your DOCTYPE and <html/> element).  this will make your markup no longer well formed. so if you need to add some client side script to the page you should use Page.ClientScript.RegisterClientScriptBlock()

but when i tested it even i did Response.Write("some thing") the browser still pick up the style i set in a stylesheet, so maybe check for the stylesheet first.
kateLAuthor Commented:
Thanks zeroxp,
My css is in the Head of my page

What would RegisterClientScriptBlock do?
MSDN says:
The RegisterClientScriptBlock method adds a script block to the top of the rendered page.

Is that not just the same as adding the <script> block to the head of the page, what the difference?

the RegisterClientScriptBlock just give you the ability to add some client side script when you have to do that from server code (eg. when you want display a message dialog to display some server error message).
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

kateLAuthor Commented:
thanks zeroxp,

it happens everywhere is my code.

When i do:
Page.ClientScript.RegisterStartupScript(this.GetType(), this.UniqueID, "parent.window.frames[\'LeftFrame\'].location.href = '../../LeftMenu.aspx?selectedCustomerID=" + _customerID + "';", true);

the style sheet on LeftFrame is scraped

when i do:
Response.Write("<Script language=javascript>alert('Please select an invoice ')</Script>");

on a completely different page the style sheet of that page is scraped.

I apply the css like this:
<head runat="server">
    <title>Untitled Page</title>
    <link rel="stylesheet" href="../../styles/main.css" type="text/css" />

can you see anything wrong with it.

Thanks a million for the help
i tried to write some thing to the page and the stylesheet still worked. here is my files:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Test01._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="" >
<head runat="server">
    <title>Email Test Page</title>
    <link rel="stylesheet" type="text/css" href="test.css" />
    <form id="form1" runat="server">
    <div class="textSpecial">
       Some Text: ABC DEF GHI JKL
       <div id="Test">Other Text</div>

      protected void Page_Load(object sender, EventArgs e)
         if (!IsPostBack)
            ClientScript.RegisterClientScriptBlock(this.GetType(), "Key01", "<script type=\"text/javascript\">alert('gogoog');</script>", false);
            Response.Write("<script type=\"text/javascript\">alert('blah');</script>");
            ClientScript.RegisterStartupScript(this.GetType(), "Key02", "<script>alert(1);</script>");
            Response.Write("<Script language=javascript>alert('Please select an invoice ')</Script>");

.textSpecial {font-size:larger; color:#ff0000;}
#Test {font-size:100px; background-color:#f00; color:#fff; position:absolute; top:500px; left:500px;}

so is that cause your stylesheet completely not working or only some attributes not right?
kateLAuthor Commented:
I got it (or you did I should say!)

When I use:
Response.Write("<Script language=javascript>alert('Please select an invoice ')</Script>");

it doesn't work but when I use:
Page.ClientScript.RegisterStartupScript(this.GetType(), this.UniqueID, "alert('Please select an invoice ');", true);

Must be what you said "markup no longer well formed"

But strange that when you use Response.Write your css are applied just fine.

What happens when you do:
Response.Write("<script type=\"text/javascript\">alert('blah');</script>");
in a button click event (not your Page_Load event) because that's what I am doing?

I'd be interested to know
i pasted the same code into a Button_Click event handler, it still came up fine with the style.
i tried to change DOCTYPE to XHTML 1.0 strict/Transitional, HTML 4.01, XHTML 1.1 or no DOCTYPE declaration. they all worked fine.
kateLAuthor Commented:
hmmm so i guess we'll never know ... strange ...

Thanks a lot zeroxp, you've been a great help
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.