GridView Inherting

C

Chuck P

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

Walter Wang [MSFT]

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 ([email protected], 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.
 
C

Chuck P

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

Walter Wang [MSFT]

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 ([email protected], 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.
 
C

Chuck P

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

Walter Wang [MSFT]

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 ([email protected], 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.
 
W

Walter Wang [MSFT]

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 ([email protected], 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top