为什么DropDownList.SelectedValue是依靠视图状态? [英] Why does DropDownList.SelectedValue is relied on viewstate?

查看:95
本文介绍了为什么DropDownList.SelectedValue是依靠视图状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在我的网页上设置:的EnableViewState =真ViewStateMode =已禁用 - 然后 - 视图状态是禁用的页面(除非覆盖。 .. 的)

If I set on my page : EnableViewState="true" ViewStateMode="Disabled" - Then - the Viewstate is disable for the page ( unless override...)

然后,尝试读取(的假设在最后转储到屏幕和值已被填充的控件中选择的):

Then, trying to read from (assuming the control has been populated in the last dump to the screen and a value is selected):

MyDDL.SelectedValue 将产生

这是因为禁用ViewState中的:

That's because of disabled viewstate :

但我的问题是在更高层次上:

But my question is at a higher level :


  • 如果它是所有关于形式的值(这是我仍然可以从的Request.Form [MyDDL.UniqueID] 获得) - 我们正在谈论的输入这并不需要什么保存的价值。

  • If it's all about a form value (which I still can get from Request.Form[MyDDL.UniqueID]) - and we're talking about an input which doesn't need anything to save its value.

为什么确实名为的DropDownList 属性(的SelectedValue )就是靠它上的ViewState?

Why does the DropDownList property named (SelectedValue) Is relied on ViewState ?

P.S。文本框的 onchangeevent 不依赖于视图状态虽然控制是一个输入(其中犯规需要的ViewState) - 它保存的文本值,然后它的时候postback.But只依赖于视图状态进行比较,当你设置onchange事件(和自动回)

p.s. the TextBox onchangeevent does rely on viewstate although the control is an input (which doesnt need viewstate) - it saves the value of the text and then it compare it when postback.But it only relies on viewstate when you set onchange event ( and autopostback)

推荐答案

摘要:如果你想不ViewState的工作控制,你需要填充/绑定在每一个回传Items集合。我建议在Page_Init事件做(即OnInit方法)。

首先,我总是这样推荐这真棒文章:<一href=\"http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx\">TRULY了解的ViewState。

First off, I always recommend this this awesome article: TRULY Understanding ViewState.

本的SelectedValue不需要的ViewState。纵观code代表列表控件,它的DropDownList从继承,我们看到了code:

The SelectedValue doesn't require ViewState. Looking at the code for ListControl, which DropDownList inherits from, we see the code:

public virtual string SelectedValue
{
  get
  {
    int selectedIndex = this.SelectedIndex;
    if (selectedIndex >= 0)
      return this.Items[selectedIndex].Value;
    else
      return string.Empty;
  }

重要的是要从此code带走的是项目列表,必须填充得到的SelectedValue。

The important thing to take away from this code is that the Items list must be populated to get the SelectedValue.

如果你使用ViewState中,该项目集合的持续到/从ViewState中,它允许SelectedValue属性,而无需重新绑定控件加载工作。

If you utilize ViewState, the Items collection is persisted to / loaded from ViewState, which allows the SelectedValue property to work without rebinding the control.

这篇关于为什么DropDownList.SelectedValue是依靠视图状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