public fields in webforms

G

Guest

All the fields in webforms are automatically declared as protected.
If I change them to public, they are converted back to protected.

Why is this happening ?

Thanks
 
J

Jim Cheshire [MSFT]

Trebor,

The reason that's happening is that controls in your Webform cannot be
public. When you browse to a Webform, an instance of that class is
created. The request is served and a response is sent to the client. At
that point, the instance of your Webform is no longer accessible.
Therefore, it would be useless to have a public member of a Webform.
Protected is the correct access modifier.

Jim Cheshire, MCSE, MCSD [MSFT]
ASP.NET
Developer Support
(e-mail address removed)

This post is provided "AS-IS" with no warranties and confers no rights.

--------------------
 
G

Guest

I'm asking this question besause I found some articles on the web
which are recommending Server.Transfer for moving between forms.

Ex: // New public properties are defined in WebForm1:
public string TBText
{
get
{
return TextBox1.Text;
}
}


// Then on ButtonClick event, the other form is invoked

Server.Transfer("WebForm2.aspx");
----------------------------------------------------------

The destination form - WebForm2 is using code like this to access fields in
WebForm1.

WebForm1 Form1;
Form1 = (WebForm1) HTTP.Context.Handler;
TextBox1.Text = Form1.TBText;

I was wonderig if I can avoid the step for creating public properties, by
declaring the fields in WebForm1 as public,
and then access their values in WebForm2 like:

Ex. 2

WebForm1 Form1;
Form1 = (WebForm1) HTTP.Context.Handler;
TextBox1.Text = Form1.TextBox1.Text;


Is the approach in Ex1 Ok?

Is the approach in Ex2 Wrong and Why?

Thanks








Jim Cheshire said:
Trebor,

The reason that's happening is that controls in your Webform cannot be
public. When you browse to a Webform, an instance of that class is
created. The request is served and a response is sent to the client. At
that point, the instance of your Webform is no longer accessible.
Therefore, it would be useless to have a public member of a Webform.
Protected is the correct access modifier.

Jim Cheshire, MCSE, MCSD [MSFT]
ASP.NET
Developer Support
(e-mail address removed)

This post is provided "AS-IS" with no warranties and confers no rights.

--------------------
From: <Trebor>
Subject: public fields in webforms
Date: Thu, 8 Apr 2004 12:55:49 +0200
Lines: 9
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet
NNTP-Posting-Host: 195.26.139.69
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:224389
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

All the fields in webforms are automatically declared as protected.
If I change them to public, they are converted back to protected.

Why is this happening ?

Thanks
 
J

Jim Cheshire [MSFT]

Trebor,

You can't use method 1 because the variable will be out of scope. If you
want to use that method, you should declare the variable as a static
variable.

Jim Cheshire, MCSE, MCSD [MSFT]
ASP.NET
Developer Support
(e-mail address removed)

This post is provided "AS-IS" with no warranties and confers no rights.


--------------------
From: <Trebor>
References: <#[email protected]>
Subject: Re: public fields in webforms
Date: Fri, 9 Apr 2004 14:23:21 +0200
Lines: 97
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet
NNTP-Posting-Host: 195.26.139.81
Path: cpmsftngxa06.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTFEED01.phx.gbl!TK2MSFTNGP08
.phx.gbl!TK2MSFTNGP09.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:224632
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

I'm asking this question besause I found some articles on the web
which are recommending Server.Transfer for moving between forms.

Ex: // New public properties are defined in WebForm1:
public string TBText
{
get
{
return TextBox1.Text;
}
}


// Then on ButtonClick event, the other form is invoked

Server.Transfer("WebForm2.aspx");
----------------------------------------------------------

The destination form - WebForm2 is using code like this to access fields in
WebForm1.

WebForm1 Form1;
Form1 = (WebForm1) HTTP.Context.Handler;
TextBox1.Text = Form1.TBText;

I was wonderig if I can avoid the step for creating public properties, by
declaring the fields in WebForm1 as public,
and then access their values in WebForm2 like:

Ex. 2

WebForm1 Form1;
Form1 = (WebForm1) HTTP.Context.Handler;
TextBox1.Text = Form1.TextBox1.Text;


Is the approach in Ex1 Ok?

Is the approach in Ex2 Wrong and Why?

Thanks








Jim Cheshire said:
Trebor,

The reason that's happening is that controls in your Webform cannot be
public. When you browse to a Webform, an instance of that class is
created. The request is served and a response is sent to the client. At
that point, the instance of your Webform is no longer accessible.
Therefore, it would be useless to have a public member of a Webform.
Protected is the correct access modifier.

Jim Cheshire, MCSE, MCSD [MSFT]
ASP.NET
Developer Support
(e-mail address removed)

This post is provided "AS-IS" with no warranties and confers no rights.

--------------------
From: <Trebor>
Subject: public fields in webforms
Date: Thu, 8 Apr 2004 12:55:49 +0200
Lines: 9
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet
NNTP-Posting-Host: 195.26.139.69
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:224389
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

All the fields in webforms are automatically declared as protected.
If I change them to public, they are converted back to protected.

Why is this happening ?

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top