That will probably resolve the selected style issue, but imagine a grid with
an EditItemIndex. When databound, the datagrid fires ItemDataBound and
passes a particular ItemType. The ItemType for the row being edited is going
to suffer the same issue caused by the way SelectedIndex is handled. But
just resetting the EditItemIndex won't help since the item has already been
created, and the columns have all instantiated their ItemTemplate rather
than their EditItemTemplate.
Resetting the SelectedIndex right after ViewState is loaded -- now that
sounds like a hack. I wouldn't consider binding the grid before adding it to
the collection to be a hack at all... its common practice to assign default
values to dynamic controls just before adding them to a page. The reason is
if you were to set properties after adding it to the page, those properties
would be tracked and persisted in viewstate on the page, even though they
are default values. Default values aren't supposed to be persisted, because
theres no reason to ... the value is going to be restored by code on the
next postback, so why is it in viewstate too? By binding the grid first, I'm
saying "here is your default data... now do your thing." For example, you
can do it with a label like so:
l = new Label();
l.Text = "default text";
Controls.Add(l);
If some other control ends up changing the label's text for some reason, the
label persists the new value in viewstate, and on subsequent posts, the text
is maintained, even though I'm explicitly setting a default value on every
post. Its all because I'm setting the default value before viewstate is
being tracked (IsTrackingViewState). This is exactly how the framework
assigns values that you specify declaratively on an aspx page. Just check --
override OnInit, and check the value of a label that is declared on the
page. It should be the same as what it says in the aspx code. But place 100
of those labels on the page, or 1000, or 10000, and you wont see viewstate
get any larger at all. Now do the same thing with a dynamicly created
label -- set its text before adding it to the control collection. Then set
it after you add it to the control collection -- if you do the ladder,
viewstate will take a hit, and there's no reason for it to.
Obviously the grid's DataBind() wasn't designed with that use in mind. Which
means I'm back to my original problem ---
How do I get a grid NOT to persist all its data in viewstate, without
disabling viewstate on the entire gird?
[quoted text, click to view] "mklapp" <mklapp@zippy.com> wrote in message
news:3A444BA1-8E46-4D7D-A413-E4BBA7FF539B@microsoft.com...
> After rebinding the data on postback, try setting SelectedItemIndex to the
persisted SelectedItemIndex when you detect the postback. That should
assert the SelectedItemStyle and resolve any other state ambiguities
precipitated by your clever hack.