Results 1 to 4 of 4

Thread: no reference to object

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    no reference to object

    Whenever I try to run a page with a stylesheet link in it, I get an error that says you need runat="server" in the head tag.

    So I found this and tried it in the Page_Load event

    Code:
            HtmlLink fred = new HtmlLink();
            fred.Href = "Stylesheet.css";
            fred.Attributes.Add("rel", "stylesheet");
            fred.Attributes.Add("type", "text/css");
            Page.Header.Controls.Add(fred);
    But when I run it it says

    Object reference not set to an instance of an object.

    Why? When I am typing it intellisense fills it all in. I have used the 'new' keyword. What is wrong?

    Thanks for any help.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: no reference to object

    So why not just add runat="server" in the <head> tag?

    <head runat="server" id="PageHead">

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    Re: no reference to object

    Quote Originally Posted by mendhak
    So why not just add runat="server" in the <head> tag?

    <head runat="server" id="PageHead">
    Because if I do that the javascript I have on the page does not run. I tried moving the javascript out of the <head> and into the <body> but still does not run. This page also has Ajax controls on it. I'm beginning to think they cause problems.

    What is particularly weird is that loads of pages on the site run quite happily with the usual stylesheet link in the head. The only ones that ask for runat="server" in the head tag are the ones that use the Ajax Toolkit.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: no reference to object

    Question: Are you using themes? Even a default one? If yes, put your CSS in the default theme folder.

    As for the requirement by the ajax toolkit, this is most likely because the ajax code needs to dynamically inject <script> tags into the head section, hence the runat="server" needed.

    You mentioned that if you add the runat="server" your javascript stops running. (Why do you get all the weird situations?) Could you instead then attempt to use code similar to post 1 to add your javascript? Or, if it's in-page javascript code, use Page.RegisterClientScriptBlock

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width