Why does DropDownList.SelectedValue is relied on viewstate?

后端 未结 4 482
南方客
南方客 2021-02-08 22:01

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

The

4条回答
  •  无人共我
    2021-02-08 22:38

    If you want the DropDownList to work without ViewState, you can bind the control in page_load only once as given below:

         protected void Page_Load(object sender, EventArgs e)
                {        
         //whatever you use declarative binding (in aspx page), or define data source  here
                    if (!IsPostBack)
                    {
                      ddl.DataBind(); //fire databinding events and fill items, and selectedvalue has a value.
                    }
    
                    //you can get the selectedvalue
                    var sv=ddl.SelectedValue ; //
                }           
    
    • In the case ( ViewState is disabled), Asp.net FrameWork retrieve the items from the back end with every PostBack.

    • In the case (ViewState is enabled), Asp.net FrameWork retrieve the items from the ViewState without hitting the back end with every PostBack

    Normally, the Asp.net FrameWork fire the data binding events in PreRender event: Read ASP.NET Page Life Cycle Overview

    You can confirm that behavior by enabling Trace.

    SelectedValue doesn't rely directly on ViewState from source code of ListControl , BUT depend on items as described above.

提交回复
热议问题