Jan
10
2007
Step 3 - page2.aspx
Posted by admin under
ASPCodeHeaderManager
In this page we are using two controls (just to show you it's possible).
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="page2.aspx.cs" Inherits="page2" Title="Untitled Page" %>
<%@ Register TagPrefix="headermanager" Namespace="ASPCodeHeaderManager" Assembly="ASPCodeHeaderManager" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<headermanager:ASPCodeHeader ID="whatever" runat="server">
<IncludeScripts>
<headermanager:IncludeScript Path="~/scripts/hello.js" />
<headermanager:IncludeScript Path="~/scripts/world.js" />
</IncludeScripts>
<IncludeStylesheets>
<headermanager:Stylesheet Path="~/css/sheet2.css" />
</IncludeStylesheets>
<ScriptsInHeader>
<headermanager:GlobalScriptArea>
function world123()
{
}
</headermanager:GlobalScriptArea>
</ScriptsInHeader>
</headermanager:ASPCodeHeader>
<h1>this one includes the javascripts hello.js and world.js </h1>
<br />
<br />
Test hello to click <a href="javascript:Hello();">here</a>
<br />
Test world to click <a href="javascript:World();" title="Page 2">here</a>
Somewhere later we have another
<headermanager:ASPCodeHeader ID="ASPCodeHeader1" runat="server">
<IncludeScripts>
</IncludeScripts>
<IncludeStylesheets>
</IncludeStylesheets>
<ScriptsInHeader>
<headermanager:GlobalScriptArea Id="pekka">
function hello123()
{
}
</headermanager:GlobalScriptArea>
</ScriptsInHeader>
</headermanager:ASPCodeHeader>
<br />
<asp:Label ID="lblPekka" runat="server">1</asp:Label>
<br />
<asp:Label ID="clickhere" runat="server" BackColor="Aqua">Click here to set label above to 42</asp:Label>
</asp:Content>
Here we include some scripts
<headermanager:IncludeScript Path="~/scripts/hello.js" />
<headermanager:IncludeScript Path="~/scripts/world.js" />
we include another stylesheet
<headermanager:Stylesheet Path="~/css/sheet2.css" />
We also add a globalscriptarea:
<headermanager:GlobalScriptArea>
function world123()
{
}
</headermanager:GlobalScriptArea>
And for the second control we add another globalscriptarea:
<headermanager:GlobalScriptArea>
function hello123()
{
}
</headermanager:GlobalScriptArea>
And to top it off: by codebehind (cs file) we add another globalscriptarea:
private void SetupJS()
{
string s1 = @"function OnClick()
{
alert('Yupp');
document.getElementById('THECTRL').innerText = '42';
}
";
s1 = s1.Replace("THECTRL", lblPekka.ClientID);
ASPCodeHeaderManager.GlobalScriptArea oArea = new ASPCodeHeaderManager.GlobalScriptArea();
oArea.Id = "what";
oArea.Content = s1;
ASPCodeHeader1.ScriptsInHeader.Add(oArea);
clickhere.Attributes.Add("onclick", "OnClick()");
clickhere.Style.Add("cursor", "pointer");
}
That's just so useful when you need to get around name mangling by using ClientID for something in the javascript.
Now all that results in this:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
Untitled Page
</title><script type="text/javascript" src="/www/scripts/hello.js"></script><script type="text/javascript" src="/www/scripts/world.js"></script><meta Name="keywords" Content="hello, there2" /><meta Name="description" Content="Jojamenjavisst" /><link type="text/css" rel="stylesheet" href="/www/css/sheet2.css" /><script type="text/javascript">
function world123()
{
}
function hello123()
{
}
function OnClick()
{
alert('Yupp');
document.getElementById('ctl00_ContentPlaceHolder1_lblPekka').innerText = '42';
}
</script></head>