Reference Page Controls From Class

  • Thread starter Thread starter Alphonse Giambrone
  • Start date Start date
A

Alphonse Giambrone

How can I reference a page or user control's properties (such as viewstate)
and controls from another class?

TIA
 
You have to inherit the control to access things like the ViewState. For
example you want to extend the page class you would do:

public class ExtendedPage : System.Web.UI.Page


I'm not sure if that's what you're asking but that's what I understood from
your question
 
Hi Alphonse,

From your description, you means how to reference a page or user control
from another class, does the class you mean a utilitiy class which want to
be isolated with the web related infos?

If so, I think there are two means you can choose:
1. Define a function in the certain class which contains a param the type
of which is "Page" or "UserControl" then, you can reference the certain
Page or UserControl's members: For example:
public class ...
public void processPage(Page page)
{
page.XXX. = xxxx;
}

public void processUserControl(UserControl uc)
{
uc.xxxx = xxx;
}


2. In ASP.NET web applicatoin the HttpContext.Current static member provide
the reference to the current processed Request's Context and if the
request's handler is a Page handler(or other classes derived from
System.Web.UI.Page), we can use the HttpContext.Current.Handler to get the
Current Page's reference ,such as:

public void processPage()
{
Page page = (Page)HttpContext.Current.Handler;
page.XXX = xxx;
}

In addtion, here are some former threads discussing on the similar
questions:

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=I5$opc
HGEHA.616%40cpmsftngxa06.phx.gbl&rnum=1&prev=/groups%3Fhl%3Den%26lr%3D%26ie%
3DUTF-8%26oe%3DUTF-8%26q%3Dasp.net%2Bpage%2Bfrom%2Bclass%2B%2Bsteven%2Bcheng

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=wAVBte%2
4CEHA.564%40cpmsftngxa06.phx.gbl&rnum=13&prev=/groups%3Fq%3D%2Bpage%2Bmember
%2Bsteven%2Bcheng%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26start%3D10%2
6sa%3DN

Hope also helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Thanks both for the replies.

It looks like you have nailed it for me Steven.

I should have mentioned that I am working in VB.NET. While I am familiar
with some of the c# syntax, I don't quite understand the following line:
Page page = (Page)HttpContext.Current.Handler;
What would be the equivalent in VB?

Also, is there any difference in performance using method 1 vs. method 2?

--

Alphonse Giambrone
Email: a-giam at customdatasolutions dot us
 
Hi Alphonse,

I'm sorry for mistaken the langugage, here is the VB.NET version:
#1
Public Sub processPage(ByVal page As Page)
page.XXX = XXX
End Sub

#2
Public Sub processPage()
Dim page As Page = CType(HttpContext.Current.Handler, Page)
' in fact in VB.NET the following code is also ok
'Dim page As Page = HttpContext.Current.Handler
page.XXX = XXX;

End Sub

As my own oponion, both is ok and the I prefer the #1 better because it is
more convenient and more understandable. No critical concerns on
performacne both. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Back
Top