Simple form POST from HTML to ASP.NET page fails - asp.net

Ultimately I have to send form post data from an iPad app to a simple ASP.NET page. Before I do that I just want to get the basic ASP.NET page working by sending a simple form post from an HTML page I have directly to the asp.net page. I post the html form to the asp.net page, and the Request.Form object is always null. I know the page is being hit, because the debugger stops on my breakpoints in the codebehind (.cs).
I know that if I sent the form post from the .aspx page it would work; that's the traditional asp.net form post model. But if the page won't process a post from an arbitrary html page then I believe it will also fail when the post comes from the ipad.
This is puzzling to me. Does ASP.NET somehow discriminate on form posts? Does it somehow know that the post didn't originate from its own aspx, and ignore the post? What is going on and how do I solve this? Thanks in advance.

Yes, ASP.NET does discriminate on form POSTs. If you set up an ASP.NET form normally and then use a tool such as Fiddler to see exactly what is being posted, then you will see all the hidden fields and values that ASP.NET requires for that page. Then, you'll be ready to send data from a non-ASP.NET source.

Related

POST collection type data from silverlight to aspx page.

I have a problem posting the data from silverlight to the aspx page which is in same domain.
I need to open aspx page in a new tab that needs data of type combination (id, amount) like
3-XX-YY-ZZ, 12
4-XX-YY-ZZ, 20
5-XX-YY-ZZ, 15
etc...[many]
and process it and display.
I tried to do it using querystrings and HtmlPage.PopupWindow(). It works but that would come with size limits.
Please help.
I think you need to do a HTTP post with WebClient api. This would help you to post data as a web page will do it.
Here is a link which I found this might be helpful to elaborate next steps...
http://viswaug.wordpress.com/2009/09/17/making-a-http-post-in-silverlight-a-k-a-thread-hopping/
HTH
I can access javascript from silverlight application. And this problem was solved by using javascript function from silverlight to post the hidden field data to aspx page.

How does Asp.net Knows which Page to Generate?

When a client is on page A.aspx , and he press some button there is a postback.
The server knows which page to rebuild according to the request.
but how does the client knows which page to re-ask ? by the current url of his browser ?
where this information is saved in the client side ?
Its defined in the action property of <form>. The client does not need to re-ask, the server sends a response of his request.
ASP.NET is just a part of the .NET framework, but what every client sees on a web browser in plain old HTML.
ASP.NET gives you several controls that makes it easy to use them programatically, so we can set all sort of things in our code (that is run before the page is showing) to do the exactly what we want.
every link, button, image, grid, it's just HTML tags, like <a> for links, <input type="button"> for buttons etc...
Keep in mind that now, there are 2 variantes of the ASP.NET, the WebForms and the MVC (you can also read about choosing one in prole of the other)
in every ASP.NET WebForms there is always a <form> on the start of the <body> and wrapps all your code, so, any submit will do a PostBack into the same file name, in your example A.apsx will always post into A.aspx, then if you want, for example, send that request to B.aspx you need to have a Click Event that would use the Server.Transfer("B.aspx") and that would redirect the entire post to B.aspx just like it was a post from B.aspx
in the newest pattern, the ASP.NET MVC, it drives with Routes witch let's you set up any, every, one, multiple, ways to reach the same page. In MVC the URL does not point to a specific page, but to a specific Controller and it's up to the Controller to send, after processing the data, to a specific View, that is why in MVC there are no pages in the url (though you can add it to the route if you want, and you can accomplish the same with WebForms using a Routing plugin).
Now, in MVC it's there is no <form> wrapping up your entire code, you need to, if you want to submit something, create your own <form> and point to the correct route
but, just like in Webforms or any HTML page, posts are made through form submittion, and it's "path" it's always whats in the form attribute action that let's you know what's the next step.
I hope this helps you realizing that there is no big monster in ASP.NET, that is only a way to reuse controls and access them programmatically and that, in the end, it's all HTML :)
A general answer: on the client side it's either a submit from within a form or a link.
The form points to either a relative URL (that means the current URL plays a key role) or an absolute URL (the current URL plays little to no role).
For links it's generally the same: either they are relative or absolute. One big difference: links are use HTTP GET while forms can use HTTP POST (thus transferring more data without encoding them to the URL as parameters).
For a button it's the form that gets submitted.

