Home All Groups Group Topic Archive Search About

UserControl inside of datagrid - loses its viewstate when datagrid is re-bound on postback

Author
21 Mar 2005 11:26 AM
DotNetJunky
I have a simple usercontrol, a datepicker which contains 3 dropdownlist , it
resides inside a datagrid column and i set the selecteddate property of the
usercontrol from one of the DataBinder.Eval 's ... everythign works fine.
The datepicker also works fine on a regular page, and will normally persist
its viewstate on a page.postback event. However now i am trying to use it in
a datagrid and running into problems.

When the datagrid is re-bound on postback or it's selectedIndexChanged
event, the usercontrol's dropdowns become empty. It isnt saving it's
viewstate and i cant figure out why.

Can someone point me to the right direction of how to persist the state of a
usercontrol's dropdownlists on re-bind of the datagrid?

Inside the usercontrol i am viewstating the properties of it, IE, the
selecteddate, selectedday/month/year , etc. Everything is viewstated with a
prefix of this.ID + "_" + propertyvalue.

Thanks in advance,

- DNJ

Author
21 Mar 2005 5:32 PM
Brock Allen
You should only load (DataBind) the DataGrid when IsPostBack is false. So
don't do it in every request. Rebuilding it every time flushes the ViewState
and thus all the behavior is going to not work as you've noticed.

-Brock
DevelopMentor
http://staff.develop.com/ballen



Show quoteHide quote
> I have a simple usercontrol, a datepicker which contains 3
> dropdownlist , it resides inside a datagrid column and i set the
> selecteddate property of the usercontrol from one of the
> DataBinder.Eval 's ... everythign works fine. The datepicker also
> works fine on a regular page, and will normally persist its viewstate
> on a page.postback event. However now i am trying to use it in a
> datagrid and running into problems.
>
> When the datagrid is re-bound on postback or it's selectedIndexChanged
> event, the usercontrol's dropdowns become empty. It isnt saving it's
> viewstate and i cant figure out why.
>
> Can someone point me to the right direction of how to persist the
> state of a usercontrol's dropdownlists on re-bind of the datagrid?
>
> Inside the usercontrol i am viewstating the properties of it, IE, the
> selecteddate, selectedday/month/year , etc. Everything is viewstated
> with a prefix of this.ID + "_" + propertyvalue.
>
> Thanks in advance,
>
> - DNJ
>
Author
21 Mar 2005 9:09 PM
DotNetJunky
Brock, The only time i am re-binding the datagrid is when PageIndexChanged
(requred for paging unless i am mistaken - IE, you set the newpageindex and
then call databind to show the new page) as well as when rows are deleted or
updated. Most importantly i need  this fixed for when rows are deleted...

I am doing a check for page.ispostback on page_load and doing the initial
databind() there, however i also fire the databind() on the events mentioned
above.

Thanks,
- DNJ


Show quoteHide quote
"Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
news:128369632470051255114592@msnews.microsoft.com...
> You should only load (DataBind) the DataGrid when IsPostBack is false. So
> don't do it in every request. Rebuilding it every time flushes the
> ViewState and thus all the behavior is going to not work as you've
> noticed.
>
> -Brock
> DevelopMentor
> http://staff.develop.com/ballen
>
>
>
>> I have a simple usercontrol, a datepicker which contains 3
>> dropdownlist , it resides inside a datagrid column and i set the
>> selecteddate property of the usercontrol from one of the
>> DataBinder.Eval 's ... everythign works fine. The datepicker also
>> works fine on a regular page, and will normally persist its viewstate
>> on a page.postback event. However now i am trying to use it in a
>> datagrid and running into problems.
>>
>> When the datagrid is re-bound on postback or it's selectedIndexChanged
>> event, the usercontrol's dropdowns become empty. It isnt saving it's
>> viewstate and i cant figure out why.
>>
>> Can someone point me to the right direction of how to persist the
>> state of a usercontrol's dropdownlists on re-bind of the datagrid?
>>
>> Inside the usercontrol i am viewstating the properties of it, IE, the
>> selecteddate, selectedday/month/year , etc. Everything is viewstated
>> with a prefix of this.ID + "_" + propertyvalue.
>>
>> Thanks in advance,
>>
>> - DNJ
>>
>
>
>
Author
21 Mar 2005 10:15 PM
Brock Allen
Ah, ok... hmm... Well, then when you rebind the DataGrid do you do it via
the Page.DataBind() or do you do it directly on the DataGrid control? If
you do it Page wide then you might be blasting the entries in the DropDownList.


