can I run multiple server side forms in asp .net - asp.net

In my project I want to run 2 forms in one webpage which is master page one is for contact us and one is for signup user. but multiple server side forms are not allowed. is there any solution for fix it?

You can use postback ability of Asp.net Webforms.
You should create one button for singup and another one for contact. You can catch event in server side and you do what want to do.

You can create regular client side forms created using HTML in the master page, and use the Request.Form to read the posted data. For this to work, all the input elements must have a name, and you can read them using Request.Form["ElementName"].
Take into account that forms cannot be nested.
So, your master page needs to llok like this
...
<body>
<form id="form1" runat="server">
</form>
<form id="contact" action="Contact.aspx" method="post">
<input type="text" name="Message"/>
<input type="submit" value="Contact us!"/>
</form>
</body>
The first <form> is the regular ASP.NET server side form, which supports server side components
The second <form> is a custom HTML form. It doesn't have the runat="server" attribute, and it's outside the server side <form>.
The second form action points to a .aspx page. In its page load event you can use the Request.Form["Name"] to acces the name of the contact form. Note that you also need to include a submit button to send the data to the server, and a method="post" to specify the method to send the page.
This is resorting to "basic" HTML controls, so there is no View State, and the posted values will be lost if they're not reset in the server. I.e. if the form is posted, and there's an error, the previously posted values will be lost when rendering the page again.
If you want to make it more sophisticated you can use Javascript and, optionally AJAX. But that's a more complex solution. See this for an example.

ASP.NET Forms only supports one form. You could use an iFrame and render your second page (second form) inside of the iFrame.
Here is an interesting post about placing some page content after the tag. This may be helpful to you.
http://forums.asp.net/t/1611822.aspx?Dynamically+adding+content+to+page+outside+of+the+aspnet+form+from+within+a+UC

Related

using classic asp code inside asp.net?

I have a application that running both classic asp and asp net. inside the classic asp page, I have a combo box and depends on the selection of the combo box I need to do something inside my asp.net page. For instance, if inside my classic asp page, I have a combo box and inside the combo box; book is selected than when I enter a price for book as a zero inside my asp.net page I supposed to get an alert. Is there any way to do that?
asp.net code
if (Convert.ToDecimal(Values["myBookPrice"]) == 0)
{
//You cannot use 0 price for books!
}
Let say that you have some input control on any page, html, asp what ever, with some parametre that you wish to pass to an asp.net page
if the input control have a name attribute like
<form method="post" action="thenewpage.aspx">
<input name="nameOfInput" type="text" id="idofinput" />
<input type="submit" name="btnGo" value="Create Box" id="btnGo" />
</form>
and you have a button that make post back to an asp.net page, then you get that parametre on asp.net side using the Request.Form and the name of the input control as:
Request.Form["nameOfInput"]
If you make a call the page with url parameter, you need to use some kind of javascript to dynamically create the URL string for the call.
The issue that you have here, is that the asp.net page will hit the validation of the page security because is not going to find the page validation informations he needed to secure the page.

aspx: a form is always forwarded to the same page

