Legacy html form in ASP .net application - asp.net

I have an html page that I am converting over to an asp .net page. This page contained a form that accesses an external website that I have no control over. There is some sample code below:
<asp:Content ID="sample" ContentPlaceHolderID="body" Runat="Server">
<form name="Subscribe" method="post" action="http://anexternalwebsitehere.com/subscribe.asp">
<input type="text" name="email" size="45" maxlength="120" />
<input type="submit" name="submit" value="Subscribe" />
</form>
</asp:Content>
The form is more complicated than the example I have provided, but it gives a rough idea of what i need to convert over. Here are the problems I have encountered.
If I leave it as is:
When you click on the submit button you have a postback to the current page and not to the external page
If simply convert everything over to be asp form controls and change the postback url:
The id's become some convoluted "ctl00_body_ctl00" which the external page is not able to interpret.
Note: I do need the page to be an aspx page because I am using a master page for other content on the page.
Additional note: this is not Microsoft MVC.
What am I missing?

The issue was with nested forms as others have mentioned.
I was able to fix all my issues by simply doing the following:
Remove the extra form element i was adding.
Leave all controls as simply html controls, except for the submit button.
Replace the submit button with an asp .net button, and set the postback url.
The old code is as follows:
<asp:Content ID="sample" ContentPlaceHolderID="body" Runat="Server">
<form name="Subscribe" method="post" action="http://anexternalwebsitehere.com/subscribe.asp">
<input type="text" name="email" size="45" maxlength="120" />
<input type="submit" name="submit" value="Subscribe" />
</form>
</asp:Content>
The new code:
<asp:Content ID="sample" ContentPlaceHolderID="body" Runat="Server">
<input type="text" name="email" size="45" maxlength="120" />
<input type="submit" name="submit" value="Subscribe" />
<asp:button postbackurl="http://anexternalwebsitehere.com/subscribe.asp" text="Subscribe" runat="server" />
</asp:Content>
This fixes any of the issues with invalid nested forms as there are none. It also addresses the issue of asp .net renaming the asp elements because the only control that is being renamed is the asp button control which was not necessary for the submission to succeed.

Since you probably have the server form tag on your masterpage spanning your contentplaceholder, this new form you're declaring will be placed inside the server-form (by server-form i mean the one asp.net use for postbacks with runat="server")
I've had cases when i needed a special non-server form on an aspx page that already had a server-form, and the way i solved the problem was to place this non-server form outside the server-form - what i mean is, place it after the server-form. Since you use masterpages, you will need a new contentplaceholder on that masterpage, you can call it "noform". It is placed after the server-form so any content put in this noform will be placed outside the server-form. This mean no asp.net controls will work in this specific contentplaceholder (noform) since they won't be picked up by the framework, but you will be able to place your non-server form there and do your magic on that.

The problem, as you've probably guessed, is that you've got one form inside another form - ie the legacy form is appearing inside the ASP.NET form required by the master page.
One quick (if rather clunky) way to get around this is to close the ASP.NET form above the legacy form, and then open a new form below the legacy form. This means you've got three forms on the page, none of which are nested.
So you end up with something like this:
<asp:Content ID="sample" ContentPlaceHolderID="body" Runat="Server">
</form>
<form name="Subscribe" method="post" action="http://anexternalwebsitehere.com/subscribe.asp">
<input type="text" name="email" size="45" maxlength="120" />
<input type="submit" name="submit" value="Subscribe" />
</form>
<form method="post" action="myAspNetPage.aspx">
</asp:Content>
The closing </form> tag at the start closes the ASP.NET from the master page. You then have your form, which should now work as expected. Then the open <form> tag at the end simply ensures that the closing </form> tag from the master page is valid HTML.
Obviously anything appearing on the master page after the legacy form won't be within the standard ASP.NET form, so this may not work for you depending on how the rest of your page is structured.
It's not a particularly elegant solution, but it works as a quick fix (depending on what else is on your master page). We've used it where we had one legacy form required on a site with hundreds of pages, so we simply wanted a one-off fix rather than anything that affected the master page itself.
In our case, we couldn't change the legacy form as this was supplied by a third-party, regularly changed, and needed to be dropped into the ASP.NET page without a developer getting involved to amend it (eg as opposed to Brian's solution to his own question which requires editing the form and is clearly a better option in his case - and probably in most other cases where there is a similar problem).

