Access control on a page from inside an external class

  • Thread starter Thread starter Andy B
  • Start date Start date
A

Andy B

I have a class I am creating for data access. I need to access controls from
inside the class that are on a particular page. How do I do this? or is
creating an instance of the page class and using FindControl the only way to
do it?
 
Hi Andy,
It is not a good practice to add some UI code in your data layer. Normally
we write the Data Access layer to abstract the database related code from
business and Presentation layer code.

Instead i would suggest you to write parameterized methods in your data
layer and make an object of this class in business/UI layer and then call
these methods from there.

for example you can have a method that authenticates user so you can do it
this way:

In data layer:

Public function AuthenticateUser(ByVal userName as string, ByVal password as
string) As boolean

'Make the Db call here and then return the result as true/false depending on
whether you got a row for the given username-password or not.



End function

Please let me know if it worked for you.else send me the code that you are
writing.

regards,
Joy
 
I would have at least 8 or so UI controls that the class would need to have
access to. Is this an unusual amount of parameters? I was possibly thinking
of sending a collection of controls but not for sure if that will work or
not...
 
Hi Andy,
I would have been better equipped to help you, had i been aware of your
business scenario.

However, if you have more than 3 control's value to be passed as parameter
then i would suggest you to create a Parameter class. The following example
will make the suggestion more clear for you:

Suppose you had to design an Customer registration page.
Suppose it had the following text fields:
1. Name
2. Age
3. City
4. Country
5. Language
6. Phone
7. Fax

You can write a Customer class that has the following private members and
should also be supported by getter/setter properties for each of the private
members:

Public Class Customer
{
Private string strName ;
Private string strAge;
Private string strCity;
Private string strCountry;
Private string strLanguage;
Private string strPhone;
Private string strFax;

//Getter/Setter properties will follow here
public string Name
{
get
{
return strName ;
}
set
{
strName = value;
}
}

}

Then write the Data layer methods as such that they accept Customer class
object as parameters.
In the UI layer first make a Customer object and then initialise all its
properties with control's values and then make an object of the data layer
and call the DB method on this object thereby passing it the Customer object
as parameter.



Please let me know if this worked for you.

regards,
Joy
 
Hi Joy,

Sorry it took so long to get back to you on this subject. I haven't tried
your idea yet, but it is an interesting idea for some other part of the
project possibly. I don't think this will work directly for this particular
problem. Here is in some more detail of what I need to do, now that I have
clearance to do so. On this particular page, there is a wizard control that
has 7 different steps to it. In step 1, there are the following controls:
1. ContractHeaderDirectionsLabel - Gives directions for step 1 of the
wizard.
2. ContractTitleTextBox - A textbox that will contain a title for the
created contract (entered by user).
3. ContractTitleTextBoxCounter - A custom built control that counts the
words/characters in the ContractTitleTextBox.
4. ContractTypeTextBox - A textbox that will contain the type of contract
being created (entered by user).
5. ContractTypeTextBoxCounter - A custom built control that counts the
words/characters in the ContractTypeTextBox.
6. HeaderActionButton - A LinkButton that when pressed, either initially
saves or saves changes in the contract header depending on the state of the
wizardStep.

The different "states" of the wizard step are:
1. Default- shows the wizard in a state where the contract header has never
been created before. The HeaderActionButton above would have a Text value of
"Save header" and a CommandName value of "FirstSave".
2. Preview mode where the HeaderActionButton above has the TextValue of
"Edit header" and a CommandName of "EditHeader". In this mode, the 2
textboxes above (ContractType and ContractTitle) would be changed to
readonly, the 2 TextBoxCounters would be made to be invisible and the
ContractHeaderDirectionsLabel would be changed to display a new set of
directions.
3. EditMode - This mode would have the HeaderActionButton Text value set to
"Save changes" and the CommandName set to "SaveChanges". In this mode the 2
TextBoxes are set back to their default mode with the current header
information inside the TextBoxes, the 2 TextBoxCounters would return to a
visible state and the HeaderDirectionsLabel would show another set of
directions for editMode.

This is why I need to know exactly how to handle this sort of stuff with
accessing controls from outside the page itself. Am I going to far by saying
that I need to have it done this way? I am looking at the plans for the 7
wizard steps, and it looks like putting all of that code inside the page
class would make the page very huge and bulky for what it does. It is
looking like the full 7 step wizard will take up around 300 or more code if
all in the page class. How should I continue with the controls access issue?
 
Hi Andy,
After going thru the task that you have in hand i would still suggest the
same way as i had told you earlier because of the following reasons:
1. Non Bulky code for UI - If you decide to make use of parametrized class,
then you can be rest assured that it is not going to make your UI page bulky,
the reason is even if you write the code in data access layer then also that
DLL will be loaded in the context of Page DLL, in essence lines of code
written in the data access class is not going to reduce the actual code being
loaded in runtime.
2. Runtime - As a general practice we should choose to do most of the things
statically rather than at runtime. For exampls, if you choose to write the
code in Data access layer then to be able to grab the UI controls of the page
you would first need the Http Context (executing context) and then only you
would be able to access the controls.

ALTERNATIVE Approach:
In .NET 2.0 we have a new feature called Cross-page posting, in this you can
have a UI page posting itself to a different page and in the code behind of
that page you can easily access the previous page's controls. The reason you
are able to do so is in this the UI page (having the controls) is a strongly
typed class and the next page can easily access this class.

Take a look at the following URL for cross-page posting example:

http://blogs.msdn.com/tinghaoy/archive/2005/12/15/504357.aspx

regards,
Joy
 
So I create a class that holds the values required to switch the UI to the
different states, pass the controls to this class in the UI and then change
the values elsewhere? If I had an example of doing your idea with an actual
control (like a textbox) then It would be easier to get how to do...
 
Hi Andy,
Did you take a look at how to make use of Cross-page posting?

give me your mail ID and i will mail you the sample code for parmaterized
class and would you want it in VB .NET or C#?

regards,
Joy
 
Back
Top