ASP.Net using multiple form tag and post them to another page - asp.net

I don't know it's even possible but I try to achieve to post data from one page to another using the second form.
The problem is I need a form tag for the user interface containing callback panels etc. I want to put a second form with some hidden-field:
<form id="postForm" method="post" action="target.aspx">
<input type="hidden" id="id" />
</form>
I submit the form from javascript with jquery:
$("#id").val(id);
$("#postForm").submit();
Is there a way access the value of the hidden field on the target page?

You should give the hidden field a name attribute:
<input type="hidden" id="id" name="id" />
You'll then be able to use Request.Form in your target page to access the posted data:
string postedId = Request.Form["id"];

Related

Is there a good way to pass both form information and URL parameters to server?

In my web forms project (a forum) I have a form that is accessed through a link from another page, a link which includes parameters. for example:
EditPost.aspx?mid=0
When the page is loaded, the form hasn't yet been submitted and Request.QueryString contains the URL parameters I loaded the page with. However, after I send the form, Request.QueryString conatins the values of the form fields. This is a problem because I need the URL parameters after the form is sent.
The question is, how do i make the page send me those URL parameter with the form information?
If I'm approaching this wrong, and I'm supposed to keep the URL parameters from when the page is first loaded, let me know how.
Thanks!
Edit:
<form action="" id="f_editComment" name="f_editThread" method="post" onsubmit="return true;">
<%=ThreadTitle %>
<textarea id="body" name="body" runat="server"></textarea>
<input type="submit" id="submit" name="submit" />
</form>

What happens to an "input" on "submit"? And how can that be done by ASP.Net? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Read Post Data submitted to ASP.Net Form
I have a google checkout "buy now" button, and am trying to add dynamically created content to send when it's clicked. Using the original html is proving a bit difficult so I want to create an ASP.Net ImageButton Instead of that.
I've succeeded in creating the button with the right image, and hooking it up to an event handler in the codebehind.
However, I'm not sure what exactly happens when the original button is clicked, in order to try and emulate it in the new ImageButton.
The original code is:
<form action="https://sandbox.google.com/checkout/..." id="Form1" method="post" name="..." target="_top">
<input name="item_name_1" type="hidden" value="..." />
...
<input alt="" src="https://sandbox.google.com/checkout/buttons/buy.gif?merchant_id=..." type="image" />
</form>
And I want to place a dynamically created item_name_1.
What do I have to do in the Button1_Click method for that?
The short, concise and usefull version:
Html:
<form id="__parent" action="..." method="post" runat="server">
<input id="__child0" name="type" type="hidden" value="button" runat="server" />
<input id="__child1" name="name" type="hidden" value="teh_button" runat="server" />
<input id="__child2" name="value" type="hidden" value="Hello?" runat="server" />
</form>
tehfile.cs:
<%# Page Language="C#" CodeFile="tehfile.cs" %>
String
_type = __child0.Value,
_name = __child1.Value,
_value = __child2.Value,
_element = String.Format(
"<{0} {1}=\"{2}\" {3}=\"{4}\" {5}=\"{6}\" />",
"input",
"type", _type,
"name", _name,
"value", _value );
Literal _lit = new Literal( );
_lit.Text = _element;
__parent.AddControl( _lit );
To post that data to another server on the ASP.NET server-side, you are going to need to use something like the WebRequest class.
Or also in order to post a form, you can use a remote post class like any of the ones here: Remote HTTP Post with C# , the answer by #BobbyShaftoe is the one i've used in many projects.
Same question/POST here. I would have commented instead of answered, but seems this is a better/formatted way to get to bottom of it all:
In your comment to #MarcusHansson's answer:
I fail to see how this addresses the question of how to use the codebehind to send the information
You are mixing server side with client side submission methods.
If you want to submit using "code behind" you must implement server-to-server HTTP Post. In the context of Google Checkout, I've provided that link in your other post.
Your client side is using an HTML FORM which in, and of itself is how you "send the data". You can try all sorts of client-side submission processes, but at the end of the day, it is a client-side (Javascript) method.
What is "dynamic" about your buy now button? It's meant for a single item (at a time) purchase. Why can't you construct all the variables you need at the same time you create the button? What are you adding (that requires another redirect or postback)?

can a forms action url contain querystring values? [duplicate]

This question already has answers here:
What happens if the action field in a <form> has parameters?
(4 answers)
Closed 2 years ago.
can a forms action url contain querystring values?
Yes
it can.
But
when method="get" then the querystring will be stripped out and replaced by the form input names/values (since the form controls are those that build the GET querystring).
<form method="get" action="?param=foo">
<input type="hidden" name="param" value="bar" />
</form>
will submit param=bar
To keep the value you should specify method="post" on the form.
<form method="post" action="?param=foo">
<input type="hidden" name="otherparam" value="bar" />
</form>
will submit param=foo&otherparam=bar
<form method="post" action="?param=foo">
<input type="hidden" name="param" value="bar" />
</form>
will submit param=foo&param=bar (so, depending on how you process the request, you could get either an array value or unexpected results).
Yes, it can.
(Keystrokes)
I've just checked using a reduced test case:
Form.htm that contains a form with an action of default.aspx?query=1 and a submit button.
default.aspx that contains code in Page_Load to write out Request.QueryString["query"]
The result I got when clicking on the button was a page that read:
1
So, the answer is yes.

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>

Legacy html form in ASP .net application

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);
}

Resources