Le problème est connu, c'est pas tres compliqué à expliquer mais si tu es debutant, je vais peut etre me passer de l'explication, ca risque de t'embrouiller
mais qd tu travailles en web, tu ne garde pas tes variables au cours de la page, car une fois que la page est compilé, le serveur oublie tout de la page, et meme les variables utilisé.
pour cela l'astuse est la suivante :
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server"> private string MyProperty1 { get { return Session["MyProperty1"].ToString() ;} set { Session["MyProperty1"] = value;} }
private string MyProperty2;
void Button1_Click(object sender, EventArgs e) { MyProperty1 = "Button1"; MyProperty2 = "Button1"; Literal1.Text = MyProperty1; Literal1.Text += "<br/>"; Literal1.Text += MyProperty2;
}
void Button2_Click(object sender, EventArgs e) { Literal1.Text = MyProperty1; Literal1.Text += "<br/>"; Literal1.Text += MyProperty2; } </script>
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" Runat="server" Text="Button" OnClick="Button1_Click" /> <asp:Button ID="Button2" Runat="server" Text="Button" OnClick="Button2_Click" /> <asp:Literal ID="Literal1" Runat="Server" /> </div> </form> </body> </html>
|
Dans cet exemple seul MyProperty1 reste correct alors que MyProperty2 "disparait" et c'est je pense le code que tu as marqué
@+
Cyril