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

Ask a Question related to ASP.NET Data Grid Control, Design and Development.

  1. #1

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

    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



    DotNetJunky Guest

  2. Similar Questions and Discussions

    1. Get controls values upon auto-postback of another control inside DataGrid
      Hi This is edited from post from a while back, there's still something I need to find an answer for (boy datagrids are a headache when you first...
    2. custom usercontrol inside of datagrid - loses its state/viewstate on re-bind/postback of the datagrid
      I have a simple usercontrol, a datepicker which contains 3 dropdownlist , it resides inside a datagrid column and i set the selecteddate property of...
    3. DataGrid empty on Postback with ViewState enabled
      Hi there! I'm having a problem with the datagrid control. I can't seem to get the datagrid to restore it's contents from ViewState. A...
    4. viewstate of the user control inside datagrid...
      Hi there! I have datagrid with template column and I am using the same user control for display (in ItemTemplate) and collect (in EditItemTemplate)...
    5. Problem with UserControl Property in a DataGrid after Postback
      Hello, I have this page.aspx ---------- <%@ Register TagPrefix="uc1" TagName="Userdetails" Src="Userdetails.ascx" %> ... <asp:datagrid...
  3. #2

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

    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
    [url]http://staff.develop.com/ballen[/url]


    > 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
    >


    Brock Allen Guest

  4. #3

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

    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
    > [url]http://staff.develop.com/ballen[/url]
    >
    >
    >
    >> 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
    >>
    >
    >
    >

    DotNetJunky Guest

  5. #4

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

    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
    [url]http://staff.develop.com/ballen[/url]


    > 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
    >> [url]http://staff.develop.com/ballen[/url]
    >>> 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
    >>>


    Brock Allen Guest

  6. #5

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

    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....




    "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
    > [url]http://staff.develop.com/ballen[/url]
    >
    >
    >
    >> 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
    >>> [url]http://staff.develop.com/ballen[/url]
    >>>> 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
    >>>>
    >
    >
    >

    DotNetJunky Guest

  7. #6

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

    > 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
    [url]http://staff.develop.com/ballen[/url]




    Brock Allen Guest

  8. #7

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

    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.





    "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
    > [url]http://staff.develop.com/ballen[/url]
    >
    >
    >
    >



    DotNetJunky Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139