Your button's click event will handle submission of the url and data.
//C# source
protected void button_Click(object sender, EventArgs e)
}
string customURL = "http://anexternalwebsitehere.com/";
string emailValue = textBoxEmail.Text; //of course validate this for proper email...
customURL += "page.aspx?email=" + emailValue;
Response.Redirect(customURL);
}

Related

ASP.NET WebForms form not being posted - why?

Although I don't consider myself a web programmer, I've done a fair amount of web programming, so I'm almost embarrassed to ask what is wrong with the below code. There is something fundamental about ASP.NET that I must be missing.
I have two pages, source.aspx and destination.aspx:
source.aspx - html:
<body>
<form id="form1" action="destination.aspx" method="post" runat="server">
<input id="Text1" type="text" />
<input id="Text2" type="text" />
<input id="Checkbox1" type="checkbox" />
<input id="Submit1" type="submit" value="submit" />
</form>
</body>
destination.aspx - code behind:
protected void Page_Load(object sender, EventArgs e)
{
// Below variable gets assigned null.
string text1 = Request.Form["Text1"];
}
When I submit the source.aspx form, once it gets to the destination.aspx form, there is no information in the FORM variables. I thought that the forms 'runat="server" ' would ensure that I ended up in the ASP.NET page pipeline, and in fact I can step through this code. There are no POSTed form variables other than viewstate, and the PARAMs collection doesn't have anything corresponding to control data either, not even ones that would correspond to decorated control names. The question is, what is happening that is making my POSTed variables 'disappear', at least to the destination page?
I'm not looking for alternatives how to make this work (i.e. make the controls server controls with runat="server", etc). I can fix the problem. What I am trying to determine is 'what is it about ASP.NET that makes my controls not appear to be receivable by the destination page. Thanks - I thought that I understood HTTP pretty well, but there seems to be a little sleight of hand courtesy of ASP.NET that I'm not seeing.
You can remove the runat="server" off of your form tag since you want to opt out of what ASP.NET gives you with server controls. You are basically thinking correctly that this should work without needing all the ASP.NET page processing bits.
It's a small change you need to make - you need to use 'name' instead of 'id' on your input controls in order for them to appear in the Form collection. It's a subtle thing but I believe it's not exclusive to ASP.NET - the name attribute specifies what to associate the value with in the POST variable collection.
Consult HTML input - name vs. id for more information on id vs. name
Good luck
Request.Form uses name attribute from elements. So you should write name attributes to each of the html elements.
<body>
<form id="form1" action="destination.aspx" method="post" runat="server">
<input id="Text1" name="Text1" type="text" />
<input id="Text2" name="Text2" type="text" />
<input id="Checkbox1" name="Checkbox1" type="checkbox" />
<input id="Submit1" type="submit" value="submit" />
</form>
</body>

How can I create Simple aspx webpage which will send me parameters in the link

