私はaspxページでasp.netとこのコードを使用します:
public partial class Default : System.Web.UI.Page
{
string _Name;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
_Name = "Maikel";
ViewState["Name"] = _Name;
}
}
protected void btnAddName_Click(object sender, EventArgs e)
{
if (ViewState["Name"] == null)
{
txtName.Text = "Empty";
}
else
{
txtName.Text = ViewState["Name"].ToString();
}
}
}
大丈夫です。テキストボックスに「Maikel」と表示します。しかし、このコードを使用すると:
<%@ Page Language="C#" AutoEventWireup="true" **ViewStateMode="Disabled" EnableViewState="true**" CodeBehind="Default.aspx.cs" Inherits="WebApplication3.Default" %>
ViewState["Name"]
ですEmpty
!テキストボックスに「 」を表示しますEmpty
。なぜ?
ViewState
での使用を手伝ってくださいViewStateMode="Disabled" EnableViewState="true"
。
編集:
私はマスターページと (使用マスターページからの web) を使用し、マスターページに次のコードを記述します。
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" ViewStateMode="Disabled" EnableViewState="true" Inherits="WebApplication3.Site1" %>
コードViewState["Name"]
ビハインドページ(使用マスターページからのWeb)のコード、ViewStateは空ではありません!! なぜ?