1

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?

Ahmad Rezaye
  • 37
  • 6
  • 11

2 Answers2

4

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

To disable view state by default for an entire page, set the ViewStateMode attribute of the @ Page directive to Disabled.

Control.ViewStateMode Property (MSDN)

To disable view state for a page and to enable it for a specific control on the page, set the EnableViewState property of the page and the control to true, set the ViewStateMode property of the page to Disabled, and set the ViewStateMode property of the control to Enabled.

The ViewStateMode property of a page or a control has an effect only if the EnableViewState property is set to true. If the EnableViewState property is set to false, view state will be turned off even if the ViewStateMode property is set to Enabled.

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 ViewStateMode and you will be able to use the ViewState in the code behind

For MasterPage you can disable the ViewState on ContentPlaceHolder

<asp:ContentPlaceHolder ID="HeadContent" runat="server" EnableViewState="false">
</asp:ContentPlaceHolder>

and at Master page level enable the ViewStateMode

Habib
  • 219,104
  • 29
  • 407
  • 436
  • i have numbers control in page. use ViewStateMode="Disabled" at @page for Disabled ViewState All control in page. but i want use ViewState[] in Code. What's the solution? – Ahmad Rezaye Jan 10 '13 at 09:50
  • @AhmadRezaye, you can place all your controls inside a panel, and for that panel you can set the ViewState to false. At Page level enable ViewStateMode and you will be able to use the ViewState in the code behind – Habib Jan 10 '13 at 09:52
  • @AhmadRezaye, why can't you enclose all your controls inside an asp.Net panel and set the viewstate to false for the panel. That will effect the controls inside it. – Habib Jan 10 '13 at 10:05
  • @AhmadRezaye, check the edit, In Master page you may disable the viewstate for content placeholder and at page level enable the ViewStateMode – Habib Jan 10 '13 at 10:26
1

For EnableViewState="True" ViewStateMode="Disabled" page will not maintain any viewstate i.e ViewState["Name"]

If you need that to be managed, you need to set EnableViewState="True" ViewStateMode="Enabled"

Manoj Purohit
  • 3,413
  • 1
  • 15
  • 17
  • @AhmadRezaye As far as I know, master pages can be thought as a control on Page and disabling their ViewState will have no effect on the page's ViewState. – Manoj Purohit Jan 10 '13 at 10:23