Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > "Binding" objects to the top level Page class

Reply
Thread Tools

"Binding" objects to the top level Page class

 
 
Davíð Þórisson
Guest
Posts: n/a
 
      10-25-2004
asking a lot these days... how would I ever learn .Net without the
newsgroups!! Well I'm stuck on yet another point. All pages on my web have a
user control (initialise) that handles for example database connection;
opens db on page_onload and closes db on page_unload. But so that the
connection object is as easily accessible from all subpages and other user
controls I'd like to somehow "bind" it to the top level Page class.
Otherwise I foresee problems with accessing the db object from sub-codes.
Can anyone please tell me how do to this kind of thing?? Or should I maybe
use some other way?


 
Reply With Quote
 
 
 
 
Karl Seguin
Guest
Posts: n/a
 
      10-25-2004
I think most people would tell you two answers:

First, the typical way to achieve what you are trying to do is to use a
comon base-page for all of your pages:

public class MyBasePage
inherits System.Web.UI.Page

private _connection as SqlConnection
public property Connection as SqlConnection
get
return _connection
end get
set
_connection = value
end set
end property


sub page_load
_connection = new Sql...
end sub



and have all your pages inherit from it:

public class WebForm1
Inherits MyBasePage



But the other thing people would tell you is that you shouldn't be using
connections like this. There are a couple good reasons. First of all,
codebehind is a presentation logic layer, and it shouldn't do anything with
System.Data.SqlClient (some people, like me would say it shouldn't do
anything with the complete System.Data namespace, but that's for another
day). You should be using a data access layer through a business layer for
all database connection stuff. Secondly, even if you ignore the first piece
of advice, you shouldn't be opening a single connection and closing it at
the end of the request. You should open connections as late as you need
them and close them as soon as you are done with them. Thanks to connection
pooling it's much better to open and close connections 20 times in a page's
life, than to hold one open at the start and close it at the end...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


"Davíð Þórisson" <> wrote in message
news:...
> asking a lot these days... how would I ever learn .Net without the
> newsgroups!! Well I'm stuck on yet another point. All pages on my web have

a
> user control (initialise) that handles for example database connection;
> opens db on page_onload and closes db on page_unload. But so that the
> connection object is as easily accessible from all subpages and other user
> controls I'd like to somehow "bind" it to the top level Page class.
> Otherwise I foresee problems with accessing the db object from sub-codes.
> Can anyone please tell me how do to this kind of thing?? Or should I maybe
> use some other way?
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
c is a low-level language or neither low level nor high level language pabbu C Programming 8 11-07-2005 03:05 PM
page-level vs control-level enableViewState =?Utf-8?B?Sm9l?= ASP .Net 3 10-26-2005 07:14 PM
Page Level and Applicatoin Level Custom Errors rranveer@gmail.com ASP .Net 2 02-13-2005 02:03 AM
[Reflection] Possible to query instance of abstract top level classwithout having the .class file of the concrete class? Stefan Siegl Java 1 08-19-2004 02:26 AM



Advertisments