开发者

C# Control Function Load order

开发者 https://www.devze.com 2023-04-12 13:37 出处:网络
I am trying to have a dropdownlist set a temporary variable in the ViewState: ViewState[\"TempProductVariantId\"]. Once it is set and the page reloads, I want to use that variable inside a function. A

I am trying to have a dropdownlist set a temporary variable in the ViewState: ViewState["TempProductVariantId"]. Once it is set and the page reloads, I want to use that variable inside a function. After that function is complete the ViewState["TempProductVariantId"] is set to 0. My issue is I need to know what order the default functions load in the control. Ie. Page_Load, OnPreRender, SelectedIndexChange, etc...

DropDownList SelectedIndexChange

protected void ArtistArtwork_SelectedIndexChange(object sender, EventArgs e)
{
    DropDownList ddl = sender as DropDownList;
    TempProductVariantId = int.Parse(ddl.SelectedValue);
}
开发者_StackOverflow

Page Load

protected void Page_Load(object sender, EventArgs e)
{
    CreateAttributeControls();
    TempProductVariantId = 0;    
}

The problem with this is it work's literally every other time I change the selected index on the DropDownList.

So again I want to set it, use it, discard it. Any tips would be appreciated.


You need to look at the ASP.NET Page Life Cycle Overview. Specifically look at the Control Events. To answer your specific example though, ArtistArtwork_SelectedIndexChange is running after Page_Load.


The asp.net page lifecycle overview can be found here: http://msdn.microsoft.com/en-us/library/ms178472.aspx but basically its Page_init -> Viewstate loaded -> Page_Load -> Events Fire -> Page_PreRender -> ViewState Saved -> Page_Render. Another trick you can use is to only initialize state variables on initial load by checking Page.IsPostBack (true on subsequent loads of the page)

Hope this helps.

0

精彩评论

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