We had a little issue that was driving us nuts. We have checkboxes on an asp.net page that had AutoPostback=true, and just for the first change the initial state of the box wasn't being remembered.
The root cause seems to be that we were changing Enabled on the parent Panel during the OnPreRenderComplete() event handler and that seems to mess up the view state on the initial render for reasons I'm unclear on. Specifically, some of the controls (the checkboxes in this case) don't appear to be in the viewstate so they aren't recognized as changed on the postback.
I thought the actual setting of view state took place after PreRenderComplete, so why would Enabling the parent Panel in that handler not get the view state mechanism working properly?
When I get to SaveStateComplete and look at my controls, I see EnableViewState all the way up, but demonstrably the control state isn't in the bag.
After the first postback, viewstate grows a few K, the child controls are in there and life goes back to normal.
I'd like to understand why changing Panel.Enabled = true in OnPreRenderComplete isn't sufficient to get the child controls into viewstate. If anyone has wisdom to shed I'd appreciate it.
Small repro here:
Checkbox.aspx:
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
CodeFile="Checkbox.aspx.cs" Inherits="CheckboxTest" ValidateRequest="false" %>
<asp:Content ID="Content3" runat="server" ContentPlaceHolderID="OuterMainContentPlaceHolder">
<asp:Panel ID="ItemPanel" Visible="true" Enabled="false" runat="server">
<asp:CheckBox ID="SendEmail" AutoPostBack="true" OnCheckedChanged="OnSendEmailChanged"
Text="Send Email" ToolTip="Whether or not to send email this time" runat="server" />
<asp:TextBox Width="90%" runat="server" ID="BoxChange" />
</asp:Panel>
</asp:Content>
Checkbox.aspx.cs:
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Web.UI;
public partial class CheckboxTest : Page
{
private bool m_PostBackWasEmailChange = false;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
protected override void OnPreRenderComplete(EventArgs e)
{
base.OnPreRenderComplete(e);
ItemPanel.Enabled = true;
}
protected override void OnLoadComplete(EventArgs e)
{
base.OnLoadComplete(e);
if (!IsPostBack)
SendEmail.Checked = true;
else
{
if (m_PostBackWasEmailChange) BoxChange.Text = "Checkbox was turned " + (SendEmail.Checked ? "on" : "off");
}
}
protected override void OnSaveStateComplete(EventArgs e)
{
base.OnSaveStateComplete(e);
}
protected void OnSendEmailChanged(object sender, EventArgs e)
{
m_PostBackWasEmailChange = true;
}
}
Aucun commentaire:
Enregistrer un commentaire