前言:有些asp.net站點,爲了避免ViewState生成的冗餘代碼,會選擇在webconfig中禁用掉ViewState,如下
<configuration> <system.web> <compilation debug="true" targetFramework="4.7.2"/> <httpRuntime targetFramework="4.7.2"/> <pages clientIDMode="Static" enableViewState="false"></pages> </system.web>
那麼,有些依賴ViewState回傳的服務器控件,如CheckBoxList,頁面提交會有獲取不到值得情況,下面給出解決方法
情況1:如果依賴母版頁,母版頁要啓用enableViewState
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="WebApplication11.Site1" EnableViewState="true" %>
子頁面也要啓用
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication11.WebForm2" EnableViewState="true" %>
情況2:子頁面如果不依賴母版頁,直接啓用就可以獲取服務器控件回傳得值
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication11.WebForm1" EnableViewState="true" %>