Wrox Programmer Forums
|
ASP.NET 1.x and 2.0 Application Design Application design with ASP.NET 1.0, 1.1, and 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.x and 2.0 Application Design 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 October 9th, 2003, 03:03 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 463
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to madhukp
Default Global variables and functions

I am new to ASP.net. I have the following doubts. Could anybody please help me ?

How can I have a global variable (for eg. database connection string)? I have defined one in global.asax. But how can I access it in a page ?

Can I have a module like thing in ASP.Net (similar to VB). I need to define some functions which should be accessible from all pages.

Suppose I have two buttons on a form. One is to submit the form. The other is to clear the form. I am using required field validator and other validator controls. When I click the clear button, I am getting the summary of validation messages instead of getting the form cleared. How can I say that the validation should happen only on submission ? I see that all buttons are submitting the form. I want only some buttons to submit the data. Others should do client side works like enabling / disabling controls, clear them etc. How can I achieve them ?
 
Old October 9th, 2003, 04:08 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In relation to your first question, namely the connection string problem I have used he Web.config file and its capabilities to store key/value pairs. This is done like this...
Code:
<configuration>
    <appSettings>
        <add key="connectionString" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\database.mdb" />
        <add key="illustrationLogo" value="graphics\logo.jpg" />
        <add key="dataTimeFormatString" value="MMMM, yyyy." />        
    </appSettings>
...
... and then you can retireve the information using System.Configuration like this...
Code:
this.connectionString = ConfigurationSettings.AppSettings["connectionString"];
(remember 'using System.Configuration;' (C#)).

As to the question about the global methods, there is several solutions I think. One is to make a class - say Common - and then implement the methods, which should be global in this class (e.g. TestMethod()). You can the call the methods like this...
Code:
(new Common()).TestMethod();
I am not too experienced with .NET aswell, so there might be more elegant solutions to this problem.

About the form submission... I would like an answer either. I have got a cancel button, which should just redirect to another page. You can see if this thread gives an answer this... http://p2p.wrox.com/topic.asp?TOPIC_ID=5049

And Peter thanks for the correction underneath.

Hope this helps

Jacob.
 
Old October 9th, 2003, 09:06 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, madhukp...

I think I found an answer for the button question. There is a property that you can set for a Button, which omits the validation. I have not tried it yet with Validation Controls implmented, however I think I will try something like this...
Code:
Button cancel = new Button();
cancel.ID = "cancelButton";
cancel.Text = "cancel";
cancel.CausesValidation = false;
.. let me know if it works in your setup!?

Hope it helps

Jacob.

 
Old October 9th, 2003, 09:57 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Jacob's answer on the common functionality is partially correct.

In most cases, you create a separate class file (or many depending on how you need to organize things) that contains common functionality. You need to determine if the methods you have can be called totally independent of an instance of the class they are in (remember that all methods must live in a class). If the method can work totally by itself, then you just need to make it accessible without the class.

In a class you define a function as such:
(C#)
public class myCommonFunctions
{
    public static void myFunction()
    {
    }
}
(VB)
Public Class myCommonFunctions
    Public Shared Sub myFunction()
    End Sub
End Class

Now, elsewhere in your code you can just call the functions like this:
myCommonFunctions.myFunction()

Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
Are functions allowed in global.asa file mat41 Classic ASP Basics 1 January 10th, 2008 01:58 AM
Problem accessing global variable inside functions jedijr88 Beginning PHP 3 June 29th, 2007 12:51 PM
Global variables zarol Beginning PHP 3 May 10th, 2005 06:17 AM
Help with functions and variables DeadlyDesigns.NET Beginning PHP 6 January 26th, 2005 12:28 AM
Functions and variables collie VB.NET 2002/2003 Basics 3 February 5th, 2004 02:45 AM





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