How To Create Nested Form in Asp.net - asp.net

I have a simple website and I use masterPage for designing my template.
everythings work fine, but when I add a Custom (google) Search Box in it my pages correpted.
infact asp does not support Nested Form and as you all know google use a simple form to get queries from the users.
so at first I redesign my site and put 2 Form in it. One server form for my pages content and one other form for google search box. untill here everything work fine .
so I force to add 2 new button beside of my search box and these buttons need a runat=server form, so now I need an approach that let me enable a third form (second runat=server form ) or find an approach to use simple form inside of runat=server form, actually
howcan I put 2 form inside each other or how could we enable a nested form ?

Nested forms won't be possible. You'll need to make those buttons work without being in a runat="server" form.

Cause Asp Forms are not a display control and just accessible in code for programmers,
so I use it in a unregular manner,
as you all know every XML markup like XHTML (asp) have some element (in asp case : control) and each element have its own attribute (in asp case controls properties)
so I just need to put my controls inside the Form (root) Element and cause when page loading on client machine, whenever browser see the server form just change a flag to true (the server form is available) then you can use what ever you have inside of the form,
so if you can lagically put your controls inside of the form , so putit, nothing bad happen )

Related

Can we use multiple forms in a web page?

So far, all the web pages I met contain at most one <form> tag. Why not multiple ones? I can not think of reasons why multiple forms can't coexist within the same web page.
Also, to be specific to ASP.NET - why are all the server controls are placed within the <form> tag? Why not place them somewhere else?
Plus,
I noticed that in an .aspx file, the <form> tag has the runat=server attribute, while a normal server control such as Button also has one. So it seems the <form> is also a server control. But strangely enough, I cannot find it in the Visual Studio Toolbox.
There can be multiple forms, with hacks.
It is indeed a shortcoming of WebForms. In ASP.NET MVC you can implement as many forms as you want (and it is valid & correct behavior of web pages).
The reason all server controls are placed inside <form> tag is to allow the WebForms engine to recognize them, load their values & save their values from/to the ViewState. Almost all infrastructure of control management in WebForms is based on the idea that a tag contains everything you access from the code-behind.
As pointed out, this is one of the shortcomings of WebForms. I do want to point out, additionally, that with cross-page posting and validation groups, you can typically reach your desired behavior (for most "multi-form" solutions).
Regarding the additional question: the <form runat="server"> is parsed as HtmlForm class behind the scenes, which inherits from HtmlControl like any other HTML element with runat="server".
Unlike any other HtmlControl though, there can exist only one instance per page and it does not appear in the toolbox as it's added automatically to every new Form you create, so it's quite pointless.
Yes, it can be done - by creating a custom HtmlForm object and toggling the forms as needed. I've just answered a similar question here (with code):
Paypal Form Ruins My ASP.NET webforms layout -> How to Solve?
many non server forms - you can , but only one runAt Server form
i also found this :
A server-side form tag is the tag which has a runat="server" attribute. If this attribute
is missing, then it's a typical HTML form tag. The conclusion is that you are allowed to use
multiple form tags on a page, as long as only one has the runat="server" attribute. The
disadvantage of the form that doesn't have this attribute, is that view state won't work
(meaning form values will disappear when using the back/forward browser buttons). It's a
small price to pay if you really need multiple forms on a page.
Take master page & set design.
Take one form in master page.
Second form take in contain place holder.
In contain place holder in only for write form tag (not use)
Add aspx page & design second form but not write form tag only for control put
Take button click event fire code write
This is proper way of two form

Two form tags in one .aspx page

I'm having the following problem - I have a master page with an ASP menu control on it which is inherited by every content page of the web site.
The thing is that in (almost) every content page I have a form with runat = "server" and I get a compile error that I can't have 2 form tags with runat = "server" in one page (since I must put the menu control in another form tag).
How should I go about it? I'm doing this as a course project for a university course in C#/ASP.NET and it is said in the asignment that we must use master pages and we must use the asp navigation controls for the site navigation, so I can't use clear html for the menu or drop the master pages...
The first and easiest option is to remove the forms from the actual pages and use a single form for everything. ASP.NET Web Forms is designed to work that way. Since it is a university project this will be fine.
The better way is to use client side (no runat="server" form). You can handle the posts manually in a sort of "PHP fashion" by using the Request.Form object and read values off of it. This will not work if you are required to use the ASP.NET menu controls. So basically you cannot use this approach based on requirements.
P.S. Why is the post tagged with ASP.NET MVC tag? You should not have this problem if you are using ASP.NET MVC. There are other problems though.

ASP.Net Page abstraction

