Hi, I'm comming from .NET 1.1 with Delphi to .NET with VS2005 C#.
At this time, I have 2 questions:
How can I add a Global.asax with code in Global.asax.cs?
How can I add a property or method to Global and access it from a WebForm?
Regards
Select "Website" > "Add New Item"
In the dialog, there will be an option to add a Global.asax (listed as Global Application Class) to your project. It should open up in code view for you to modify. It will be very similar to what you worked with in 1.1
I did it yesterday, but I can't add a Global.asax.cs file and I can't use my properties in Global from others pages.
I'm a VB guy but shouldn't it just be Global.asax? Not Global.asax.cs?
Your global.asax file should open up like this:
<%@.ApplicationLanguage="C#" %>
<scriptrunat="server">
void Application_Start(object sender,EventArgs e){
// Code that runs on application startup
}
void Application_End(object sender,EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender,EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender,EventArgs e){
// Code that runs when a new session is started
}
void Session_End(object sender,EventArgs e){
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
</script>
And obviously the method are there.
Just go to the menu file -> new file -> global.asax
Hope this helps.
Let me know if you have any more problems.
Fine, I got same here yesterday, can I have my code in a .cs file or just in .asax?
Now, add this code to Global
public DateTime ThisTime {
get {
return DateTime.Now;
}
}
And try access it from WebForm1.aspx.
The global file should not have .cs extension
Why are you doing the get in global.asax?
You can get time anywhere.
In .NET with Delphi, I do this:
my Global declaration in Codebehind:
TGlobal = class(System.Web.HttpApplication)
{$REGION 'Designer Managed Code'}
strict private
procedure InitializeComponent;
{$ENDREGION}
strict protected
procedure Application_Start(sender: System.Object; e: EventArgs);
procedure Session_Start(sender: System.Object; e: EventArgs);
procedure Application_BeginRequest(sender: System.Object; e: EventArgs);
procedure Application_EndRequest(sender: System.Object; e: EventArgs);
procedure Application_AuthenticateRequest(sender: System.Object; e: EventArgs);
procedure Application_Error(sender: System.Object; e: EventArgs);
procedure Session_End(sender: System.Object; e: EventArgs);
procedure Application_End(sender: System.Object; e: EventArgs);
private
FDB: AsaConnection;
public
constructor Create;
published
property DB: AsaConnection read GetDB;
end;
Now using it form a page:
myVar := TGlobal(HttpContext.Current.ApplicationInstance).DB;
I wanna to do the same in c#2.0 to create only 1 connectino to db per request.
could you describe what you are trying to do in just a little bit more detail?
If you want to do an db connection, you can do it in many ways. ADO.net should ensure that there is only one connection per request.
Depending on what database you have, you can use sql, oledb connection class to do these.
Are you trying to connect to db for the entire application?(ado.net discount and reconnet everytime you connect to db by default)
zote:
I think for what you are trying to do, the way to accomplish this would be to create a baseclass and have your aspx page inherit from the baseclass. Then you can add the connection instantiation to the baseclass as a property. Thus, once the baseclass has instantiated the connection, it will be available via 'this.MyConnection()' throughout the trip to the server. Nothing would occur in the Global.asax.
What others are also communication is that there is no Global.asax.cs file. The Global.asax file IS ultimately the location of the code.
I'm trying to add a method or property to Global and access it from a WebPage.
zote:
zote:
I think for what you are trying to do, the way to accomplish this would be to create a baseclass and have your aspx page inherit from the baseclass. Then you can add the connection instantiation to the baseclass as a property. Thus, once the baseclass has instantiated the connection, it will be available via 'this.MyConnection()' throughout the trip to the server. Nothing would occur in the Global.asax.
What others are also communication is that there is no Global.asax.cs file. The Global.asax file IS ultimately the location of the code.
Please read his reply carefully.
I think this is what you want.
But,If you want something like a global method, it would be easier to create a web service.
0 comments:
Post a Comment