views:

9323

answers:

10

OK, another road bump in my current project.

I have never had form elements in both my master and content pages, I tend to have all the forms in the content where relevant.

In the current project however, we have a page where they want both. A login form at the top right, and a questions form in the content.

Having tried to get this in, I have run in to the issue of ASP.NET moaning about the need for a single form element in a master page. TBH, I really dont get why this is a requirement on ASP.NET's part, but hey ho.

Does anyone know if/how I can get the master and content pages to contain form elements that work independantly?

If not, can you offer advice on how to proceed to get the desired look/functionality?

A: 

no, you can only have one asp.net form per page. That has been the rule since 1.0

They should both share the same form

WebDude
This is incorrect. You can only have one form with runat="server", not one form. Remember, at the end of all the programmatic stuff, it is still just a web page :)
Rob Cooper
+1  A: 

You can only have one form on an ASP.NET page. One way to handle this is to put an event handler on the login button in the master page. The handler will validate the user and redirect to the same page on success (to correctly run the Page_Load handler, which is run before event handlers).

Dan Goldstein
+3  A: 

the form tag itself is in the MasterPage, as such, you can code any asp.net server controls onto the master page that you wish. And you can write up the processing logic for those server controls on the master page's code behind file.

So, in your example, you can have the login controls on the upper right of the master page, and then have the authentication logic in the code page for the MASTER PAGE, not your content page.

This allows you to have the login controls on every page, and maintain that processing, as well as maintain the content controls and their processing on their individual pages.

Stephen Wrighton
So are you suggesting to wrap the whole page inside a <form runat="server">? Of course it will solve the problem but, isn't it a bad practice?
niaher
Not in ASP.Net Webforms. The standard behavior is that the whole page is wrapped in a form element.
Stephen Wrighton
+1  A: 

Everyone else has already mentioned that you can only have a single form element in a given ASP.NET page, and that it would be contained in the master page. So far, so good. But I don't think that helps you get fully where you want to be ...

In your master pages, you've (I assume!) defined asp:ContentPlaceHolder controls. Your pages which use the master then have corresponding asp:Content tags. All your page content must go in these corresponding asp:Content tags.

Once in that tag, they are part of the master page's form. The master page can respond to events from its own controls, and the pages themselves respond to events from their own controls, and you're set.

If you need the page to interact with the master page, you can access it via the Page.Master property. To interact with any publicly-visible code (methods, properties, etc.) from the master page, you'd cast this property to the correct type, and access the publicly-visible code from there.

That should get you where you need to be in this scenario. (It's worked for me on multiple sites!)

John Rudy
+3  A: 

Hi All,

Thought I would review some of my outstanding questions and see if I can close some of them off.

This one was an interesting one. I outright refused to believe you can only have one form on an ASP.NET page. This to me made no sense. I have seen plenty of webpages that have more than one form on a web page, why should an ASP.NET page be any different?

So, it got me thinking.

Why does a ASP.NET page need a form element?

ASP.NET pages try to emulate the WinForms environment, by provided state persistance through the PostBack model. This provides an element of state to a stateless environment. In order to do this, the runtime needs to be able to have the ability to maintain this state within each "form". It does this by posting back data to itself. It's important to note that:

  • There is nothing really fancy about a PostBack.
  • It uses a HTTP form and POST, the same as any other form, from any other stack.
  • Just because it looks like it might be doing something special, its not, all that happens is it POST's back with some info about what caused it, so you can do things like handle client-side events, in server-side code.

So why only one?

This to me was the million pound question (I am British). I understand that ASP.NET needs this, especially if you are using ASP.NET server controls, but why the hell can't I make my own additional forms?

So, I thought screw it, just make your own form!

And I did. I added a bog-standard, simple form with a submit action of "#". This then performs a POST to the current page, with the Form data for the given form in the request.

Guess what? It all worked fine. So I ended up with:

  • A master page, with a HTML form in
  • This form posts back to the current page (basically the page using the master).
  • In the Page_Load code-behind for the master, I then added code to check the request to see what data was passed in the request. If it contains data (say a hidden field) then I know the post was sourced from the Form on the master page, if not, then it is most liekly a PostBack from content, and can be ignored.
  • I then surrounded the Content tags with <form runat="server" id="aspNetForm"...> </form> tags. This meant that all content pages automatically had a form to work with.

This provided me with a relatively simple, clean solution to my problem. My login form works fine in tandem with all the content forms created, some of which are complex forms, others use lots of server controls and many PostBacks, and so on.

I hope this helps others.

Rob Cooper
+1  A: 

Rob,

Interesting solution. I don't see any problem with what you are doing. The problem some may encounter however, is if they try to do this with 2 server forms. There's no rule in ASP.NET that you can't have more than 1 HTML form on a page, just that you can't have more than one "runat='server'" form on the page. Obviously you've found a pretty easy way of meeting your needs.

I've found that for the most part dealing with a single form is not a problem because the ASP.NET framework basically separates everything for us with naming containers. But in your initial post comment you hit on the important factor that was absent yet critical to the essence of the original question: enter key behavior. That always throws a monkey wrench into the works.

If you were to use a standard "all encompassing" server form, couldn't you capture the right action using a textbox text changed event? Of course, if the user changed both values before hitting enter on either you would get strange behavior. And I think the core problem with the enter key is that once you have more than one submit input on an HTML form, hitting ENTER in a textbox doesn't do anything. Only when there is a single INPUT element does the enter key cause one to be "clicked".

Peter
Hey Peter, you are correct in that enter key behaviour can be "interesting" :D With my particular problem, all works fine since the default submit key for the form will always be "pressed" on Enter. This always seems to work fine on the forms generated, so have not investigated further.
Rob Cooper
I should probably add that the two forms (Login, Main Content) are simple in design. The Login (HTML) has only one submit button, and the content (ASP.NET) can easily have its default action set via properties.
Rob Cooper
A: 

Out of interest, I'm new to .Net and the idea of just having 1 form surrounding all of the content concerns me a little from an accessibility point of view as potentially anyone using a screenreader won't be able to access any of the content contained within the form without first having to swicth out of forms mode (which in some readers, I believe they can only do once it's been through the "form" once)

How does .Net get around this in order to comply with the new legal accessibility standards?

This is a good question, and TBH, one I do not know the answer to. This would be best asked as a question in its own right :)
Rob Cooper
A: 

I solved the "clicking the return key in the login sub-form causes the main form to submit" problem in my current project by embedding an iframe into the master page. The iframe pointed to the login.aspx page which authenticated the user.

<iframe id="login" src="login.aspx" frameborder="0" enableviewstate="false" scrolling="no" runat="server"></iframe>

(form some reason I needed the closing /iframe tag otherwise design view got confused)

Steve Davies
A: 

You can have more than 1 form. (just only 1 visiable at a time) codeline 1 = form 1 visable / form 2 hidden . Code 2 Form 2 visable / form 1 hidden. = solved (this is great for static contact forms as well

ThomaxVision