Persisting DataSource on Postback using ViewState

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

  1. #1

    Default Persisting DataSource on Postback using ViewState

    Hi All,
    Ok, I've got a server control that implements custom databinding and
    generates a list of RadioButtons. (Yes, you'd think why not use the off the
    shelf RadioButtonList; because the RadioButtonList contains a collection of
    ListItems and not RadioButtons. We needed to disable a particular item in
    the RadioButtonList but apparently there is no way of disabling a ListItem -
    if there is, someone tell me!) Anyway, here's the problem:

    The control works like a champ except for one thing. The databinding
    implementation is somehow different than that of a standard out of the box
    control you get with asp.net. With say a DropDownList, you can do the
    following:
    if (!Page.IsPostBack)
    MyDropDownList.DataBind();
    and with this, it'll only hit the DataBind() once, when the page first
    loads. Everytime after that it uses viewstate.

    I understand why that works and have no questions about that. My question
    revolves around "how" it saves it's state to viewstate. Using the ViewState
    decoder found here [url]http://staff.develop.com/onion/resources.htm[/url] I can bind a
    normal DropDownList to a DataSource and use that tool to actually see the
    values it's storing in viewstate. The only way I came up with saving my
    custom datasource to viewstate was something like this:
    //During the initial DataBind() call
    ViewState["MyDataSource"] = MyDataTableDataSource;

    Then on postbacks I use ViewState["MyDataSource"] as the DataSource.
    Although that works it's bothering me that it's working differently than the
    standard controls. Decoding the viewstate of my custom control reveals that
    the way i'm saving my datasource to viewstate is different, it shows my
    variable name having a Binary value and the Binary value is HUGE for a
    datatable consisting of 2 rows and 2 short columns. I tried using the
    examples here
    [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcontemplateddataboundcontrolsample.asp[/url]
    but just couldn't figure out how they were saving the DataSource to
    viewstate. They save only enough info to repopulate the controls on
    postback which is what I want, i don't want to save my entire datatable or
    dataset to viewstate.

    Anyone have any ideas on this?

    TIA,
    Chris


    Chris Carter Guest

  2. Similar Questions and Discussions

    1. Dropdownlist not keeping viewstate after postback
      I have a complex datagrid containing several ASP.NET form elements. One of the elements is a checkboxlist. This is populated from a database but...
    2. Control not maintaing viewState and PostBack not firing event.
      I created a nWeb Custom Control that inherits from WebControls.Panel and add a CheckBoxList to this control. I can't seem to get this control to...
    3. DataSource & PostBack
      Hi there, I am binding a DataTable of a typed DataSet to a DataList control. Two columns are editable (i.e. Boolean types, implemented with a...
    4. 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...
    5. Persisting DDL.SelectedValue or DDL.SelectedItem.Value without Viewstate
      Use the forms collection. Something like Request.Form("myDDL") in VB. "Paul Aspinall" <paul@nospamaspy.co.uk> wrote in message...
  3. #2

    Default Re: Persisting DataSource on Postback using ViewState

    "Chris Carter" <chris@panteravb.com> wrote in message
    news:uy5jUcQhDHA.3700@TK2MSFTNGP11.phx.gbl...
    > Hi All,
    > Ok, I've got a server control that implements custom databinding and
    > generates a list of RadioButtons. (Yes, you'd think why not use the off
    the
    > shelf RadioButtonList; because the RadioButtonList contains a collection
    of
    > ListItems and not RadioButtons. We needed to disable a particular item in
    > the RadioButtonList but apparently there is no way of disabling a
    ListItem -
    > if there is, someone tell me!) Anyway, here's the problem:
    >
    > The control works like a champ except for one thing. The databinding
    > implementation is somehow different than that of a standard out of the box
    > control you get with asp.net. With say a DropDownList, you can do the
    > following:
    > if (!Page.IsPostBack)
    > MyDropDownList.DataBind();
    > and with this, it'll only hit the DataBind() once, when the page first
    > loads. Everytime after that it uses viewstate.
    >
    > I understand why that works and have no questions about that. My question
    > revolves around "how" it saves it's state to viewstate. Using the
    ViewState
    > decoder found here [url]http://staff.develop.com/onion/resources.htm[/url] I can bind
    a
    > normal DropDownList to a DataSource and use that tool to actually see the
    > values it's storing in viewstate. The only way I came up with saving my
    > custom datasource to viewstate was something like this:
    > //During the initial DataBind() call
    > ViewState["MyDataSource"] = MyDataTableDataSource;
    You don't save the data source to ViewState. On the initial request, you use
    the data source to set the properties of your child controls. Then, when you
    recreate the child controls on subsequent postbacks, the child controls will
    load their own properties from ViewState. You may also need to use ViewState
    to save some properties of the control as a whole which are not properties
    of the child controls. For instance, you may need to keep track of the
    number of child controls and which one is disabled. You can use these on
    PostBack to recreate the appropriate number of child controls and to
    disable the proper control.
    --
    John Saunders
    Internet Engineer
    [email]john.saunders@surfcontrol.com[/email]


    John Saunders Guest

  4. #3

    Default Re: Persisting DataSource on Postback using ViewState

    Thanks John, I was under the impression i would only have to recreate the
    radiobutton with the original ID(before viewstate was evaluated) and the
    rest of it's attributes would be persisted automatically thru viewstate.
    Anyway, i needed to save the GroupName, Text, value, and ID to viewstate on
    my own using a combination of arraylist, pair, and triplet objects then
    recreate those on postback and all worked out fine. Thanks again for the
    response.

    "John Saunders" <john.saunders@surfcontrol.com> wrote in message
    news:u$INC9RhDHA.2960@TK2MSFTNGP11.phx.gbl...
    > "Chris Carter" <chris@panteravb.com> wrote in message
    > news:uy5jUcQhDHA.3700@TK2MSFTNGP11.phx.gbl...
    > > Hi All,
    > > Ok, I've got a server control that implements custom databinding and
    > > generates a list of RadioButtons. (Yes, you'd think why not use the off
    > the
    > > shelf RadioButtonList; because the RadioButtonList contains a collection
    > of
    > > ListItems and not RadioButtons. We needed to disable a particular item
    in
    > > the RadioButtonList but apparently there is no way of disabling a
    > ListItem -
    > > if there is, someone tell me!) Anyway, here's the problem:
    > >
    > > The control works like a champ except for one thing. The databinding
    > > implementation is somehow different than that of a standard out of the
    box
    > > control you get with asp.net. With say a DropDownList, you can do the
    > > following:
    > > if (!Page.IsPostBack)
    > > MyDropDownList.DataBind();
    > > and with this, it'll only hit the DataBind() once, when the page first
    > > loads. Everytime after that it uses viewstate.
    > >
    > > I understand why that works and have no questions about that. My
    question
    > > revolves around "how" it saves it's state to viewstate. Using the
    > ViewState
    > > decoder found here [url]http://staff.develop.com/onion/resources.htm[/url] I can
    bind
    > a
    > > normal DropDownList to a DataSource and use that tool to actually see
    the
    > > values it's storing in viewstate. The only way I came up with saving my
    > > custom datasource to viewstate was something like this:
    > > //During the initial DataBind() call
    > > ViewState["MyDataSource"] = MyDataTableDataSource;
    >
    > You don't save the data source to ViewState. On the initial request, you
    use
    > the data source to set the properties of your child controls. Then, when
    you
    > recreate the child controls on subsequent postbacks, the child controls
    will
    > load their own properties from ViewState. You may also need to use
    ViewState
    > to save some properties of the control as a whole which are not properties
    > of the child controls. For instance, you may need to keep track of the
    > number of child controls and which one is disabled. You can use these on
    > PostBack to recreate the appropriate number of child controls and to
    > disable the proper control.
    > --
    > John Saunders
    > Internet Engineer
    > [email]john.saunders@surfcontrol.com[/email]
    >
    >

    Chris Carter Guest

  5. #4

    Default Re: Persisting DataSource on Postback using ViewState

    I am not sure how old this post is and whether this was not possible in past versions but I have disabled items in a RadioButtonList this way:
    rblEditRegistrations.Items(0).Enabled = False

    Quote Originally Posted by Chris Carter View Post
    Hi All,
    Ok, I've got a server control that implements custom databinding and
    generates a list of RadioButtons. (Yes, you'd think why not use the off the
    shelf RadioButtonList; because the RadioButtonList contains a collection of
    ListItems and not RadioButtons. We needed to disable a particular item in
    the RadioButtonList but apparently there is no way of disabling a ListItem -
    if there is, someone tell me!) Anyway, here's the problem:

    The control works like a champ except for one thing. The databinding
    implementation is somehow different than that of a standard out of the box
    control you get with asp.net. With say a DropDownList, you can do the
    following:
    if (!Page.IsPostBack)
    MyDropDownList.DataBind();
    and with this, it'll only hit the DataBind() once, when the page first
    loads. Everytime after that it uses viewstate.

    I understand why that works and have no questions about that. My question
    revolves around "how" it saves it's state to viewstate. Using the ViewState
    decoder found here [url]http://staff.develop.com/onion/resources.htm[/url] I can bind a
    normal DropDownList to a DataSource and use that tool to actually see the
    values it's storing in viewstate. The only way I came up with saving my
    custom datasource to viewstate was something like this:
    //During the initial DataBind() call
    ViewState["MyDataSource"] = MyDataTableDataSource;

    Then on postbacks I use ViewState["MyDataSource"] as the DataSource.
    Although that works it's bothering me that it's working differently than the
    standard controls. Decoding the viewstate of my custom control reveals that
    the way i'm saving my datasource to viewstate is different, it shows my
    variable name having a Binary value and the Binary value is HUGE for a
    datatable consisting of 2 rows and 2 short columns. I tried using the
    examples here
    [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcontemplateddataboundcontrolsample.asp[/url]
    but just couldn't figure out how they were saving the DataSource to
    viewstate. They save only enough info to repopulate the controls on
    postback which is what I want, i don't want to save my entire datatable or
    dataset to viewstate.

    Anyone have any ideas on this?

    TIA,
    Chris
    Owen Waldrep is offline Junior Member
    Join Date
    Aug 2010
    Posts
    1

  6. #5

    Default Re: Persisting DataSource on Postback using ViewState

    I would start by installing Firebug in Firefox and using its NET panel to look at exactly what is being posted to the server. The NET panel has a POST tab that will show you everything that's being sent. And yeah, that probably does include viewstate.
    [URL=http://www.pussy-dreams.com/niches/korean.php]korea pussy[/URL] | [URL=http://www.pussy-dreams.com/]pussy galleries[/URL] | [URL=http://www.pussy-dreams.com/niches/hairy.php]hairy cunts[/URL]
    kolleen.koby is offline Junior Member
    Join Date
    Mar 2012
    Location
    NY
    Posts
    10

Posting Permissions

  • You may not post new threads
  • You may not 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