i use asp.net and this code in aspx page:
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();
}
}
}
its OK. and display “Maikel” in textbox.
But when I use this code:
<%@ Page Language="C#" AutoEventWireup="true" **ViewStateMode="Disabled" EnableViewState="true**" CodeBehind="Default.aspx.cs" Inherits="WebApplication3.Default" %>
ViewState["Name"] is Empty! and display “Empty” in textbox. why?
plese help me for use ViewState with ViewStateMode="Disabled" EnableViewState="true".
Edit:
I use master page and (web from use master page), and write this code in master page:
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" ViewStateMode="Disabled" EnableViewState="true" Inherits="WebApplication3.Site1" %>
and code ViewState["Name"] in code behind page(web from use master page), ViewState is not Empty!! why?
In your case you have disabled the ViewState property for the whole page by setting
ViewStateMode="Disabled"at page level. That is why you are not getting anything in the view state.ASP.NET View State Overview
Control.ViewStateMode Property (MSDN)
EDIT:
To Use ViewState in Page.
You can place all your controls inside a panel, and for that panel you can set the ViewState to false. At Page level enable
ViewStateModeand you will be able to use the ViewState in the code behindFor MasterPage you can disable the ViewState on
ContentPlaceHolderand at Master page level enable the
ViewStateMode