When you debug this, in your SelectedIndexChange event does the DDL have
all of its items?

-Brock
DevelopMentor
http://staff.develop.com/ballen



Show quoteHide quote
> Brock, The only time i am re-binding the datagrid is when
> PageIndexChanged (requred for paging unless i am mistaken - IE, you
> set the newpageindex and then call databind to show the new page) as
> well as when rows are deleted or updated. Most importantly i need
> this fixed for when rows are deleted...
>
> I am doing a check for page.ispostback on page_load and doing the
> initial databind() there, however i also fire the databind() on the
> events mentioned above.
>
> Thanks,
> - DNJ
> "Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
> news:128369632470051255114592@msnews.microsoft.com...
>
>> You should only load (DataBind) the DataGrid when IsPostBack is
>> false. So don't do it in every request. Rebuilding it every time
>> flushes the ViewState and thus all the behavior is going to not work
>> as you've noticed.
>>
>> -Brock
>> DevelopMentor
>> http://staff.develop.com/ballen
>>> I have a simple usercontrol, a datepicker which contains 3
>>> dropdownlist , it resides inside a datagrid column and i set the
>>> selecteddate property of the usercontrol from one of the
>>> DataBinder.Eval 's ... everythign works fine. The datepicker also
>>> works fine on a regular page, and will normally persist its
>>> viewstate on a page.postback event. However now i am trying to use
>>> it in a datagrid and running into problems.
>>>
>>> When the datagrid is re-bound on postback or it's
>>> selectedIndexChanged event, the usercontrol's dropdowns become
>>> empty. It isnt saving it's viewstate and i cant figure out why.
>>>
>>> Can someone point me to the right direction of how to persist the
>>> state of a usercontrol's dropdownlists on re-bind of the datagrid?
>>>
>>> Inside the usercontrol i am viewstating the properties of it, IE,
>>> the selecteddate, selectedday/month/year , etc. Everything is
>>> viewstated with a prefix of this.ID + "_" + propertyvalue.
>>>
>>> Thanks in advance,
>>>
>>> - DNJ
>>>
Author
22 Mar 2005 3:01 AM
DotNetJunky
I call it directly on the DataGrid, not the Page... i have friends that
usually do the Page.DataBind() however i've never really used that method, i
try and control each (control) at the time it needs to be bound/manipulated,
plus i always check for rows before binding to list controls, and if there
are none i add a default/and or message.

The dropdownlist doesn't post back, it is just for selection only... no
selectedindexchanged event fires on it, no need for it.

I have been searching high and low for the answer but nothing has revealed
the problem... i am implementing INamingContainer on the usercontrol, so it
really isnt an ID conflict. the only thing i am definitely seeing during
debugging is that all the properties of the usercontrol on postback are
completely wiped from the ViewState. All properties read/write from
ViewState as i said using this.ID + "_" + as a prefix....