I am trying to do something from scratch in asp
I would like to get something like below. From basic hard coded values.
http://localhost:2478/Default.aspx?phone=905123456&order=123456
After that I want to use this parameter in my variables for SQLquery
I am new to asp.net and vb , I'm learning. Can you please explain me in details? Any helps will be appreciate.
Can you please provide any basic code from where I can start
Based on your latest comments:
<form method="get" action="default.aspx">
<input type="text" name="phone" />
<input type="text" name="order" />
<input type="submit" value="submit" />
</form>
Key points:
method=get (fields are in url instead of body)
note that the form above doesn't have runat=server (more below) - it's a plain HTML Form on plain HTML page
in the context of ASP.Net Web forms, you may run into issues particularly if you are using Master Pages (you can't nest forms, and a Master page is a container for your entire page).
ASP.Net forms do POSTbacks - re: method=post
You can use/add plain forms in an ASP.Net page as long as you don't nest them (outside of the server side ASP.Net form).
If you have no choice (e.g. Master Page), you'll have to construct the Querystring manually after a POSTback, then Response.Redirect to some other target (adding the querystring manually).
there are other non-ASP.Net-y ways of doing it - e.g. javascript, but that a different story

Change DNN Form into normal GET form

I'm working on making some changes to a Dot Net Nuke website with a customized skin. I found out that the header to the skins file was located in 'Default.aspx' here.
The form has some very strange behavior. I have had to disable the enter button because pressing within the form causes the webpage to go to "/HOME.aspx" however that action is never specified within the Default.aspx.
The code is as follows.
<dnn:Form id="Form" runat="server" ENCTYPE="multipart/form-data" >
<asp:Label ID="SkinError" runat="server" CssClass="NormalRed" Visible="False"></asp:Label>
<asp:PlaceHolder ID="SkinPlaceHolder" runat="server" />
<input id="ScrollTop" runat="server" name="ScrollTop" type="hidden" />
<input id="__dnnVariable" runat="server" name="__dnnVariable" type="hidden" />
</dnn:Form>
The form after being processed displays in the browser as.
<form name="Form" method="post" action="/HOME.aspx" onsubmit="javascript:return WebForm_OnSubmit();" id="Form" enctype="multipart/form-data">
What I want the code to display as is simply.
<form name="Form" method="get" action="/SearchResults.aspx" id="Form">
I tried removing the dnn code with the html directly but removing the dnn form causes the website to crash.
EDIT
What I'm trying to do can be seen at http://www.ontariosheep.org
Notice if you press the button the search works but pressing enter causes the page to refresh.
You can use some Javascript to do this:
jQuery('#SearchBox').keypress(function(e){
if(e.which == 13){
e.preventDefault();CallSearchPage('http://www.ontariosheep.org/SearchResults.aspx');
}
});
You would need to put that in script tags and also in a jQuery document ready area... like
<script>
jQuery(document).ready(function(){
//code above here
});
</script>
Changing the behavior of the form in DNN is not something you are going to do easily. DNN uses the ASP.NET Web Forms model, so the action for the page is always the current page.
If you want to customize this the only real way is to modify the form action via JavaScript on a specific page, but note that doing that prior to a button click or similar trigger WILL break all administration functions on the page that require a postback to the server.
What are you trying to accomplish?

Cross-page posting. Is it a good pratice to use PreviousPage in Asp.net?

I found this article on MSDN talking about Cross-page posting.
I never worked with Page.PreviousPage. But I think its interesting.
Do you use it? Is it a good pratice, or a bad idea?
What do you think about?
Thanks.
The cross page posting is a helper to post some data to a different page and still have the asp.net code behind functionality.
Why is this exist ? because asp.net have a limitation of one and only form per page. But actually to an html page you can have many forms and many different post to different pages.
So to give a tool to that case, is let you set a second page to post the data, and you setup this on the Button (and not by placing second form), and from there is solve this issue, to post the data to a different page.
For example... with out asp.net and with simple html on a page you can do that.
<body>
<form method="post" action="samepage.html">
Username: <input type="text" name="user" />
<input type="submit" value="Submit" />
</form>
<form method="post" action="page_b.html">
email for news letter: <input type="text" name="email" />
<input type="submit" value="Submit" />
</form>
</body>
To solve a situation like this, and because asp.net not allow two forms at the same page, gives this option.
<body>
<form id="form1" runat="server">
Username: <asp:TextBox runat="server" ID="Name" />
<asp:Button runat="server"/>
email for news letter: <asp:TextBox runat="server" ID="email" />
<asp:Button runat="server" PostBackUrl="page_b.aspx" />
</form>
</body>
In the second case, you have one form, but you set the PostBackUrl to a different page, and from there asp.net still handle the data on code behind direct on a second page (with out redirect).
I hope this example gives you and an idea where to really use the previous page. Also what is more usually is the Redirect, how ever there are case that you need to have the result to a different page. So its per case if you use it or not.

ASP.net post and default page

Scenario:
I have a regular aspx page with a form.
When someone clicks a button the form submitted via post like normal.
HOWEVER. The page where the form resides is the default page(Default.aspx). So when someone goes to the site: http://site.com/ and submits the forms he gets redirected to http://site.com/default.aspx. I tried setting the action of the form to http://site.com/. However asp.net does not allow to use root urls with a POST.
So is there any workaround? Ajax is not an option.
Have you considered creating a plain HTML form? You would need to place your plain HTML form outside the ASP.Net runat=server form on your page.
<form id="form1" runat="server">
ASP.Net controls on your page go here
</form>
<form method="post" action="http://www.site.com">
<input type="text" name="input1" />
<input type="submit" name="input2" value="Submit" />
</form>

Resources