custom usercontrol inside of datagrid - loses its state/viewstate on re-bind/postback of the datagrid

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default 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 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? I have tried going
    into the usercontrol and removing the if (!Page.IsPostBack) <binding of the
    data....> procedure in the OnLoad event, however that solves the empty
    dropdowns on postback/re-bind of the datagrid, but the selectedDate property
    doesn't get re-set again. So i am still screwed there.

    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. can i use dynamic variable inside a dataadapter and bind to datagrid
      Hi I want to use a sql designed in the dataadapter and I want to pass a variable so it can retrieve related data by using "WHERE" clause in the...
    2. 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...
    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: custom usercontrol inside of datagrid - loses its state/viewstate on re-bind/postback of the datagrid

    Your user control should implement INamingContainer which is an
    interface that has no methods, therefore implementing it is extremely
    easy
    MyUserControl: WebControl, INamingContainer


    Then make sure that your UserControl is always given the same ID upon
    postback and done so in a timely manner.

    Follow these 2 steps and your usercontrol should be able to maintain
    its viewstate across postbacks

    recoil@community.nospam Guest

  4. #3

    Default Re: custom usercontrol inside of datagrid - loses its state/viewstate on re-bind/postback of the datagrid

    Well i was already implementing INamingContainer, can't even remember if it
    was purposely or not:

    public class SimpleDatePicker : System.Web.UI.UserControl,
    System.Web.UI.INamingContainer
    However, as far as giving it the same ID on postback - I am not dynamically
    adding the control to the datagrid, i am actually creating a tag on the page
    for the usercontrol and putting it right into the grid in design-time such
    as:

    <%@ Register TagPrefix="UserControl" TagName="SimpleDatePicker"
    Src="usercontrols/SimpleDatePicker.ascx" %>

    <UserControl:SimpleDatePicker id="DatePickerExpires" runat="Server"
    SelectedDate='<%#DataBinder.Eval(Container.DataIte m, "DATE_EXPIRES") %>'>
    </UserControl:SimpleDatePicker>
    So would that make a difference? Do i need to dynamically add it on the
    datagrid ItemDataBound event or something?

    Thanks for your help, you definitely have me started in the right
    direction...

    - DNJ










    "recoil@community.nospam" <marc.derider@gmail.com> wrote in message
    news:1111424498.215454.120270@l41g2000cwc.googlegr oups.com...
    > Your user control should implement INamingContainer which is an
    > interface that has no methods, therefore implementing it is extremely
    > easy
    > MyUserControl: WebControl, INamingContainer
    >
    >
    > Then make sure that your UserControl is always given the same ID upon
    > postback and done so in a timely manner.
    >
    > Follow these 2 steps and your usercontrol should be able to maintain
    > its viewstate across postbacks
    >

    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