Showing posts with label viewstate. Show all posts
Showing posts with label viewstate. Show all posts

Wednesday, March 28, 2012

Dynamic Control creation and viewstate management (client to server)

Hi all,

I am new to AJAX and this is my first port.

In my requirement i need to create set of dynamic controls (multiple instances of a custom webcontrol) to do this i am following the below steps.

    I have a user control with update panel in itI have a button "Add" to add new instance.Hidden field count to check number of dynamic instances addedHidden field to keep track of dynamic id'sRemove button to remove the instanceHandle UpdatePanel Load event to load the previous instance of controls before the control renders
    I have custom web control which inherits the ASP:Table classI override CreateChildControls method to create controls setHidden field to maintain the disabled control ids

rendering dynamic controls and its viewstate is working fine.

I am facing problem here.

I have some business logic in which the if the user selects "Yes" as the option in a RadioButtonList i am disabling few controls in the dynamically rendered sets at clientside. How to I get the controls state in the CreateChildControl method to see what was the control state using the hidden field as i am not able to get the form Posted values at this time. (Note: I know that we can use the UniqueID of the hidden field, but this will not work here as the unique id will be still the control ID unique id will be different when it renders.

So i want to know how to manage the viewstate from client and server in this scenario?

I tried to put in words as simple as possible.

Thanks for the help

- Satish

Missed to add this

When user clicks on "Yes" radio button i am saving the control id in the hidden field where in server side i can see the value in the hidden field at the server side and load the control state (either enabled or disabled). i am having problem here on how to retain the control state from clientside to the server side when it renders.

Thanks

Satish

Saturday, March 24, 2012

DropDownList SelectedIndexChanged event not firing

I have a dropdown list that has many items in it - so in order to reduce viewstate size I have shut off viewstate for that control. But since doing so - the selectedindexchanged event is not firing (I guess because without view state the control does not know if there was a change in the selected value so it can't determine if it should fire the changed event.

Is there any way I can detect this change on the client and fire the selectedindexchanged event on the server?

thanks
Michael

May be you can try to bind the dropdown list in the Page_Init event handler.. Then you do not have to worry about viewstate.. Give it a try..


I tried that but that means everytime there is a postback (async or sync) it has to hit the db to re-populate that list.

I tried keeping track of the previously selected value in viewstate manually to detect a change and it detected the change correctly but I get an event validation error.

thanks
Michael


ok. I guess we are missing the pieces of information here... So you do not care about re-populating the list again? so what is going to be in the list.. after the postback is finished?

What event validation error you are talking about? Do you even care about the SelectIndexChanged event? Or you can simply explain more about what exactly you are trying to do?


I have a drop down list with autopostback = true and when an item is selected an update panel will refresh based on the selected item in the dropdown list. So I don't need to reload the dropdown list once it is loaded initially.

I actually solved the issue by looking at the event source in the forms collection and if it is the dropdown list - I know that the selected item was changed because the dropdown list won't do a postback unless the selected item was changed. I couldn't use the selecteditemchanged event because with the viewstate disabled - the server side does not know if the selected item changed.

I thought I had to store the previously selected item in viewstate to detect if a new item was selected but it turns out that a postback only occurs when the selected item is changed - which makes sense - the browser know that the item was changed - even though the server does not.

thanks
Michael

Wednesday, March 21, 2012

DropdownList controls inside AccordionPane and Clearing ViewState on Control

I'm having problems with ViewState loading stale data on DropDowns embedded within an AccordionPane.

The dropdowns are bound to a generic collection object using Databinding via a 'PopulateList' method that's in a helper class:

//bind a listControl with an objectpublic static void PopulateList(System.Web.UI.WebControls.ListControl list,string valueDataName,string textDataName, Object bindingObject){ list.DataSource = bindingObject; list.DataValueField = valueDataName; list.DataTextField = textDataName; list.DataBind();}
the 'PopulateList' method gets called in the set property of each generic collection object: 

public System.Collections.Generic.IList<GenericCollectionItem<int,string>> PlanList

{get {return _planList; }set { _planList =value; ddPlan.Items.Clear();if (_planList.Count > 0) { ddPlan.Enabled =true; ListHelper.PopulateList(ddPlan,"ID","Description", _planList); }else { ddPlan.Enabled =false; } }}
The problem I'm having is when the list is empty, the dropdown retains the old values even
though the code to clear and disable the dropdown is being executed. Anyone know a way to
clear ViewState for an individual control or have any suggestions on what might be going on?
I don't want to disable the ViewState for the entire page or for the control, because the control
values will only be changed when the generic collection is changed, so i need viewstate for all other times...
Any help greatly appreciated!
Thanks

Hi kylen,

You could try Viewstate.Remove(key)

I think that the key is the ID of the control which viewstate is tracking. (more about viewstate here:http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx )

Another thing you could do is clear the items of the DDL in the load event. The Load occurs after the ViewState has set the last state-values to the controls (Well, I think the load is after ... otherwise try the PreRender of the page)

Let me know if anything worked out!

Kind regards,
Wim