GridView Inherting

Discussion in 'ASP .Net Building Controls' started by Chuck P, Jun 25, 2007.

  1. Chuck P

    Chuck P Guest

    I am inherting from the GridView and adding a property
    ShowEditButton. When this is true a new column is added with an Edit
    button. I override the CreateColumns event to do this.

    What is the difference between, storing the property value in a private
    local variable and ViewState?

    In the design mode, I can change the property value and the display changes
    automagically. This happens whether I use viewstate or a private variable. I
    guess if a controls property is changed in design mode some event fires and
    the control is rerendered???

    At runtime I would like to change the value of the property in the PageLoad
    event.
    However, the control doesn't seem to get rerendered. Should I add something
    to the setter to get the control to rerender or something?

    thanks,
    Chuck P, Jun 25, 2007
    #1
    1. Advertising

  2. Hi Chuck,

    Unless the ShowEditButton property doesn't need to be persisted across
    postback, I think you will need to save it in ViewState.


    Without complete code listing, I cannot tell exactly what's the cause of
    the issue, but I can point you to a complete working example on how to
    extend the GridView control:

    #Cutting Edge: Extending the GridView Control -- MSDN Magazine, May 2006
    http://msdn.microsoft.com/msdnmag/issues/06/05/CuttingEdge/


    By the way, I've posted a reply to your previous post "GridView get ODS
    DataObjectTypeName", which I haven't seen your reply. Could you please tell
    me the status of it? Normally if you could post a reply to confirm the
    suggestion worked or not, we will easily track each post's status. Thanks.


    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Walter Wang [MSFT], Jun 26, 2007
    #2
    1. Advertising

  3. Chuck P

    Chuck P Guest

    I read that article. I wonder how you could change whether you want the
    checkbox column or not at runtime.

    If you have a property AddChkBoxColumn and you decided to change the value
    in the aspx. pageload, I don't think it would do anything because the
    control has already been initialized and gotten viewstate stuff. So the
    column is sitting their in the control.

    In the property set, maybe what you do is call CreateColumns after the SET?
    Chuck P, Jun 26, 2007
    #3
  4. Hi Chuck,

    You might have noticed that GridView's CommandField can dynamically
    show/hide various buttons:

    protected void Page_Load(object sender, EventArgs e)
    {
    foreach (DataControlField dcf in GridView1.Columns)
    {
    CommandField cf = dcf as CommandField;
    if (cf != null)
    {
    cf.ShowEditButton = CheckBox1.Checked;
    break;
    }
    }


    Internally, whenever a DataControlField is changed, GridView will set a
    flag RequiresDataBinding and calls DataBind again if needed.

    To fix HotGridView.cs:

    public bool AutoGenerateCheckBoxColumn
    {
    get
    {
    object o = ViewState["AutoGenerateCheckBoxColumn"];
    if (o == null)
    return false;
    return (bool)o;
    }
    set {
    if (AutoGenerateCheckBoxColumn != value)
    {
    ViewState["AutoGenerateCheckBoxColumn"] = value;
    if (base.Initialized)
    {
    base.RequiresDataBinding = true;
    }
    }
    }
    }


    (Note the sample code has some bug in OnPreRender, it should check whether
    or not FindControl returns null value)


    With above change, you will be able to use code like this to dynamically
    show/hide the checkBox column:

    GridView1.AutoGenerateCheckBoxColumn = CheckBox1.Checked;


    Hope this helps.


    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Walter Wang [MSFT], Jun 28, 2007
    #4
  5. Chuck P

    Chuck P Guest

    thanks, Walter very very helpful.

    I still don't fully understand the control life cycle.
    Adding
    if (base.Initialized)
    {
    base.RequiresDataBinding = true;
    }
    to my properties seemed to do it.

    I guess when you set that the gridivew regenerates its columns whenever it
    databinds. When does it databind again? After all the postback events have
    happened and imeadiately before rendering?
    Chuck P, Jun 28, 2007
    #5
  6. For every control inherited from BaseDataBoundControl, it will call
    EnsureDataBound() in its OnPreRender(); this will make sure it calls
    DataBind() if RequiresDataBinding is set. The DataBind() will take care of
    regenerating required child controls.


    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Walter Wang [MSFT], Jun 29, 2007
    #6
  7. Hi Chuck,

    Thank you for the code. You asked a new question about the CheckBox
    column's state will get reset if the postback is caused by the update
    button.

    After some debugging, I can see it's because the GridView gets re-bound to
    the data. This can also be reproduced by adding <asp:CommandField
    ShowEditButton="true" /> to the GridView: after you checked some CheckBox,
    if you edit a row, the checkbox states are reset since the GridView is
    re-bound. You will also notice that if you change the row's data in editing
    mode, and you click another row's "Edit" command again, the previously
    editing row's data will also get reset since the GridView will re-bind.

    I think is by design behavior and I doubt it's possible to persist the
    checkbox states when rebinding: what if the backend data is changed after
    rebinding? the previously checked rows' index may have changed too.



    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Walter Wang [MSFT], Jul 12, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nalaka

    GridView in a GridView

    Nalaka, Nov 11, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    6,603
    Nalaka
    Dec 5, 2005
  2. =?Utf-8?B?bWdvbnphbGVzMw==?=

    GridView Hierarchical View - Gridview in Gridview

    =?Utf-8?B?bWdvbnphbGVzMw==?=, May 9, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    17,956
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    May 9, 2006
  3. =?Utf-8?B?QXJ0?=

    Page inherting from .master - unexpected results

    =?Utf-8?B?QXJ0?=, May 26, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    354
    =?Utf-8?B?QXJ0?=
    May 26, 2006
  4. Frans Englich

    Inherting from object. Or not.

    Frans Englich, Jan 26, 2005, in forum: Python
    Replies:
    7
    Views:
    362
    Nick Coghlan
    Jan 28, 2005
  5. ThatsIT.net.au

    Inherting classes

    ThatsIT.net.au, Sep 7, 2009, in forum: ASP .Net
    Replies:
    4
    Views:
    352
    ThatsIT.net.au
    Sep 9, 2009
Loading...

Share This Page