Server Side Code In Master Page Head

Delford Chaffin asked on April 6, 2015 20:16

I have the following in the HEAD area of my Master Page:

<script runat="server">
  protected void Page_Load(object sender, EventArgs e)  
  {
    Response.Write("::::::");
  } 
</script>

But when I view source on the page, I see the code (i.e. it's not being processed as server-side code. What am I missing?

Thanks!

Image Text

Recent Answers


Charles Matvchuk answered on April 6, 2015 20:36 (last edited on April 6, 2015 20:45)

Does it work in the body and not just in the head section ?

0 votesVote for this answer Mark as a Correct answer

Delford Chaffin answered on April 6, 2015 21:28

It does, actually.

Here is my ultimate goal. I need to set a cookie and then have web parts pick up that value. It seems Web Parts all run there OnInit methods before code in the body.

So that is what I really need - a way to set a cookie early enough that it is available as the web parts run their OnInit methods.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on April 6, 2015 21:40

Put a webpart on your master page do do this. OR in the layout of your master page add the code you need. I'd suggest going with a custom webpart that does your cookie work. You should also look into the CookieHelper class, it works better with Kentico than the standard .net cookie methods.

0 votesVote for this answer Mark as a Correct answer

Delford Chaffin answered on April 7, 2015 16:07

Brenden,

First off - nice hat (Go Vols!).

And thanks, I already had a custom web part taking care of the cookies and it wasn't working quite the way I needed and that's why I was trying other things. What I found out through this process is that a web part's OnInit is fired after OnContentLoaded - not sure I would have guessed that as "Content Loaded" would seem to imply it was towards the end of the process.

I ended up moving all the cookie set up and checking code (in other web parts) to the OnInit method and all seems to be working well now. I did use the CookieHelper class as well as it simplified the code - it's affect on the actual timing issue I'm less sure of.

At the end of the day, what would be really nice to have is some kind of Kentico page life cycle (i.e. what events fire in what order). Does anyone know if this exists? I haven't found it yet.

Thanks!

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on April 7, 2015 17:40

Delford,

I had once documented that and how it was different from standard asp.net but not sure where I have that now. Maybe check out this PowerPoint from Connections 2010. It's for v7 but should still be valid or very close.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.