ViewState Sysptoms

SYMPTOMS

The following server controls persist their information across requests even when the control ViewState (the EnableViewState attribute) is set to False :

  • The TextBox control.
  • The CheckBox control.
  • The RadioButton control.

CAUSE

This behavior occurs because the ViewState of a control is only one of the methods that are used to persist a control's attributes across requests. In the server controls that are mentioned in the "Symptoms" section, attributes that are not normally posted to the server through the form-get or the form-post are handled by the ViewState. These values include attributes of the control, such as BackColor. Attributes that are normally posted to the server are handled by the IPostBackDataHandler interface. An example of such an attribute is the checked attribute of the CheckBox control.

 

This behavior is by design.

Steps to Reproduce the Behavior

  1. Create an .aspx page with a Web Forms CheckBox control(ID=CheckBox1) and a Button control.
  2. Add the following Microsoft Visual C# code for the Page_Load event:
    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!IsPostBack)
    {
    CheckBox1.BackColor = Color.FromName("Tomato");
    }
    }
  3. Verify that the EnableViewState property for the CheckBox control is set to True .
  4. Save and compile your project, and then browse to the .aspx page.

    Even though the background color for the CheckBox control is set when you first request the .aspx page, it is maintained across requests when you submit the form with the button.
  5. Set the EnableViewState property of the Checkbox control to False .
  6. Save and compile your project, and then browse to the .aspx page with a new instance of the browser.
  7. Click to select the check box for the checked attribute. When you resubmit the .aspx page with the button, the background color of the Checkbox control is lost. However, the checked state of the control is maintained.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值