Home All Groups Group Topic Archive Search About

force viewstate to clear on OnCommand event

Author
18 Dec 2006 5:23 PM
J055
Hi

I have a number of nested repeater and placeholder controls. When a
LinkButton is clicked I delete a db record. I'm relying on viewstate quite a
lot so when a linkbutton's OnCommand event is rasied I'd like to remove the
record from the viewstate so that the rest of the page controls load from
there. Is that possible?

Thanks
Andrew

Author
18 Dec 2006 5:42 PM
J055
OK, I think I want to clear the Parent repeater control which contains the
LinkButton. I only seem to be able to see the Repeater Item.

Something like this:

protected void btnRemove_Command(Object sender, CommandEventArgs e)

{

// delete record and clear LinkButton's parent Repeater

LinkButton btn = (LinkButton)sender;

btn.Parent.Parent.Clear();


}

Thanks
Author
19 Dec 2006 5:30 AM
Walter Wang [MSFT]
Hi J055,

After I carefully reviewed your question, I'm still not quite clear about
it. Do you mean that you want to get the reference to the Repeater from a
LinkButton's Click event (the LinkButton resides in Repeater's template)?

If this is the case, you can turn on Trace in your @ Page directive and you
can see the control hierarchy. You will learn that the Repeater is
LinkButton.Parent.Parent.

To clear the repeater items, bind it to a null reference:

    protected void link1_Click(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        Repeater rpt = btn.Parent.Parent as Repeater;
        rpt.DataSource = null;
        rpt.DataBind();
    }



Sincerely,
Walter Wang (waw***@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Author
19 Dec 2006 11:58 AM
J055
Hi Walter

Thanks for your reply. Actually I want to remove the RepeaterItem which is
the parent of the LinkButton. When the LinkButton command event is fired the
record is deleted from the database. The matching RepeaterItem row then
needs to be removed. If the deleted record was the last one then I need to
removed the whole repeater and a parent PlaceHolder.

The Repeater control has 2 Repeater ancestor controls which I don't want to
Re-bind if possible. Also I'm not sure how I could do this anyway.

Thanks again
Andrew
Author
19 Dec 2006 4:19 PM
Brennan Stehling
J055,

I believe what are looking to do is trim the size of the ViewState as
data items are removed from the repeaters.  And you want to do so
without pulling the data again and re-binding it.

Perhaps it takes a while to load the data so this is why you are trying
to avoid that overhead.  What you can do is place a DataSet into the
Cache and set a SqlCacheDependency on it to ensure it stays current.

http://msdn2.microsoft.com/en-us/library/xsbfdd8c.aspx

When you do remove or update an item in the repeater you should adjust
the row in the DataSet and then have the changes committed back to the
database with the AcceptChanges method.

http://msdn2.microsoft.com/en-us/library/system.data.dataset.acceptchanges.aspx

This assumes you have set it up to work as a typed DataSet with insert,
update and delete functionality.

Here is another way to do it...

http://msdn2.microsoft.com/en-us/library/zky7370c.aspx

The example there shows how a typed DataSet manages the changes

Brennan Stehling
http://brennan.offwhite.net/blog/

J055 wrote:
Show quoteHide quote
> Hi Walter
>
> Thanks for your reply. Actually I want to remove the RepeaterItem which is
> the parent of the LinkButton. When the LinkButton command event is fired the
> record is deleted from the database. The matching RepeaterItem row then
> needs to be removed. If the deleted record was the last one then I need to
> removed the whole repeater and a parent PlaceHolder.
>
> The Repeater control has 2 Repeater ancestor controls which I don't want to
> Re-bind if possible. Also I'm not sure how I could do this anyway.
>
> Thanks again
> Andrew
Author
19 Dec 2006 4:22 PM
Brennan Stehling
Of course once the changes for the DataSet are accepted successfully
you will want to set the DataSet as the DataSource on the repeater and
call DataBind on it.

Just be mindful of the SqlCacheDependency.  If another user changes the
data which in in this DataSet you will need to manage that potential
conflict.

Brennan Stehling
http://brennan.offwhite.net/blog/

Brennan Stehling wrote:
Show quoteHide quote
> J055,
>
> I believe what are looking to do is trim the size of the ViewState as
> data items are removed from the repeaters.  And you want to do so
> without pulling the data again and re-binding it.
>
> Perhaps it takes a while to load the data so this is why you are trying
> to avoid that overhead.  What you can do is place a DataSet into the
> Cache and set a SqlCacheDependency on it to ensure it stays current.
>
> http://msdn2.microsoft.com/en-us/library/xsbfdd8c.aspx
>
> When you do remove or update an item in the repeater you should adjust
> the row in the DataSet and then have the changes committed back to the
> database with the AcceptChanges method.
>
> http://msdn2.microsoft.com/en-us/library/system.data.dataset.acceptchanges.aspx
>
> This assumes you have set it up to work as a typed DataSet with insert,
> update and delete functionality.
>
> Here is another way to do it...
>
> http://msdn2.microsoft.com/en-us/library/zky7370c.aspx
>
> The example there shows how a typed DataSet manages the changes
>
> Brennan Stehling
> http://brennan.offwhite.net/blog/
>
> J055 wrote:
> > Hi Walter
> >
> > Thanks for your reply. Actually I want to remove the RepeaterItem which is
> > the parent of the LinkButton. When the LinkButton command event is fired the
> > record is deleted from the database. The matching RepeaterItem row then
> > needs to be removed. If the deleted record was the last one then I need to
> > removed the whole repeater and a parent PlaceHolder.
> >
> > The Repeater control has 2 Repeater ancestor controls which I don't want to
> > Re-bind if possible. Also I'm not sure how I could do this anyway.
> >
> > Thanks again
> > Andrew
Author
20 Dec 2006 7:14 AM
Walter Wang [MSFT]
Hi J055,

Sorry for the misunderstanding.

Since Repeater is designed to be used with data binding, I'm afraid it's
not possible to remove an individual RepeaterItem from Items without
re-binding the Repeater to the changed data source.

I think Brennan's suggestion is good if you have concern about the cost of
re-querying the data.       

Regards,
Walter Wang (waw***@online.microsoft.com, 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.