Show quoteHide quote
"Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
news:139748632470220914673056@msnews.microsoft.com...
> Ah, ok... hmm... Well, then when you rebind the DataGrid do you do it via
> the Page.DataBind() or do you do it directly on the DataGrid control? If
> you do it Page wide then you might be blasting the entries in the
> DropDownList.
>
> When you debug this, in your SelectedIndexChange event does the DDL have
> all of its items?
>
> -Brock
> DevelopMentor
> http://staff.develop.com/ballen
>
>
>
>> Brock, The only time i am re-binding the datagrid is when
>> PageIndexChanged (requred for paging unless i am mistaken - IE, you
>> set the newpageindex and then call databind to show the new page) as
>> well as when rows are deleted or updated. Most importantly i need
>> this fixed for when rows are deleted...
>>
>> I am doing a check for page.ispostback on page_load and doing the
>> initial databind() there, however i also fire the databind() on the
>> events mentioned above.
>>
>> Thanks,
>> - DNJ
>> "Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
>> news:128369632470051255114592@msnews.microsoft.com...
>>
>>> You should only load (DataBind) the DataGrid when IsPostBack is
>>> false. So don't do it in every request. Rebuilding it every time
>>> flushes the ViewState and thus all the behavior is going to not work
>>> as you've noticed.
>>>
>>> -Brock
>>> DevelopMentor
>>> http://staff.develop.com/ballen
>>>> I have a simple usercontrol, a datepicker which contains 3
>>>> dropdownlist , it resides inside a datagrid column and i set the
>>>> selecteddate property of the usercontrol from one of the
>>>> DataBinder.Eval 's ... everythign works fine. The datepicker also
>>>> works fine on a regular page, and will normally persist its
>>>> viewstate on a page.postback event. However now i am trying to use
>>>> it in a datagrid and running into problems.
>>>>
>>>> When the datagrid is re-bound on postback or it's
>>>> selectedIndexChanged event, the usercontrol's dropdowns become
>>>> empty. It isnt saving it's viewstate and i cant figure out why.
>>>>
>>>> Can someone point me to the right direction of how to persist the
>>>> state of a usercontrol's dropdownlists on re-bind of the datagrid?
>>>>
>>>> Inside the usercontrol i am viewstating the properties of it, IE,
>>>> the selecteddate, selectedday/month/year , etc. Everything is
>>>> viewstated with a prefix of this.ID + "_" + propertyvalue.
>>>>
>>>> Thanks in advance,
>>>>
>>>> - DNJ
>>>>
>
>
>
Author
22 Mar 2005 3:04 AM
Brock Allen
> i have friends that usually do the Page.DataBind() however i've never really
used
> that method

We all have "friends" that do *those* sorts of things ;)

> I have been searching high and low for the answer but nothing has
> revealed the problem... i am implementing INamingContainer on the
> usercontrol, so it really isnt an ID conflict. the only thing i am
> definitely seeing during debugging is that all the properties of the
> usercontrol on postback are completely wiped from the ViewState. All
> properties read/write from ViewState as i said using this.ID + "_" +
> as a prefix....

Well... we're still missing something. Sorry I can't tell what it is...

-Brock
DevelopMentor
http://staff.develop.com/ballen
Author
22 Mar 2005 3:16 AM
DotNetJunky
Here i attached the control, see if you can get at it, if not let me know i
will post it into the body of a new post. Play with it, let me know what you
think. Just throw it into a datagrid, link it in from design-time into a
column and set the selecteddate property = to a date... change the page of
the datagrid and watch it reset the dropdowns to no selected property...
well, only if you feel like it :)

Anyway it's a handy little control for simplicity's sake...


Show quoteHide quote
"Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
news:151037632470394763978704@msnews.microsoft.com...
>> i have friends that usually do the Page.DataBind() however i've never
>> really
> used
>> that method
>
> We all have "friends" that do *those* sorts of things ;)
>
>> I have been searching high and low for the answer but nothing has
>> revealed the problem... i am implementing INamingContainer on the
>> usercontrol, so it really isnt an ID conflict. the only thing i am
>> definitely seeing during debugging is that all the properties of the
>> usercontrol on postback are completely wiped from the ViewState. All
>> properties read/write from ViewState as i said using this.ID + "_" +
>> as a prefix....
>
> Well... we're still missing something. Sorry I can't tell what it is...
>
> -Brock
> DevelopMentor
> http://staff.develop.com/ballen
>
>
>
>

[attached file: SimpleDatePicker.ascx.cs]
Author
22 Mar 2005 3:20 AM
DotNetJunky
Sorry here is the ascx file as well, although just a few simple
dropdownlists.

Oops i guess there is some postback handling i forgot about... although just
for populating the year/month

I can catch that with a ItemCommand in the datagrid.. i just forgot about it
and apparently wasnt.





Show quoteHide quote
"Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
news:151037632470394763978704@msnews.microsoft.com...
>> i have friends that usually do the Page.DataBind() however i've never
>> really
> used
>> that method
>
> We all have "friends" that do *those* sorts of things ;)
>
>> I have been searching high and low for the answer but nothing has
>> revealed the problem... i am implementing INamingContainer on the
>> usercontrol, so it really isnt an ID conflict. the only thing i am
>> definitely seeing during debugging is that all the properties of the
>> usercontrol on postback are completely wiped from the ViewState. All
>> properties read/write from ViewState as i said using this.ID + "_" +
>> as a prefix....
>
> Well... we're still missing something. Sorry I can't tell what it is...
>
> -Brock
> DevelopMentor
> http://staff.develop.com/ballen
>
>
>
>

[attached file: SimpleDatePicker.ascx]