Refreshing a user control

M

MattB

I have a page header that I made as a user control (ascx) that I drop on
every page in my application. The application is an eCommerce application
and I have a total for items in the cart on the header. To keep this total
in sync, I count the items in the cart and refresh the label text in the
load event of my header control. This all works well, except for one
scenario. When the user is on the "Show Cart" page, they can delete items
from the cart. I implemented this using the DeleteCommand of a datagrid.
When this is done, the total does not reflect the item being deleted until I
manually refresh the page.
So I'd like to be able to refresh the header control or even the whole page
if an item is deleted. How can I do that? Thanks!

Matt
 
M

Marina

I'm guessing it's that the UserControl gets it's data, then the item is
deleted - but the usercontrol still has the predeleted information?

In this case, I recommend having a Refresh method on your UserControl that
can refresh the data, and have the code that deletes items call this method
to force the usercontrol to use the new data.
 
M

MattB

Thanks. I haven't done this before - got any (or links to) examples?
Really the part I need to know is how do I refresh the control?
I was thinking there would be something like Page.Refresh, but I couldn;t
find such a thing.
 
M

MattB

Actually, it works if I just redirect to the same page I'm on (to refresh)
so I'm using that for now. Is there a betterway to do that or is the
redirect OK? Thanks!
 
M

Marina

I just described exactly what needs to be done. Write a method on the user
control (surely you've written methods before), and then just call it from
your page (I'm sure you've called methods on objects).
 
K

Kikoz

Hi.

Calling a public method of a UserControl that takes care of changing some
property of some control (label or something) is ok, but ugly and not
manageable. The normal and most elegant approach would be to use events. For
instance:

your page gets loaded;
grid (or other control on the page itself) gets bound to some data sourse;
UserControl gets loaded, too;
initially that label (or whatever) of UserControl gets its Text property set
to something;
user clicks Delete link or button on your grid;
DeleteCommand event (or whatever you have attached to this action) gets
fired;
you do your magic to get rid of that record and re-bind the grid;
in the same event handler (DeleteCommand of the grid) you fire your own
event that you declared inside of the page's class or outside;
you can declare your event parameters class inherited from EventArgs class
which will be "used" by your event and instantiate it with your own
properties when you fire your custom event;
your UserControl will subscribe to that page's event and will "listen" to it
(look inside of InitializeComponent() method that VS created in your page's
class - that's how it's done);
if it "hears" event, it gets the total records from your custom
EventArgs-based class or (ugly way) goes to db again and finds out that
total number again.

Sounds awfully complicated but in a real life it's just several lines of
code. That way your page lives completely separate life as well as your
UserControl; they don't care about each other unless that event gets fired.
That's how it should be done.

You can start reading more about events and how to work with them here:
http://www.codeproject.com/csharp/eventdelegates.asp or check MSDN and
framework documentation. It's a great stuff, soon you'll be wandering how
you've managed to live without them :)

Regards,
Kikoz
 
S

Scott Mitchell [MVP]

MattB said:
Thanks. I haven't done this before - got any (or links to) examples?

You may find this article helpful in learning more about User Controls:

An Extensive Examination of User Controls
http://tinyurl.com/6p2ju

It contains examples of adding properties/methods to User Controls, as
well as events. hth

--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top