Reputation: 967
The case
I have two web forms, and some codebehind. The first webform is a formular where I enter a string. I want to send that string to the second formular using a post method, and display it.
The problem
I am getting an error message, saying the MAC Viewstate could not be 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 am I 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 your 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 : I have very little experience with web technologies, and I'm trying to learn asp.net and html, so 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 your 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 your first name : <input type="text" name="FirstName"/><br />
<asp:Button PostBackUrl="~/WebForm2.aspx" Text="Validate" runat="server"/>
</div>
</form>
WebForm3.aspx is working fine, and the WebForm1 using an asp:Button is also working, thank you very much.
Additional question :
The purpose of this test was to look for other ways to send data in a "common" way (the post method, since "they" said it is the most secure way). Now, I am only left with one question : what is the best practice, using two files (WebForm1 and WebForm2 method), or a single file (WebForm3 method) ? In other words, should the same page be responsible for gathering data and treating them, or should those responsabilities be split into two files ?
EDIT 2, and last question
When using two files, I see that the IsPostBack property is equal to false. When using a single file, I see that the IsPostBack property is equal to true, when submitting. Is that property only changing (and therefore useful) when using a single file ?
Upvotes: 2
Views: 35955
Reputation: 12341
Webform1.aspx
<form id="form1" runat="server" action="WebForm2.aspx" method="post">
Assuming I understood what you're trying to accomplish - if you want to override the PostBack
mechanism of WebForms, an easy way is to use PostBackUrl
with a Button
control.
It's how ASP.Net WebForms work (postback to same page). There are other ways of POSTing to another WebForms Page, but try the PostBackUrl
first and see if that's all you really need (least amount of resistance).
You can look into ASP.Net MVC or WebPages too, if you want to use more "standardized" way of doing things (like simply wanting to POST to a resource of your choosing) as what I think you want to accomplish.
Hth...
Well, you won't like my answer because "it depends" -
the "common way" in ASP.net WebForms is really same page - the default PostBack mechanism, and then you'd handle display much like you did above - but through controls
(e.g. Placeholder
or user controls
).
you can also look into things like Wizard controls -> useful for those cases where you need to guide a user through Steps
- e.g. perhaps an application form that is lengthy and you want to cut it up into more manageable steps - to cobble this up on your own in a single Page
using the Postback
mechanism, or even multiple Page
s (cross-page Posting) while doable, is probably an exercise in self-punishment :)
there are other ways, but I don't want to confuse and stay on topic (e.g. AJAX).
I'll hold off on "security" because you'll need to be more specific about that - and maybe deserves a separate question. In general the operative rule is "don't trust the client" (meaning validate all data any/all clients provide your application), server-side - not just client-side validation (they should work hand in hand, not mutually exclusive).
Hth...
When using two files, I see that the IsPostBack property is equal to false. When using a single file, I see that the IsPostBack property is equal to true, when submitting. Is that property only changing (and therefore useful) when using a single file ?
IsPostBack
means "Did this POST request come from myself"?
Button.PostBackUrl
-> Page2.aspx = IsPostBack
is false
IsPostBack
is true
Having said that, "only useful" - in this simple example perhaps. But you can use that check for other things - you mentioned security earlier, so you can check whether or not your page can/will/should accept a POST request coming from anywhere.
In your sample case, the target page of the POST (when using 2 pages and PostBackUrl
) should check/validate the POST data it is receiving - because anyone can do the same thing you just did (POST to it).
Upvotes: 5