dropdown - autopostback

Posted by Community Admin on 03-Aug-2018 17:47

dropdown - autopostback

All Replies

Posted by Community Admin on 21-Oct-2010 00:00

Hi,

I'm developing my first user control to integrate with Sitefinity 4.0.

 - I have one  dropdown with AutoPostBack=true
- In page load i bind that dropdown if  IsPostBack=false
- When i select one value i loose the dropdown state and it becames an empty dropdwon.

I verified the EnableViewState Property and it's true.

What am i doing wrong ?

Posted by Community Admin on 22-Oct-2010 00:00

I'm having similar problems with data bound dropdowns.  I spent the last two days trying to get my first form to save without the lists being emptied during the post back causing validation to fail.  Seems to be related to viewstate, but I tried everything to enable it with no luck.  I decided to try reloading the lists during the postback.  My final solution: bind the lists during the OnInit event.  I usually use the OnLoad but it seems that's too late in the lifecycle.  I only bind the DataSource in the OnInit, and set the SelectedValue only during the full DataBind that happens on load.


Here's the code that worked for me:

<asp:DropDownList runat="server" ID="stateList" 
DataSource="<%# States %>" 
DataTextField="StateName" DataValueField="StateId"
/>

protected override void OnLoad(EventArgs e)
base.OnLoad(e);
if (!IsPostBack)
DataBind();

protected override void OnInit(EventArgs e)
base.OnInit(e);
stateList.DataBind();

public override void DataBind()
base.DataBind();
if (!IsPostBack)
if (!string.IsNullOrEmpty(Model.StateId))
stateList.SelectedValue = Model.StateId;

Hope this helps...
Matt

Posted by Community Admin on 22-Oct-2010 00:00

Hello Joana and Matt,

There indeed are problems with the Page's view state and the life cycle. By default the page's view state is not enabled and should be done through code. You can achieve this from the master page:

protected override void OnInit(EventArgs e)
    this.Page.EnableViewState = true;
    base.OnInit(e);

We are working on configuration screens which will allow you to set this from the UI, as well as other page properties.

Kind regards,
Radoslav Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 22-Oct-2010 00:00

it worked.

thanks

This thread is closed