Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple forms in same .ASPX page 1

Status
Not open for further replies.

Glasgow

IS-IT--Management
Jul 30, 2001
1,669
GB
I have coded a .ASPX file that includes several forms. When the page is loaded, I use the query string to determine which form should be visible and set the visible property on all the other forms to false.

This seemed to work fine when all the code was in-line and I was developing this as a stand-alone file. However, I have now brought the file into a project and split the code to generate a 'Code Behind' file. I get an error when I run the project:

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

I got the same error once or twice when developing the stand-alone file - usually because my logic was such that I'd incorrectly left more than one form visible. Before I start investigating whether my logic is again at fault I'd be interested in knowing if what I'm trying to do is illegal or bad practice in the project(/compiled?) environment. The development environment flags no errors in the code.

I guess I could have a single form and group the controls in different panels then control the visibility by enabling only the appropriate panel if this resolves the problem.
 
Yes, you can only have one form that has the runat="server" tag. You can have multiple forms on the page if you need to, but only one can have this tag and therefore use server side controls.

I'd say that you probably either need to split your functionality out into separate pages if it really is doing different functions, or use panels to show/hide whatever you deem relevant.

Mark,

Darlington Web Design
Experts, Information, Ideas & Knowledge
ASP.NET Tips & Tricks
 
OK thank you Mark. It's interesting that it all seemed to hang together using the stand-alone approach but fails when I do it the "proper" way.

I'll start down the panel route for now.
 
It will still *work* with a code behind approach, you would just have to make sure that only one form exists at any given time (which I think would just make the page difficult to maintain). For example, this will still compile and run:
Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default1.aspx.vb" Inherits="Default1" EnableViewState="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server" visible="false">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
    </form>
    
    <form id="form2" runat="server" visible="true">
    <div>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:Button ID="Button2" runat="server" Text="Button" />
    </div>
    </form>
</body>
</html>
but it's very messy and I would personally always separate this out into two pages.

Mark,

Darlington Web Design
Experts, Information, Ideas & Knowledge
ASP.NET Tips & Tricks
 
Ahah - you helped me pinpoint my problem. I was only explicitly referencing the Visible property at run-time. I had not set it when defining the multiple forms - so I assume they were all defaulting to visible when the form was being loaded, hence the error. I have now set the visible property to false on all forms at design time then explicitly set one to visible at run-time.

Not sure why this did not cause an issue before splitting the code but I'm not too bothered now that problem is resolved.

Ultimately I will probably split all this up as you suggest but at this point I'd like to stick with a single file and it's good to understand what's legal and what isn't.

Thanks again - a big help.
 
Instead of using multiple forms, and setting the visible property to false on the ones you don't want to see, I would use have a page, with only one form, and use a MultiView control in that form.

Take a look in help and on-line for examples on how to use the control. It is very simple to use and I believe that it will do what you are trying to do with the multiple forms.
 
OK I'll bear that option in mind also. Thank you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top