加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 我是否需要viewstate来输入控件,如复选框,文本框等?

发布时间:2020-12-16 07:18:28 所属栏目:asp.Net 来源:网络整理
导读:当EnableViewstate = false并且我进行回发时,我的文本框控件保留文本值,但标签控件不保留.我正在更改按钮的单击事件上的标签和文本框的文本. protected void Button1_Click(object sender,EventArgs e){ TextBox1.Text = "Message Changed"; Label1.Text = "
当EnableViewstate = false并且我进行回发时,我的文本框控件保留文本值,但标签控件不保留.我正在更改按钮的单击事件上的标签和文本框的文本.

protected void Button1_Click(object sender,EventArgs e)
{
    TextBox1.Text = "Message Changed";
    Label1.Text = "Message Changed";
}

我的页面还有另一个按钮用于回发.

此外,如果我不需要viewstate用于此控件,那么是否有一个不需要viewstate的所有控件的列表?

解决方法

即使ViewState处于关闭状态,实现 IPostBackDataHandler的所有控件都会加载其值.请注意,禁用ViewState时,TextChanged等事件和ForeColor等其他属性不起作用.

这是一个实现IPostBackDataHandler的控件列表

> CheckBox
> CheckBoxList
> DropDownList
> HtmlInputCheckBox
> HtmlInputFile
> HtmlInputHidden
> HtmlInputImage
> HtmlInputRadioButton
> HtmlInputText
> HtmlSelect
> HtmlTextArea
> ImageButton
> ListBox
> RadioButtonList
> TextBox

Why textbox persists data during postback even if View State set to off

Whenever a page is submitted or posted back to server,the entire form
data is posted to the server as a collection with the request. The
collection is in the form of NamedValue collection and this collection
has the mapping with uniqueid of the control and the value of the
control. You can read the data from the form collection by using the
following code snippet

//Reading textbox value from the form collection 
string textboxvalue = Request.Form[textbox1.UniqueID];

ASP.NET uses this primitive to update the control’s value. ASP.NET uses IPostBackDataHandler for the controls that load the data from the form collection.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读