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 ?