Reputation: 3527
I'm kinda new to ASP.NET and I'm try to figure out the proper way of posting data from the client to the server. Currently I have a ASPX page that holds the HTML stuff and I'm thinking of how to post the data from the HTML content to the server. Assuming I would like to do some javascript stuff before posting the data, what is the "best practice" for doing so.
Thanks
Upvotes: 1
Views: 3270
Reputation: 21713
Assuming you have a server-side form already, you just need to handle the onsubmit
event of the form.
<html>
<head>
<script language="javascript">
function handleSubmit()
{
var form = this;
.....
}
</script>
</head>
<body>
<form runat="server" onsubmit="handleSubmit()">
...
</form>
</body>
</html>
handleSubmit()
should be declared in a script
block or external script. If you return false
from this method, it will stop the form being submitted; anything else and it will continue to submit to the server.
Upvotes: 2
Reputation: 628
If you are using ASP.Net controls, you can take a look at viewstate and postback concepts to achieve what you want. But if you are using HTML controls i suggest that you use ajax as opposed to using html form to post data to aspx pages. You can make use of jQuery.ajax() for this purpose.
At this point the question is how to handle the ajax request at the back end? You can do one of the following:
Create one aspx page which reads posted data and returns json responses. You need to set Response.ContentType = "application/json" and write the json using Response.Write(); and call Response.End() in this page. - this is easy but a lame way of doing things.
Create one HTTP handler which reads post data and gives json responses.
Create .asmx web service and use it.
Use Page Web methods. Check http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/
Create WCF REST web services. Check http://www.codeproject.com/Articles/571813/A-Beginners-Tutorial-on-Creating-WCF-REST-Services
Use the new and awesome Web API. Check http://www.asp.net/web-api
Upvotes: 0
Reputation: 3302
ASP .NET uses only one form and the postback is triggered by ASP .NET controls, or alternatively with javascript. Some controls trigger postback by default, some of them need to be setup, for example property AutoPostBack
. That means, there is a layer above HTML and controls do it for you. You just handle events on server side (.cs files). This tries to mimic WinForms.
If you want to submit form, you just place a button and handle its click event server side, from where you can access values of other controls. Therefore there is only one form on a page, so that the process can be automated.
If you're familiar with HTML and are just beginning with ASP .NET, I suggest you to look at ASP .NET MVC, where the things are more straightforward from HTML perspective.
Upvotes: 2
Reputation: 680
just define action in your form tag and set method attribute to post and submit the form like this,
<form id="myForm" runat="server" action="Default.aspx" method="post">
// some fields are here. for instance
<asp:TextBox ID="txtName" runat="server" ></asp:TextBox>
<asp:Button ID="btnSubmit" Text="Submit" runat="server"/>
</form>
when click on submit button a textbox value post to action page "Default.aspx"
and get this value from code behind default.aspx.cs like this
public partial class Default : System.Web.UI.Page
{
string name = Request.Form["txtName"];
}
Upvotes: 0