开发者

How to get the asp:CheckBox out of the ViewState? (__ControlsRequirePostBackKey__)

I'm trying to build a simple Asp.Net website that will list some files. Each file need to be checked. So far, so good. I disabled the ViewState in the web.config, but I'm still ending up with a view values there. After decoding it with the ViewStateDecoder 2 app, I've found out that my ViewState looks like this:

<?xml version="1.0" encoding="utf-16"?>
<controlstate>
  <HybridDictionary>
    <DictionaryEntry>
      <String>__ControlsRequirePostBackKey__</String>
      <ArrayList>
        <String>ctl00$ContentPlaceHolder1$DirectoryLister1$ctl01$d__bin</String>
        <String>ctl00$ContentPlaceHolder1$DirectoryLister1$ctl02$d__obj</String>
        <String>ctl00$ContentPlaceHolder1$DirectoryLister1$ctl02$ctl00$ctl00$d__Debug</String>
        <String>ctl00$ContentPlaceHolder1$DirectoryLister1$ctl02$ctl00$ctl00$ctl00$ResGen_read_1_tlog</String>
        <String>ctl00$ContentPlaceHolder1$DirectoryLister1$ctl02$开发者_开发知识库ctl00$ctl00$ctl00$ResGen_write_1_tlog</String>
        <String>ctl00$ContentPlaceHolder1$DirectoryLister1$ctl03$d__Properties</String>
        <String>ctl00$ContentPlaceHolder1$DirectoryLister1$ctl03$ctl00$AssemblyInfo_cs</String>
        <String>ctl00$ContentPlaceHolder1$DirectoryLister1$ctl04$d____UpgradeReport__Files</String>
        <String>ctl00$ContentPlaceHolder1$DirectoryLister1$ctl04$ctl00$UpgradeReport_css</String>
        <String>ctl00$ContentPlaceHolder1$DirectoryLister1$ctl04$ctl00$UpgradeReport_xslt</String>
        <String>ctl00$ContentPlaceHolder1$DirectoryLister1$ctl04$ctl00$UpgradeReport__Minus_gif</String>
        <String>ctl00$ContentPlaceHolder1$DirectoryLister1$ctl04$ctl00$UpgradeReport__Plus_gif</String>
      </ArrayList>
    </DictionaryEntry>
  </HybridDictionary>
</controlstate>

Each checkbox that I'm rendering is in it! How can I get the checkbox out of there? None of the boxes triggers a PostBack. I've tried fileCheckBox.AutoPostBack = false;, but it doesn't solve anything.

So my question is: How can I prevent my checkboxes from registering post back information in the ViewState?

This is the code in my UserControl that generates a CheckBox for each file:

        CheckBox fileCheckBox = new CheckBox();
        fileCheckBox.AutoPostBack = false;
        fileCheckBox.CssClass = "file " + GetExtension(file.Name);
        fileCheckBox.ID = MakeId(file.Name);
        fileCheckBox.Checked = true;
        fileCheckBox.Text = file.Name;
        children.Controls.Add(fileCheckBox);

Disabling the ViewState for the control doesn't work either.

Theory

I suspect the control from doing a Page.RegisterRequiresPostBack or something. Can this be prevented?


CheckBox(also TextBox and DropDownList) do not use only ViewState to persist their values. They implement IPostBackDataHandler to keep their values on PostBack. You have to find Your own solution for this problem, because disabling ViewState won't help You.

See the link for the details.

Nevertheless, to make a control not to use ViewState, You can use EnableViewState="false" for every control which derives from the System.Web.UI.Control.


After fighting quite a lot against Viewstate, I found a solution for the same problem.

I've override the class HtmlInputCheckBox.

Here is the example.

public class CheckBoxFenixHTML : HtmlInputCheckBox
    {

        public CheckBoxFenixHTML()
        {
        }


        protected override void OnPreRender(EventArgs e)
        {
            ViewState.SetItemDirty("checked", false);

        }

    }

This works fine for me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