c# - ASP.NET how to send data with post method? -
the case
i have 2 web forms, , codebehind. first webform formular enter string. want send string second formular using post method, , display it.
the problem
i getting error message, saying mac viewstate not validated :
erreur du serveur dans l'application '/'. Échec de la validation mac viewstate. si cette application est hébergée par une batterie de serveurs ou un cluster, assurez-vous que la configuration spécifie le même validationkey et le même algorithme de validation. autogenerate ne peut pas être utilisée dans un cluster.
what doing wrong ?
webform1.aspx
<%@ page language="c#" autoeventwireup="true" codebehind="webform1.aspx.cs" inherits="webapplication1.webform1" %> <!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title></title> </head> <body> <form id="form1" runat="server" action="webform2.aspx" method="post"> <div> enter first name : <input type="text" name="firstname"/><br /> <input type="submit" /> </div> </form> </body> </html>
webform2.aspx
<%@ page language="c#" autoeventwireup="true" codebehind="webform2.aspx.cs" inherits="webapplication1.webform2" %> <!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title></title> </head> <body> <form id="form1" runat="server"> <div> first name : <%= firstname %> </div> </form> </body> </html>
webform2.aspx.cs
public partial class webform2 : system.web.ui.page { public string firstname { get; private set; } protected void page_load(object sender, eventargs e) { firstname = request.form["firstname"]; } }
note : have little experience web technologies, , i'm trying learn asp.net , html, please forgive me.
edit 1 :
webform3.aspx
<%@ page language="c#" autoeventwireup="true" codebehind="webform3.aspx.cs" inherits="webapplication1.webform3" %> <!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title></title> </head> <body> <form id="form1" runat="server"> <%if (!ispostback){ %> <div> enter first name : <input type="text" name="firstname" /><br /> <input type="submit" /> </div> <% } else { %> <div> first name : <%= firstname %> </div> <% } %> </form> </body> </html>
webform3.aspx.cs
public partial class webform3 : system.web.ui.page { public string firstname { get; private set; } protected void page_load(object sender, eventargs e) { if (ispostback) { firstname = request.form["firstname"]; } } }
webform1.aspx (revised)
<form id="form1" runat="server" method="post"> <div> enter first name : <input type="text" name="firstname"/><br /> <asp:button postbackurl="~/webform2.aspx" text="validate" runat="server"/> </div> </form>
webform3.aspx working fine, , webform1 using asp:button working, thank much.
additional question :
the purpose of test other ways send data in "common" way (the post method, since "they" said secure way). now, left 1 question : best practice, using 2 files (webform1 , webform2 method), or single file (webform3 method) ? in other words, should same page responsible gathering data , treating them, or should responsabilities split 2 files ?
edit 2, , last question
when using 2 files, see ispostback property equal false. when using single file, see ispostback property equal true, when submitting. property changing (and therefore useful) when using single file ?
webform1.aspx
<form id="form1" runat="server" action="webform2.aspx" method="post">
assuming understood you're trying accomplish - if want override postback
mechanism of webforms, easy way use postbackurl
button
control.
it's how asp.net webforms work (postback same page). there other ways of posting webforms page, try postbackurl
first , see if that's need (least amount of resistance).
you can asp.net mvc or webpages too, if want use more "standardized" way of doing things (like wanting post resource of choosing) think want accomplish.
hth...
update
well, won't answer because "it depends" -
the "common way" in asp.net webforms same page - default postback mechanism, , you'd handle display did above - through
controls
(e.g.placeholder
oruser controls
).you can things wizard controls -> useful cases need guide user through
steps
- e.g. perhaps application form lengthy , want cut more manageable steps - cobble on own in singlepage
usingpostback
mechanism, or multiplepage
s (cross-page posting) while doable, exercise in self-punishment :)there other ways, don't want confuse , stay on topic (e.g. ajax).
i'll hold off on "security" because you'll need more specific - , maybe deserves separate question. in general operative rule "don't trust client" (meaning validate data any/all clients provide application), server-side - not client-side validation (they should work hand in hand, not mutually exclusive).
hth...
update 3
when using 2 files, see ispostback property equal false. when using single file, see ispostback property equal true, when submitting. property changing (and therefore useful) when using single file ?
ispostback
means "did post request come myself"?
- page1.aspx ->
button.postbackurl
-> page2.aspx =ispostback
false
- page1.aspx -> button/control triggers postback =
ispostback
true
having said that, "only useful" - in simple example perhaps. can use check other things - mentioned security earlier, can check whether or not page can/will/should accept post request coming anywhere.
in sample case, target page of post (when using 2 pages , postbackurl
) should check/validate post data receiving - because anyone can same thing did (post it).
Comments
Post a Comment