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

Discussion in 'ASP .Net' started by =?Utf-8?B?Sm9oblNtaXRoQG5ld3Nncm91cHMubm9zcGFt?=, Apr 2, 2004.

  1. 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.
     
    =?Utf-8?B?Sm9oblNtaXRoQG5ld3Nncm91cHMubm9zcGFt?=, Apr 2, 2004
    #1
    1. Advertising

  2. 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
     
    Steven Cheng[MSFT], Apr 2, 2004
    #2
    1. Advertising

  3. 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
     
    =?Utf-8?B?Sm9oblNtaXRoQG5ld3Nncm91cHMubm9zcGFt?=, Apr 2, 2004
    #3
  4. 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
     
    Steven Cheng[MSFT], Apr 2, 2004
    #4
  5. I'm still confused on this question

    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#.
     
    =?Utf-8?B?am9obnNtaXRoQG5ld3Nncm91cHMubm9zcGFt?=, Apr 5, 2004
    #5
  6. RE: I'm still confused on this question

    Hi,

    Have you had a chance to check the demo in my last reply or have you got
    any further ideas on this issue? If you have anything else we can help,
    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
     
    Steven Cheng[MSFT], Apr 8, 2004
    #6
  7. RE: I'm still confused on this question

    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
     
    =?Utf-8?B?am9obnNtaXRoQG5ld3Nncm91cHMubm9zcGFt?=, Apr 9, 2004
    #7
  8. RE: I'm still confused on this question

    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
     
    Steven Cheng[MSFT], Apr 10, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sriram Mallajyosula

    How do I post data from aspx page to another aspx page.

    Sriram Mallajyosula, Nov 5, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,012
    Steve C. Orr [MVP, MCSD]
    Nov 5, 2003
  2. DKode
    Replies:
    1
    Views:
    1,891
    S. Justin Gengo
    Nov 12, 2003
  3. Marco Antonio Montalvo Durán
    Replies:
    2
    Views:
    4,180
    Matt Morris
    Jan 23, 2004
  4. Paolo Pignatelli
    Replies:
    6
    Views:
    2,209
    Paolo Pignatelli
    Feb 21, 2005
  5. John M
    Replies:
    1
    Views:
    1,239
    Kumar Reddi
    May 29, 2005
Loading...

Share This Page