GridView controls in "Edit mode" does not save state ?

Last post 07-04-2009 4:34 PM by PeteNet. 8 replies.

Sort Posts:

  • GridView controls in "Edit mode" does not save state ?

    05-30-2009, 1:26 PM
    • Member
      point Member
    • Daneel
    • Member since 07-17-2008, 7:43 PM
    • Posts 15

    Hi,

    I have a GridView with some controls in it (DropDownList, TestBoxes, etc...).  After some debugging I noticed the followings:

    1. The line controls of the normal mode are not the same controls of "Edit Mode".
    2. When going into "edit mode" in a GridView, the "Edit Mode" conrols of the selected line are created (constructor, init etc..).
    3. These "Edit Mode" controls does not preserve their state. SaveViewState() is not called on them. (while the normal-mode controls are saving and restoring state as normal).

    Any idea why ?

    Filed under:
  • Re: GridView controls in "Edit mode" does not save state ?

    05-30-2009, 3:34 PM
    • Member
      166 point Member
    • vinay.v.raman
    • Member since 05-18-2009, 11:28 AM
    • Bangalore
    • Posts 55

    Hi Daneel,

    Please see my explanation below.

    1. Yes you are right. The normal mode (or I call it as View mode) controls are used only to show the currently selected data for a particular column of the data row. Since a column cannot have more than one value at any given point of time (except in a rare condition!!!) simple literal control is used to display the data.

    2. I do not exactly understand your question. But the code in the constructor and init is executed even in the normal mode. But the edit mode controls are rendered only during edit mode. (Does this clarify your doubt?)

    3. Yes, the edit mode controls do not save their state by default. The programmer/developer has to write logic to save the view state informationthen should restore them when the control is rebuilt.

    I hope my answers are satisfactory and meets your expectations.

    If you still have any more doubts do reply. And please do not forget to mark my post as answer if it serves your purpose.


    Regards

    Vinay

    Regards
    Vinay V

    It is better to fail when you tried than to fail to try.
  • Re: GridView controls in "Edit mode" does not save state ?

    05-31-2009, 2:49 AM
    • Member
      point Member
    • Daneel
    • Member since 07-17-2008, 7:43 PM
    • Posts 15

    Thanks Vinay for your reply, it helped a lot to see I was not missing something here.

    I have a follow-up question for you.

    One of my grid cells has a DropDownList in it. I now understand I'll need to manually save and restore the state of the "Edit Mode" control. Still, a DropDownList control has the ability to save its own state so why can't I use this capability somehow instead of re-inventing the wheel here ?

    I noticed I cannot call MyDropDownList.SaveViewState() method from outside. Does this mean I need to create my own custom control that will know how to save its own state once it is used as an "Edit Mode" control ? It doesn't seems logical to me ASP.NET requires the programmer to manually code this stuff while the control has the ability to save its own state, the framework just don't activate it on time.

    I hope I'm clear,

    Thanks !

  • Re: GridView controls in "Edit mode" does not save state ?

    06-04-2009, 3:24 AM

    Hi Daneel,

    You have to save the state of DropDownList into Session in the postback event. Then in RowDataBound event of GridView you can reset the values of DropDownList from Session. The same to other controls in GridView such as CheckBox or RadioButton. The pre-selected state will be lost after every postback.

    Thanks,

    Qin Dian Tang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: GridView controls in "Edit mode" does not save state ?

    06-04-2009, 3:57 AM
    • Member
      point Member
    • Daneel
    • Member since 07-17-2008, 7:43 PM
    • Posts 15

    Thanks Qin Dian Tang for your answer, still I wanted to know HOW to save the dropdown state ? what's the best way to do this taking into account a DropDownList has the ability to save its own state ? I would like to use that ability, and not to write it from scratch.

  • Re: GridView controls in "Edit mode" does not save state ?

    06-22-2009, 5:36 AM
    • Member
      point Member
    • Daneel
    • Member since 07-17-2008, 7:43 PM
    • Posts 15

    Qin Dian Tang - MSFT:

    Hi Daneel,

    You have to save the state of DropDownList into Session in the postback event. Then in RowDataBound event of GridView you can reset the values of DropDownList from Session. The same to other controls in GridView such as CheckBox or RadioButton. The pre-selected state will be lost after every postback.

    Thanks,

     

    Anyone else can assist me about this issue ? I would like to know what is the best way to save the state of a DropDownList control ?. This is required due to the fact that GridView in "edit mode" does not save its controls state automatically and I need to manualy do it (as described in this thread). 

    thanks :)

  • Re: GridView controls in "Edit mode" does not save state ?

    06-25-2009, 2:06 PM
    • Contributor
      7,023 point Contributor
    • superguppie
    • Member since 05-19-2009, 11:42 AM
    • Posts 1,240

    Sorry for the late response. Been away for a while. Odd that noone picked this one up. For DDL, I have this situation regularly. Actualy, I save and restore DDL selection even on regular DDLs. That way I can work all over my website and when I get back to the page, I get the old selection there. Only limitation to how I do it is that the DDL has the same items in it every time.

    Saving is done in the SelectedIndexChanged of the DDL:

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["DropDownList1_SelectedIndex"] = DropDownList1.SelectedIndex;
    }
    

    Restoring what was saved in DataBound:

    protected void DropDownList1_DataBound(object sender, EventArgs e)
    {
        if(Session["DropDownList1_SelectedIndex"] != null)
        {
            DropDownList1.SelectedIndex = (int)Session["DropDownList1_SelectedIndex"];
        }
    }
    

    This is for a single one. To save/restore per line of the GridView, the RowIndex of the GridViewRow the DDL is in could be added to the name of the session index. Or the KeyValue of the GridViewRow. For saving that would look somewhat like this:

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session[String.Format("DropDownList1_SelectedIndex_{0}", ((GridViewRow)((Control)sender).NamingContainer).RowIndex)]
            = DropDownList1.SelectedIndex;
    }
    

    (Haven't tested it, so there may be a few bumps to hammer out.)

    Superguppie.

    Please remember to click “Mark as Answer” on the post that helps you.
    This can be beneficial to other community members reading the thread.

    When all you've got is a Hammer,
    Every Problem looks like a Nail.
    Michael Swain.
  • Re: GridView controls in "Edit mode" does not save state ?

    07-04-2009, 3:55 PM
    • Member
      point Member
    • Daneel
    • Member since 07-17-2008, 7:43 PM
    • Posts 15

    Same here...

    Thanks a lot for your response, yet, as you mentioned, my challenge is how to save the list of items of the DDL, not only the selected index or value. I can re-retrieve the items from my DB each time but this seems a bit of a strange strategy to follow...

    I'm curious how I can use the DDL built-in capability to save and restore itself - the ability is there I just don't know how to use it.

    any ideas ?

  • Re: GridView controls in "Edit mode" does not save state ?

    07-04-2009, 4:34 PM
    • All-Star
      26,790 point All-Star
    • PeteNet
    • Member since 01-21-2009, 6:15 PM
    • Posts 3,822

    Daneel,

    you can save the list into the Session too, as a Sesssion variable, just like Session["DDL1Data"]  = <yourData>, where, say, yourData could be a DataTable etc and then retrieve it back again by casting it to the respective data source like:
    DataTable dt = (DataTable)Session["DDL1Data"];
    DDL1.DataSource = dt;
    DDL1.DataBind() = dt;
    //if you have the selectedIndex stored in the Session too then you could now also make it selected to that item, as
    DDL1.SelectedIndex = Convert.ToInt32(Session["DDL1SelectedIndex"]);              //where Session["DDL1SelectedIndex"] has the selectedIndex stored previously in the SelectedIndexChanged event

    as such, the DDL does not have any in-built capability to save and restore itself...it's just a control to display data that you provide

    on the other hand if you have like static data in these lists that you may want to share across all users you could cache so that you don't have to build it up repeatedly for a user (and likewise for all users) and which obviously makes it even more efficient.

    Regards,
    Peter
Page 1 of 1 (9 items)