Read/Write controls on aspx page from class code (vb.net)

G

Guest

I suspect this is easy, but I have been stumped for a day trying to solve this..

I want to be able to have an unlimited number of aspx pages that all use the code in one class file. I want code in that class file to be able to read and write the controls on the aspx pages. For example, on all the aspx pages, I'll have a label named label1. From code in the class file, I want to be able to do something like label1.text = "hello world

I know about httpcontext, and can now do _context.response.write("hello world"), but I want a general solution that works with labels and other standard server controls, and is generic so that I can add more aspx pages later without having to change the class file that provides the code

Please give a complete example if possible, showing what I need to put in the aspx pages and what I need to put in the class file. There are dozens of posts about this subject on UseNet, but none that assume my limited background knowledge

Thanks

ps - i'm posting this twice... the second time with my MSDN subscriber email, to get a promised 48 hour response... sorry, I didn't have my email assigned earlier.
 
S

Steven Cheng[MSFT]

Hi,

From your description, you'd like to operate a certain aspx page's control
member in a component class. And it need to meet with many pages,yes ?

As for this problem, here is some of my suggestions:
1. If you'd like to retrieve the Page's instance of the current processed
asp.net page. You may use the following code in the component:

public static void ComponentMethod()
{
SimplePage sp = (SimplePage)HttpContext.Current.Handler;
sp.Text1 = "Text Value1";
sp.Text2 = "Text Value2";
sp.Text3 = "Text Value3";
}

#Simple Page is the Codebehind class of the certain aspx page.

If you want to retrieve the page's certain Control member, you need to
change its scope declaration to "public" because by default control member
is defined a s "protected". For example:

protected System.Web.UI.WebControls.TextBox TextBox1;
#change the "protected" to "public"

or you can define a public property for the pageclass to wrapper the
control member, such as
public string Text1
{
get
{
return TextBox1.Text;
}
set
{
TextBox1.Text = value;
}
}

So that you can retrive it and modify it in the certain component as I
mentioned above.

In addition, the means I mentioned above is based on that we know how many
pageclasses we have and write the component's code according to the page
class and you need to define the public properties in the
page classes. If there are many different pageclasses and the component
want to have no infos on those page classes, I'm afraid this way not work.
Do you think so?


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
 
G

Guest

Hi

I don't know any C#. Do you have an example in vb.net

Are you saying that the code in the class file needs to know up front the names of all the aspx pages that may call it? This is not ideal, but not a huge problem. But would it be OK to have 100 aspx pages use the same class code? I have to say, my feeling is that there is some way to make a generic solution, so I can have a million unknown aspx pages all use the same class code. I really, really prefer a generic solution. Remember, these aspx pages will have the same server controls on them, with the same names for each control

Thanks

----- Steven Cheng[MSFT] wrote: ----

Hi

From your description, you'd like to operate a certain aspx page's control
member in a component class. And it need to meet with many pages,yes

As for this problem, here is some of my suggestions
1. If you'd like to retrieve the Page's instance of the current processed
asp.net page. You may use the following code in the component

public static void ComponentMethod(

SimplePage sp = (SimplePage)HttpContext.Current.Handler
sp.Text1 = "Text Value1"
sp.Text2 = "Text Value2"
sp.Text3 = "Text Value3"


#Simple Page is the Codebehind class of the certain aspx page.

If you want to retrieve the page's certain Control member, you need to
change its scope declaration to "public" because by default control member
is defined a s "protected". For example

protected System.Web.UI.WebControls.TextBox TextBox1
#change the "protected" to "public

or you can define a public property for the pageclass to wrapper the
control member, such as
public string Text

ge

return TextBox1.Text

se

TextBox1.Text = value



So that you can retrive it and modify it in the certain component as I
mentioned above.

In addition, the means I mentioned above is based on that we know how many
pageclasses we have and write the component's code according to the page
class and you need to define the public properties in the
page classes. If there are many different pageclasses and the component
want to have no infos on those page classes, I'm afraid this way not work.
Do you think so


Regards

Steven Chen
Microsoft Online Suppor

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

Get Preview at ASP.NET whidbe
http://msdn.microsoft.com/asp.net/whidbey/default.asp
 
S

Steven Cheng[MSFT]

Hi,

Thanks for the followup. Not simply the front the names but the page's
codebehind class. We need to operate a class's instance according to its
class, yes? Also, its ok to have many pages share one page class, but
that'll cause many problem since we can't make so many pages completely
have same features. One ideas is to have a master page(a common page class
for all the pages) so that all the page class derived from the base page
class which contains the common features need to be operated in the common
component. Do you think so?

In addtion, below is certain website which can help translate C# code into
VB.NET. I believe it'll be helpful to you.
http://authors.aspalliance.com/aldotnet/examples/translate.aspx

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
 
G

Guest

Hi Steve

Thank you for trying to help solve my problem

However, I am completely confused by your replies. They are much too complicated for me to understand. For example:

"One ideas is to have a master page(a common page class
for all the pages) so that all the page class derived from the base page
class which contains the common features need to be operated in the common
component.

I have no idea how to "derive from the page base class

What I need is a vb.net example that is complete. That is, I need every line of code in the aspx page, every line of code in the code behind for that aspx page, and every line of code in the class file that I will use to "power" the aspx page and the other aspx pages that will have the same web controls on them, but will have a different page layout and graphics

The example can be very, very simple. Just a 'hello world' example is fine. So, one label on the aspx page. Then, read and write the text value for that label from the class file

I appreciate that I should be able to understand your answers already, but I just don't. I have hundreds of dollars of Wrox asp.net books, and have read them over and over. I have never seen this simple question answered. I am sorry for being such a pain, but I am really, really stumped, and have burned a solid week on what will probably turn out to be a three minute answer for someone who knows the answer

Best Regards, and thank you

PS - VB.NET code please! I don't know C#.
 
G

Guest

Hi Steve

I was able to make use of the sample you created, with one important change. I had to chang

Dim sp As SimplePage = HttpContext.Current.Handler

to

Dim sp As Object = HttpContext.Current.Handle

If I didn't make this change, then I could only use the PageUtil.vb code from the aspx page entitled SimplePage. The whole goal of my original question was to be able to call PageUtil code from any arbitrary aspx page, and read and write control values on such aspx pages from the PageUtil code

I suspect that by subtituting 'Object' for 'SimplePage' that I have changed from early binding to late binding. Is there a way around this? How long does it take to process a late binding vs an early binding? I know it's longer, but it still seems to happen in an instant, so I am inclined to just leave it alone

This is the first time I have used the managed newsgroups, and I must say I am VERY impressed so far. Thank you so much
 
S

Steven Cheng[MSFT]

Hi,

Thanks very much for your response. I feel glad that my suggestion has
helped you. As for the problem you mentiond:
=====================
I was able to make use of the sample you created, with one important
change. I had to change

Dim sp As SimplePage = HttpContext.Current.Handler
to
Dim sp As Object = HttpContext.Current.Handler
=======================

I think this is because you haven't referenced the certain page class( the
"SimplePage" ) in our utility class. Thus, the SimplePage class become
invisible in the utility class(also no intellisense). If we want to use
early binding to access a class's members or interfaces , we need to
reference the class's assemblie in the caller's our caller class's
project. Does this answer or question?

If you still have anything unclear, please feel free to post here. 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
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top