Referencing a form element on a PreviousPage with ASP.NET in Visual Studio .NET 2005 Whidbey and Thanks to Keith Smith

Referencing a form element on a previous page with ASP.NET Whidbey is a little more complicated than I had initially thought.  Its not horrible complicated either, but there are some tricks that I was not aware of.

1.  Form elements are private to the partial class that they are contained in.  In retrospect, that makes sense, however, I did not know that at the time.  So, to be able to reference the form element, you need to add a public property to the partial class that exposes the field.  In my example, I had

    public System.Web.UI.WebControls.TextBox TextSearchTextBox
    {
        get { return txtSearch; } //txtSearch is the from element that I am going to reference in another page.
    }

2.  In the codebehind/codebeside file that will reference the results, define a a variable of the appropiate type.  In my case, it was

  TextBox tbInput = null;

3.  To fill the TextBox

        if (null != PreviousPage && PreviousPage is Default_aspx)  //Default_aspx is the name of the class file from the page that posted to the current one.
        {
            tbInput = (TextBox)((PreviousPage as Default_aspx).TextSearchTextBox);
        }

This should get things going for you.

Many thanks to Keith Smith at Microsoft for his assistance on this and for not laughing at my questions too much.

Wally

1 Comment

  • Am I the only one who liked the CodeBehind architecture as it was in 1.x and is rather nervous about the coming changes?



    I'm certainly not at all confident of any automatic wizard's ability to convert all the subtleties of my CodeBehind-based code into a working CodeBeside-based version. This example is a case in point. Although 1.x didn't have a PreviousPage property, an analagous issue came up if you were trying to reference form elements of the Page from a user control, or vice versa. I frequently upped the accessibility of the form element variables from protected to public to make them accessible in this way. Now that those variables don't show up in the codebeside at all, what's a wizard going to do with them? I'll have to create a new property, but I'll also have to give it a new name because you can't have a property with the same name as a variable. And then all my code that references that variable needs to be changed to use the new name.



    I'm really, really uncomfortable with this huge architectural change being forced on us with no backward-compatible option. Why has there been no outcry over this?

Comments have been disabled for this content.