We have a win application that shows a web form in a web browser.
In order to get data from this web form we are using a hidden text box and get its text using HtmlDocument object of web browser control.
I want to make an abstraction of this web form that has this text box element so that other forms can use this abstraction.
I made a web control and put the text box on it.I thought that if I put this control on my page it would have the text box.When i ran my application I noticed that the text box had been rendered but had its control name in its name (WebControl$TextBoxName) and its id(WebControl_TextBoxName) and the win app throw an exception since it couldn't find the element by its id(TextBoxName).
So here's my question:
How can I make an abstract web form/web control that has some elements on it and I can use it to make my final forms have these elements on them? (their names and ids should not be changed)
Thank you for your help
dotNet 4.0 supports static id's so they don't get mangled, read up on Client Id Mode
Alternatively, you could override the render of your control to output a standard html hidden form field with whatever ID you want, and then also add a custom property that will return the textbox that will hide the fact that it isn't an asp.net server control.
Though I've never used the browser control in WinForms, I think what you want to use is a Master Page. Assuming what you're rendering in the browser control is an ASPX page, create a Master Page with the hidden text box that you want to grab your data from, and tell all of the pages you want to have that common control on to use your Master Page. When the page renders, the control id will then be "ctl00_TextBoxName". There is no way of getting around the ID concatenation, since unique IDs are needed and that's the only way to guarantee uniqueness with all the nested control abilities of ASP.NET. However, doing this will guarantee you always have that control named the same on every new form you create that inherits the Master Page. Hope that helps!
In summary (because who reads paragraphs?):
Create Master Page
Place your common control in the Master Page
Have your Form inherit the Master Page
You can read up on how Master Pages work in MSDN's Documentation.

Asp.Net nested form

I need to supply an html form (not a server form) for searching within an asp.net web-forms website. The form will post to another website where an indexed search is performed. Seeing as nested forms don't work well in asp.net, what is the simplest approach for this?
The nested form is a simple html form that performs a "get" against the search website, which I do not have programmatic control over.
Update: I resolved my issue by simply moving the server form to the appropriate place on the page, rather than having it surround the entire page. However, I'm still wondering how this would be handled if the html form needed to be placed physically between server controls (which require the server form).
However, I'm still wondering how this would be handled if the html form needed to be placed physically between server controls (which require the server form).
You can place controls on your page without requiring an HtmlForm.
In your case there's no issue declaring another form markup, but you could also just use some search control on your main form and make it issue a GET to that website.
Not only do nested forms "not work well," you basically can't have >1 form per page at all. The simplest approach is the approach you are forced to go with: write a page that only uses one <form runat="server"></form>. Since you need search functionality, is there no ASP.NET search box control that you could use?
Have a read here.
There are 4 workarounds:
Use an IFRAME
Force Submission to Navigate Using a GET Request
Dynamically Change the Form Action
Use a 3rd Party Form Handler
More details on http://www.revindex.com/Blogs/tabid/65/EntryID/21/Default.aspx
Nested forms don't work well in HTML full stop! You should never do it.
Perhaps you mean more than one form on page? Whilst it's true you can only have one form with runat="server", I can't see any reason why you couldn't have a standard form (not server form) that posted to another site at the same level (ie. not nested).
Try adding your HTML input elements to wherever you want the nested form to be. Then use JQuery to change the page form action to point to the external Website. The entire form is submitted, but with a different external Url. The minor downside is the values for all input elements on the page are posted, but most times that is not big deal.
(I only tried this using a POST, not a GET. This is basically Roman O's #3 workaround in more detail)
<div id="nested-form">
<input type="text" name="q">
<input name="searchbtn" value="Go" type="submit" class="search-button">
</div>
<script>
$(function() {
$("input.search-button").click(function() {
$('form').get(0).setAttribute('action', 'http://external.com');
});
});
</script>
maybe you try Server.Transfer() to your target page that do the search from a button for example!

I've built a ascx control and I would like to be able to keep adding them using Javascript instead of having to do a full call back

I've built a ascx control and I would like to be able to keep adding new instances of it using JavaScript instead of having to do a AJAX callback. Is this possible? I am basically building a web form for a query control and should clause X be filled in, I want to generate a control for the next clause below. I would like to learn how to do this without doing a callback.
Thanks
ASCX are server side user controls and, to my knowledge, can only be loaded by a server event. This can be accomplished through a full page postback or using UpdatePanels and ASP.net AJAX.
If you don't want to use these options and stick with a full JavaScript solution, you're looking at probably doing DOM manipulation and dynamically adding straight HTML.
If the ASCX controls don't change their appearance and all you're doing is showing and hiding them, one last alternative could be to load all of them into DIV tags that have their display style set to none. Then when the user clicks on a checkbox or whatever, you can use JavaScript to show that DIV tag containing the next control. This is how many JavaScript tab setups work.

Resources