I laugh a little on the inside because I remember having this same problem.
If you have an "Inherits" attribute on the page directive (<%@ Page
Inherits="SomeClass"%>) any methods matching page events such as Page_Load
that exist inside of your code behind file will take precedence over the
ones inside of your <script runat="server"> tags. When creating a new page,
if you have the "Place code in separate file" check box checked, VS adds the
Inherits attribute for you so that you can use the code behind file and by
default adds the Page_Load meethod for you.
There is also an "AutoEventWireup" attribute on the page directive. This
tells the compiler to add all of the appropriate Page_... methods to their
event handlers based on name (if AutoEventWireup="true"). If the Inherits
attribute is specified, you are saying that you want to put your code in
another class. When you do this the compiler looks for those methods inside
in the class that is specified by the Inherits attribute first, then it
looks in the <script runat="server"> tags. If there are any duplicates, the
compiler will only add the methods in the other class to the event.
So what's happening is that the compiler is seeing the Page_Load() in your
code behind file, and ignoring the in you <script runat="server"> tag. If
you remove the method from the code behind it will then wire up the one in
the <script runat="server> tag. It's one or the other. An alternative is to
wire up the Page_Load method in the <script> tag yourself.
In efforts to keep this from getting too long, I won't go any further unless
you have more questions.
One thing I do want to mention is something I find really cool.
There are a number of events you can subscribe to by having a method named
something special IE. Page_Load, Page_PreLoad, Page_Loaded, Page_PreInit,
etc.
The cool thing is that you don't have to match the full signature of the
method. So instead of:
Page_Load(object sender, EventsArgs e){
Response.Write("Hello World");
}
You could just put:
Page_Load(){
Response.Write("Hello World");
}
The compiler will still wire up that event for you. :)
On Wed, Sep 3, 2008 at 10:31 AM, Cplus <[EMAIL PROTECTED]> wrote:
>
> I assume that anything in script tag should run when page loads. I am
> including the aspx code that is working if I use a code behind file
> but doesn't work if I use it within script tag in and .aspx file.
> Thanks in advance for any responses.
>
>
>
> <script runat="server">
> void Page_Load()
> {
> lblServerTime.Text = DateTime.Now.ToString();
> }
> </script>
>
>
> /* This thing works if I put it in code behind file
>
> public partial class _Default : System.Web.UI.Page
> {
> protected void Page_Load(object sender, EventArgs e)
> {
> lblServerTime.Text = DateTime.Now.ToString();
> }
> }*/
>
> <head runat="server">
> <title>First Page</title>
> </head>
>
> <body>
>
> <form id="form1" runat="server" method="post">
> <div>
> Welcome to ASP.NET 2.0! The current date and time is:
>
> <asp:Label
> id="lblServerTime"
> Runat="server"/>
>
>
> </div>
> </form>
> </body>
> </html>
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web
Services,.NET Remoting" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://cm.megasolutions.net/forums/default.aspx
-~----------~----~----~----~------~----~------~--~---