on the page products.aspx i created a form:
<form id="send_info_form" method="post" action="send_email.aspx">
<input type="text" name="the_name />
<input type="submit" />
</form>
when i click on submit it's forwarded to the same page (products.aspx) and not to the page i set in action attribute of the form.
It looks like you have a misunderstanding about how ASP.NET's logic works- ASP.NET has a much different paradigm than PHP or ASP does.
It seems like you're taking more of an ASP classic or PHP approach of directly handling the landing pages of forms and POST values, which you no longer have to do. You shouldn't need a separate page to handle the logic of the form submission either; this is all handled by event handlers in the submitting page's codebehind.
Instead of handling input elements directly, you should use ASP.NET's server controls to handle all the inputs for you.
What you should be doing is:
In the Products.aspx page:
E-mail Address: <asp:TextBox runat="server" ID="txtEmail" />
<asp:Button runat="server" ID="btnSubmit" OnClick="btnSubmit_Click" Text="Submit" />
Note that there's no form tag required (besides the one already provided to you when you first make the ASPX page.
Since you're working with an object-oriented language with a business objects representing all of your HTML elements with ASP.NET, you don't have to handle reading from the POST values of the form directly.
In the codebehind for Products.aspx (I'm assuming C#, so Products.aspx.cs), add a method for btnSubmit_Click:
protected void btnSubmit_Click(object sender, EventArgs e) {
string sendEmailTo = txtEmail.Text;
// insert mail sending logic here
}
In ASP.NET, the tag will by default always post itself to the same page. This allows you to handle any events in the codebehind in the ASPX page. It's not clear from your question what exactly you're trying to do. I can think of three possible scenarios:
1) You want to post back to the same page, and toggle visibility of UI elements (other panels, etc.) based on the result of the processing, or redirect the user to a second destination page once processing is complete. This is the most common scenario and the approach I recommend be taken, because it keeps all the logic regarding the processing of the form in one place.
2) You can specify a PostBackUrl to specify another (ASP.NET) page for button controls and whatnot. From there you can do the processing from elements on the first page on the second page using the PreviousPage property. See http://msdn.microsoft.com/en-us/library/ms178139.aspx for more information.
3) If you have a separate page you wish to post to that you don't control that's not ASP.NET-based (i.e., another site's form, or a PHP/ASP3.0 site you run), things get significantly more difficult. ASP.NET puts everything in one giant elements. Since tags cannot reliably be embedded within each other in HTML, you will either have to do a silent POST manually from your codebehind, or use Javascript to quietly submit an ajax request upon submission.

Post a form from asp to asp.Net

I have a classic asp application. I want to post a contest form from that page to an Asp.Net form. The reason is that I want to use a lot of logic i have built into an Asp.Net page for validation before entering into the database and I don't know asp very well. Not to mention asp.Net being more secure.
What's the best way to accomplish this goal? My thoughts are as follows:
My asp Page:
<html>
<body>
<form action="/Contests/entry.aspx" method="post">
Name: <input type="text" name="fname" size="20" />
Last Name: <input type="text" name="lname" size="20" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
aspx page is running in a Virtual Directory and would handle anything posted to it.
Is this possible, or does aspx prevent this kind of thing?
I ( preferably ) don't want to create the form in aspx as my colleague wants to have control of the page and build the html himself and I don't want the hassle of constantly changing it.
Are there caveats I need to consider?
What roadblocks will I run into?
How do I access the Posted Form Values? Request.Form?
Yes it is possible. In general, a POST is a POST. So you can post from a PHP page to a .NET page if you wanted. You would access the Request.Form variables just as you do now. You will have to look at the ASP Classic page to see the names of the post items but in general, you can access them as if you had pasted from .NET page.
This can be done and works fine. You will access the Posted Form values as you said via Request.Form.
I think the biggest caveat is that you will need to handle invalid data in some way - typically with a webform the .aspx page would be displayed again with validation errors, but that would likely be inappropriate for your circumstance. Probably you will need to redirect them back to the .asp page with query string parameters indicating the failures and the page will need code allowing it to fill in the form fields with their previous values and display the error message.
How about calling an ASP.NET webservice from classic asp?
https://web.archive.org/web/20210125161040/http://www.4guysfromrolla.com/webtech/070302-1.shtml

Handling elements generated by Javascript in Asp.Net

I have several elements in my web page generated by JavaScript.
Here's an example of such content:
<input id="uploadfile01" type="file" onchange="change(1);" />
<input id="uploadfile02" type="file" onchange="change(2);" />
My question is:
How can I interact with these elements in the server side, (Asp.net) after a post?
(Since the elements were dynamically generated they do not exist in the original asp.net page)
I dont think that would be possible.....if i am not mistaken you want to give the option to attach multiple files. i think it would be better to place a set number of fileupload controls and set there display to none and you can use javascript to show them.
i know that this is not what you are looking for but after a postback the controls will be lost and even if they are added again they will lose there contents.(had you been generating them through code behind)

Form Elements in ASP.NET Master Pages and Content Pages

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?
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.
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.
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!)
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".
None of the previous answers gave a code example. Here's a simplified version of the Visual Studio 2012 Site.Master that illustrates how to do this:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site - Copy.Master.cs" Inherits="WebApplication1.Site1Master" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title>This is a title</title>
<asp:ContentPlaceHolder runat="server" ID="HeadContent" />
</head>
<body>
<form runat="server">
<header>
<div class="content-wrapper">
<div class="float-right">
<section id="login">
<asp:LoginView runat="server" ViewStateMode="Disabled">
<AnonymousTemplate>
<asp:ContentPlaceHolder runat="server" ID="AnonContent" />
</AnonymousTemplate>
<LoggedInTemplate>
<asp:ContentPlaceHolder runat="server" ID="LoggedInContent" />
</LoggedInTemplate>
</asp:LoginView>
</section>
</div>
</div>
</header>
<div id="body">
<asp:ContentPlaceHolder runat="server" ID="FeaturedContent" />
<section class="content-wrapper main-content clear-fix">
<asp:ContentPlaceHolder runat="server" ID="MainContent" />
</section>
</div>
</form>
</body>
</html>
So you have everything wrapped by a single Form element, so you can place controls in the master page, yet your content pages can also use controls.
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).
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)
You can access MasterPage controls from the aspx form by:
add the detractive tag to the aspx form <%# MasterType VirtualPath="~/Site.Master %>
and in the code behind use Master.FindControl(); to get the control by ID
for Example if you want to get the
Control form = Master.FindControl("form")
now you can use the master page's form in your code.
I hope this help.
Salve! In a similar thread, I posted an answer that might help you. You can use jquery to add content to an empty div. That content can include form tags, and even a submit function independant of anything the server-side code is doing. The only downside to this is if the user does not have javascript enabled!
Instead of reposting the same answer (and the code too), here is the link:
Jquery Ajax loading form on asp.net webform
This is a limitation of ASP.NET
ASP.NET is designed to have one form per page and only one form. When it was originally designed that was not a problem.
However since then this has been identified as a huge problem with accessibility.
Microsoft Fix for this was ASP.NET MVC, if you are able to I would suggest considering moving to ASP.NET MVC as it solves a large number of problems with ASP.NET
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
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

Resources