POST form from HTML to ASP.NET

B

Brian W

(Jeez did I say that right? :)

This may seem like a stupid question, but I have my reasons for wanting to
do this...

If I have an HTML page with a form tag on it, can I set the action to an
ASP.NET page something like this:

<form action="test.aspx" method="post">
<!--
Some input controls go here...
-->
</form>

How does it get handled in the .ASPX ?

Any examples anywhere?


TIA
Brian W
 
S

Steve C. Orr [MVP, MCSD]

You should be able to get the values using the old ASP syntax such as this:
Dim str as string = Request.Forms("MyControl")
 
S

Steven Cheng[MSFT]

Hi Brain,

From the description, you'd like to have a aspx page be posted to some
other pages by setting
its <form > tag's "action" attribute, yes?

Generally the ASP.NET web page is designed with "post back" model which
make it albe to program as a windows form and it's not recommended to post
the page to another by changing the <form > tag(as what we do in classic
asp) . However, there are still some other means to implement such
functions. We can make the ASP.NET page's
<form > tag as non runat=server so as to make it able to posted to another
page(just as a normal html page). However, using this means will lose the
features of the ASP.NET pages.

In addition to the above means, here are some other approaches:
1. Some some clientside script to change the form's tag's attributes when
the form's submit event is fired. But if the targe page we want to post to
is also an ASP.NET page, we'd consider some issues with the validation and
viewstate checking of the ASP.NET page. Here is a tech article on using
javascript to post a ASP.NET form:

#Post an ASP.NET form with JavaScript
http://www.codeproject.com/aspnet/jsnopostback.asp

2. If the post operation (the datas want to post) can be generate all via
code, you may also consider using the webrequest class to do the post ,
here are some references on using the webrequest class to post to a certain
url:

#Requesting Data
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconrequestingdata.asp
?frame=true

#POSTing Data with ASP.NET
http://authors.aspalliance.com/stevesmith/articles/netscrape2.asp

#How Do I...Make a POST request?
http://samples.gotdotnet.com/quickstart/howto/doc/WebRequests/clientPOST.asp
x

Hope these help.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
B

Brian W

No, I think you misunderstand the question.

I want to go from a static HTML page and have the action post to an ASPX
page

Thanks anyway

Brian W
 
K

Ken Cox [Microsoft MVP]

Hi Brian,

Send the data in your .htm like this:

<form name="myform" action="recv.aspx" method=post>
<input type=text name="thevalue" value="sendthis">
<input type=submit name="submit" value="Submit">
</form>

Parse it in recv.aspx like this:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim loop1 As Integer
Dim arr1() As String
Dim coll As _
System.Collections.Specialized.NameValueCollection
' Load Form variables into NameValueCollection variable.
coll = Request.Form
' Get names of all forms into a string array.
arr1 = coll.AllKeys
For loop1 = 0 To arr1.GetUpperBound(0)
Response.Write _
("Form: " & arr1(loop1) & "<br>")
Next loop1
End Sub
 
S

Steven Cheng[MSFT]

Hi Brian,

Sorry for misunderstanding the problem and thank for your followup. As for
handling the post form a normal html page. I think there is nothing
different, just retrieve the certain html element(such as input field)'s
value via its name attribute
#note via "name" not "id"

for example if we submit a html page like following:
<form action="WebForm1.aspx" method="post">
<table width="100%" align="center">
<tr>
<td>
FirstName:<INPUT id="txtFirstName" type="text" name="txtFirstName">
</td>
</tr>
<tr>
<td>
LastName:<INPUT id="txtLastName" type="text" name="txtLastName">
</td>
</tr>
<tr>
<td>
Email:<INPUT id="txtEmail" type="text" name="txtEmail">
</td>
</tr>
<tr>
<td>
<INPUT id="Submit1" type="submit" value="Submit" name="Submit1">
</td>
</tr>
</table>
</form>


Then, in WebForm1.aspx, we can handled this in Page_Load event as below:

private void Page_Load(object sender, System.EventArgs e)
{
Response.Write("<br>FirstName: " + Request.Form["txtFirstName"]);
Response.Write("<br>LastName: " + Request.Form["txtLastName"]);
Response.Write("<br>Email: " + Request.Form["txtEmail"]);
}

or you can use the foreach loop to loop through all the form datas in the
request:
foreach(string key in Request.Form.Keys )
{
Response.Write("<br>" + key + " : " + Request.Form[key]);
}

Hope helps. Thanks.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top