I have one master page and several content pages , I need to put a form tag inside a master page to handle sending data from my html elements and do this for my content pages as well .but I need to understand what is the best way to structure for such this scenario and what would be the effect of form tag of master page on content pages ? is it possible to put form tag in content pages when the master page has this tag inside itself ? I appreciate if I have in detail explanation ?
Form tag inside master page and content page
asp.net
Related Solutions
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.
Have not tried this with refresh, but in general you can add a meta tag like this:
var keywords = new HtmlMeta { Name = "keywords", Content = "one,two,three" };
Header.Controls.Add(keywords);
update: it is possible this way. Check Rick Strahl http://www.west-wind.com/weblog/posts/2006/Aug/04/No-more-Meta-Refresh-Tags
Best Answer
The
<form runat="server">
element lives in the master page by default when you add a new one to your project; all child pages are implemented using ContentPlaceHolders.For example, the master page: -
You can have as many ContentPlaceHolders as you need (often only one is needed though). If you then add a "child page using master page", the page-specific content is added inside the relevant
<asp:Content>
element - these are added by default once you have specified the master page to use when adding a "child page using master page": -Have a read of the MSDN docs for more - http://msdn.microsoft.com/en-us/library/aa581781.aspx