Posts Tagged codebehind

Passing Variables Between Web Forms and handling HTTP Post

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: , , , , ,

Formview Control: Binding to Datasources (Getting Started With Simple Ways and Codebehind)


The formview control has several ways of binding(linking) data to it. I will briefly describe a couple of ways to do this and list some resources I found helpful.  Getting started using the formview is not tough…. check it out!!!


Simple Way to Add a Formview and Link Data to it

Adding a formview by using the toolbox in Microsoft Visual Studio

Adding a formview by using the toolbox in Microsoft Visual Studio

First, you need to add a formview to your page by dragging and dropping it from the toolbox on the right in Microsoft Visual Studio.  In the toolbox it is in the data section.(yellow circle above) Drag and Drop it on your page….


Here is where you can start to enable paging.

Here you can bind a datasource to the formview.

Next, switch to Design view (Pink Circle Above) in Microsoft Visual Studio.  Then click on the little tab (green arrow above) on the right of your formview.  From there you click on the drop down list next to Choose Data Source: Then a wizard starts up and you fill in all the information about the connection and what data you want…. You then have a formview with all the trimmings.  [Note: If you have trouble with information for the connection wizard...i.e.- connection username, server name, password... try to ask your hosting service]

 


Binding Data to the Formview Using CodeBehind

Here I will show you one method I have used in codebehind to bind data to my formview. This is a LINQ to SQL sample using VB.NET…. I think the other steps would be different but assigning the datasource and the bind method[Lines L7&L8] would be the same regardless of how you connect to your database. Connect to the database and bind!!!

L1: Dim db As New Blog_InfoDataContext
L2: Dim query2 = From BlogEntries In db.Blog_Entries _
L3: Where DateAdd(DateInterval.Hour, 14, Now()) >= BlogEntries.Start_Date _
L4: Select BlogEntries.ID, BlogEntries.Start_Date, BlogEntries.End_Date, _
L4: BlogEntries.Series, BlogEntries.Unit, BlogEntries.Additional_Comments _
L5: Order By Start_Date Descending
L6:
L7: FormView1.DataSource = query2
L8: FormView1.DataBind()

You can run this code in any event like (Page_Load, Etc.)


Other Links

FormView Control: Step by Step – This one seems to cover most of the the basic topics that are associated with the formview control.


Tags: , , , , , , , ,