Partial Posts in asp.net

I am working on asp.net. and i want to implement partial posts in my application. my situation is like that
i dont want to url changed in address bar and even page should not refreshed at all.
for that i used script manager and update panel but still page refreshes and url also changes.
so any one have idea about it what to do?
Thank you
If you are using Update panel and still your page is getting post back in that case check that EnablePartialRendering should be true. If this is not the case then check your configuration and all the handlers as registered properly for AJAX.
I will suggest you to use jQuery instead of update panel for partial page post back. Do a google and you will find lot of example on this.
Check this ASP.NET postback with jQuery?

Running code/script as a result of a form submission in ASP.NET

An outside vendor did some html work for us, and I'm filling in the actual functionality. I have an issue that I need help with.
He created a simple html page that is opened as a modal pop-up. It contains a form with a few input fields and a submit button. On submitting, an email should be sent using info from the input fields.
I turned his simple html page into a simple aspx page, added runat=server to the form, and added the c# code inside script tags to create and send the email.
It technically works but has a big issue. After the information is submitted and the email is sent, the page (which is supposed to just be a modal pop-up type thing) gets reloaded, but it is now no longer a pop-up. It's reloaded as a standalone page.
So I'm trying to find out if there is a way to get the form to just execute those few lines of c# code on submission without reloading the form. I'm somewhat aware of cgi scripts, but from what I've read, that can be buggy with IIS and all. Plus I'd like to think I could get these few lines of code to run without creating a separate executable.
Any help is greatly appreciated.
If you don't want the page to reload after submission, you will need to use AJAX. that is alot more complicated. you would still need the submit.aspx, you cannot send email with javascript.
Code a redirect after the form submission, so instead of getting the same form back in the main document/page, you could get something like a blank page saying "Thanks for your submission!" or something of that nature.
Might be more simple to redirect the user to a result page using Respone.Redirect that displays some sort of "Your email has been sent" message, or even just redirect back to the base page.

Multiple Forms in ASP.NET

I realize that ASP.NET is only designed to support a single form. What I am confused about is what is the appropriate method for coding multiple "forms" on a .NET page (I have a login form at the top of the page, via the Site.Master and other forms will appear on any given page). Am I supposed to respond differently? It doesn't really make a whole lot of sense. I would appreciate it if someone could clarify. Thanks.
P.S. I am currently developing on .NET 2.0 although I plan to move to .NET 3.5 soon.
Nearly all ASP.NET Web Forms post back to the same page. Unless you are working on some strange outlier (which your question doesn't indicate), you just use one form tag around the entire document, and use event hookups to your controls to handle the various instances.
For example, you can have three buttons that act as Submit buttons, one for each "form" but all inside that same form tag, and each one will call its respective event. This behavior is standard and handled by ASP.NET for you; all you have to do is write the event handlers and wire them up.
I'm not sure what you mean by a logon form. Are you doing some special ajax stuff?
You should be dividing any logical "form" as you call it into its own usercontrol.
http://www.asp101.com/lessons/usercontrols.asp
When dealing with aspx pages, think of the form on the page the same way you think of the body tag.
In that case, I would hand-code the login form at the top to submit to a separate login.aspx page. Leave the rest of the page to the other form's own purpose.
by default you can't, it's how the whole webforms technology works (MVC would probobly fix this in the newest versions).
So really it depends on what you want the "two forms" to do. For example, there's no issue with having login & search on one page, as each button has its own click event and go from there. If you wanted a second aspx page to process one of them, you could collect values and pass them through a response.redirect or some other hacky solution.

Resources