Recently I used web forms to gather some user information.  Before I had always used pure HTML forms but I wanted to provide validation with asp.net validation handlers so I needed some web controls to do this.

The one problem I ran into is how to get the information from the forms.  In my scenario I have two pages:

  • User_Information.aspx (gathers users email) FORM POST
  • Add_Info.aspx (Checks if user is already in system, emails the user, and posts a response so the user can see what happened)GATHER USER INFORMATION FROM USER_INFORMATION.ASPX

This is the setup(2 pages) I thought would work best.  The only problem is that accessing form data from another page is not so simple and I played around with a lot of ideas.  Upon searching there are many ways to gather posted information from a form (session variables, etc.) Frankly I can’t remember all of them.  Here is one idea I thought might work and was really frustrating me.

        Dim mailrecipient As String
        mailrecipient = PreviousPage.Request.Form("txtEmail")

At first I thought my form post was not even working.  After stepping through the sub routine with the feature in visual studio, I realized it was finding the posted material but somehow it was not grabbing what I needed.  The problem is that when you use a web control asp.net assigns the control it’s own id.  ASP.NET assigned my textbox for email this

“ctl00$ContentPlaceHolder1$txtEmail”

No wonder the above code didn’t work.  I was using the wrong name (txtEmail).  How could I reference the above name?  Does this name change?  Several things bounced through my head and I came up with the following to remedy my problem with the help of some other people’s posts.

        Dim postedValues As NameValueCollection = Request.Form
        Dim nextKey As String
        For i As Integer = 0 To postedValues.AllKeys.Length - 1
            nextKey = postedValues.AllKeys(i)
            If InStr(1, nextKey, "txtEmail") > 0 Then
                mailrecipient = Trim(postedValues(i))
            End If
        Next

This may not look so pretty but it works. What it does is:

  1. postedValues grabs the entire form collection (in my case this is not much because I have only two textboxes but there are other things included in this collection like Viewstate and other things I don’t know too much about)
  2. After the collection is grabbed it goes through the form loop so I can find my control
  3. I use an InStr(in string) function to find the controls name (txtEmail) within that big long ASP.NET generated id (ctl00$ContentPlaceHolder1$txtEmail)

While it is not elegant it does work.  One reason I did it this way is that I am unsure of something.

  • Is the collection always returned in the same order?  If it is then I could grab the whole collection and then just use postedValues(3) or whatever the position(not 3) of the web control I needed was.  (The position is 0 based.)

I ran my code in the receiving page’s(Add_Info.aspx) Page_Load event. Hope it helps someone.



Other Links

ASP.NET Forum Discussion about How to: Pass Values Between ASP.NET Web Pages


Tags: , , , , ,