Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 20th, 2003, 10:52 AM
Registered User
 
Join Date: Nov 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default User Control - Database connection

Am I right to say there shouldn't be any directive or <%@ %> tag in the .ascx / user control file? How do I make a db connection on page_load in .ascx and import it to .aspx?

 
Old November 20th, 2003, 11:37 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

All ASPX, ASCX, ASMX files will have <%@ ... %> at the beginning. This is the directive that tells ASP.Net what that file is, what it's codebehind file is, what class to inherit when it's instantiated. Don't remove it.

"Import" is not quite the correct term. (Depending on what you mean by it.)

If you want to make something publicly accessible in a user control (i.e. visible by the page) then you should use a public property to access it. You could just declare the object as public, but I wouldn't recommend it.

You could do this with a database connection object if you wanted to. You'd should declare the object within the scope of the page (Private m_objDBConn As ...). Then create a public property to access it. Because this is an object, you could make this property read only (as no caller should need to overwrite the object reference, they just want to deal with the object's members).

Public ReadOnly Property DBConnection() As <DBConnectionType>
    Get
        Return m_objDBConn
    End Get
End Property

Are you trying to use this user control simply for the purpose of adding a DBConn to pages that need it? Because that's not really the right application of user controls.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old November 20th, 2003, 11:09 PM
Registered User
 
Join Date: Nov 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok thanks for the explanation.
I'm not sure of what's the purpose / usage of user control.
Actually what I want to do is as the scenario below:

I wanted develop a database system and we know there must be a connection / comminucation between ASP.Net and our database. And this connection method is always the same, so I would like to create this connection method in a seperate file so that everytime I need the method I can just simlpy "import" (or some other similiar term) the file. One example is as below in ASP

DBConnection.inc
----------------
Dim oConn
Set oConn = Server.CreateObject(ADODB.Connection)
oConn.Open DSN=server

Login.asp
---------

Dim Rs, Query
Query = "Select * From Table1;"
Set Rs = Server.CreateObject(ADODB.Recordset)
Rs.Open Query, oConn

I just want something similiar in ASP.Net, so is there any suggestion or simple example? Thanks again!!

 
Old November 21st, 2003, 06:02 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

User controls are basically designed so that a web developer can create a composite control to be reused in an application.

Unfortunately, there is no immediately obvious replacement for the classic asp method of an include file that declares a "global" variable such as a db connection so that any page may include it.

A more advanced technique is now available with .Net because of the much better language support in ASP.Net applications. The basic idea is simply to construct a base class that inherits from System.Web.UI.Page. Then you create a Protected property on that class that provide access to your DB connection. In that class you can set up the connection in the Page.Load event handler and close the connection in the Page.UnLoad handler. Then all of your asp.net application's ASPX pages inherit from the base class you created instead of System.Web.UI.Page. This provides them with the standard web form class functionality but also the additions you made.

I have written up a slightly more comprehensive discussion on this idea. You can find it here.

Peter
------------------------------------------------------
Work smarter, not harder.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Add Windows User control in Web User Control agarwalvidhu C# 0 March 30th, 2006 01:17 AM
Help! Custom Server Control using User Control diehard ASP.NET 1.0 and 1.1 Professional 2 January 4th, 2006 12:33 PM
Help with control initialization in user control mike_remember ASP.NET 1.0 and 1.1 Professional 7 December 19th, 2005 11:08 AM
User Control Db Connection farrukhmuneer .NET Framework 2.0 1 February 11th, 2005 03:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.