Archive for category Web Forms

Dealing with Multiple Forms (runat=”server”)

I ran into this problem the other day. I have a masterpage with a login on it but I also have other forms that I am displaying in the child pages. How could I get both forms to work correctly?

MasterPage and Child Page with multiple forms

MasterPage and Child Page with multiple forms

The scenario is illustrated above… I have two forms in the masterpage (green) and one form in the child page (pink). The reason this is a problem is that if you put multiple forms on a page you cannot use the runat=”server” tag for all of them. Also when I hit the enter key for login the validation for the email form fires.  Only one form per page can use the runat server tag.. Otherwise you get an error similar to:


A page can have only one server-side Form tag.

Exception Details: System.Web.HttpException: A page can have only one server-side Form tag.


Not really a pleasant sight. When I first came to the realization about this I was a bit frightened. How can we overcome this?

First of all after some reading I found that it is possible to have multiple forms with the runat=”server” tag on the same page. The crux is that only one of the forms can be visible at a time. So the following would work.

I read this in a Microsoft article. Here is a link that describes the situation a little. It talks about why only one form makes sense, etc.


<form runat="server" id="First">Step 1</form>
<form runat="server" id="Second" visible="false">Step 2</form>
<form runat="server" id="Third" visible="false">Step 3</form>

Anyway the above scenario is good in some cases (i.e. – if you want to do a step through wizard). You could easily show the first step then as the user moves through the steps hide the finished step and then show the next step by changing the visible property.

Unfortunately this is not my scenario. I need all my forms to be visible all the time. Here is the simple fix and one that ensures one form (like my login form) will not fire another form (like my email form). Within your controls include a validation group.


<asp:TextBox ID="Password" runat="server" TextMode="Password" ValidationGroup="Login_Member" />

ValidationGroup=”Login_Member” causes my login form to act independently of other form controls on the page. Then you can use one form tag for the whole page and put as many controls in the middle as you want. An example might look like:


<form runat="server">
<asp:TextBox ID="UserName" runat="server" ValidationGroup="Login_Member" />
<asp:TextBox ID="Password" runat="server" TextMode="Password" ValidationGroup="Login_Member" />

<asp:TextBox id="email" runat="server" value="" />
<asp:TextBox id="email_verify" runat="server" value="" />
</form>

I simplified the code above to make it more readable. You would probably have some tables in there somewhere. Only one of my form groups uses the ValidationGroup tag.  (Login) Still I have found that this works ok… Neither form interferes with the other. Perhaps I should use the ValidationGroup tag for both. Experiment with that if you like… the important thing is that I can use each form independently and not hinder the other. Also I only use one form tag with runat=”server” so I don’t get that nasty error.

Tags: , ,

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