p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 (http://p2p.wrox.com/forumdisplay.php?f=252)
-   -   Wrox Blog - Save Button & Refresh category (http://p2p.wrox.com/showthread.php?t=53515)

madAlan February 1st, 2007 07:14 PM

Wrox Blog - Save Button & Refresh category
 
Hi all,

I've noticed that after creating/modifing a Blog Entry, when I click "SAVE" button the category repeater control does not report correct data. eg. If a create a new Blog Entry in "Personal" category this is not added.

I see that on Page Load event of the BlogEntriesFilter control, a query to categories and DataBind are made in case on NOT PostBack.

I've placed this 2 lines of code
repCategories.DataSource = BlogManager.GetCategories()
repCategories.DataBind()
out of the "Not Page.IsPostBack " code, but nothing is changed.

What I'm doing wrong? How is the best to force a refresh of the repCategories asp control after the "SAVE" button event?

Alan

Imar February 2nd, 2007 02:52 AM

Hi Alan,

The problem is related to the order of events; Page_Load fires before the other events.

There are two ways to fix this:

1. Create a method on the User Control called UpdateList for example. Inside this method call GetCategories() and DataBind() as in the code below to update the list.
In the Save button's Click event, call UpdateList on the user control to force it to update the list.

2. Simply do a full redirect after the save operation:

  Response.Redirect("~/")

should do the trick as well.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.

madAlan February 2nd, 2007 07:36 AM

Hi Imar,

I created a method on BlogEntriesFilter.ascx
private void UpdateCategoriesList()
    {
        repCategories.DataSource = BlogManager.GetCategories();
        repCategories.DataBinding();
    }

but the compiler display a Build error:
"The event 'System.Web.UI.Control.DataBinding' can only appear on the left hand side of += or -="

I've not found many references for understaning this error....

Alan

Imar February 2nd, 2007 07:49 AM

DataBinding() is an event. Instead, you need to call DataBind()

private void UpdateCategoriesList()
{
  repCategories.DataSource = BlogManager.GetCategories();
  repCategories.DataBind();
}

Also, to be able to call the method from another page, you need to make it public instead of private.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.

madAlan February 2nd, 2007 09:02 AM

Yes, my error... and now also the method is pubblic.

I found to implement this more complex, that I imagined. My knowledge on ASP.NET is yet too low.

I found very difficult to understand how to invoke the UpdateCategoriesList() in BlogEntriesFilter user control from BlogEntries user control. As the SAVE button click event is in the BlogEntries user control.

Try to find help on-line but the question seems to be complex.
Any suggestion?

Thanks a lot for the graet support you already give me!
Alan




Alan

madAlan February 2nd, 2007 10:12 AM

Addendum on my tests.
I tryed to implement this "category refresh" in Page_Load event of Default.aspx.
protected void Page_Load(object sender, EventArgs e)
    {
        if (BlogEntriesFilter1.IsPostBack)
        {
            BlogEntriesFilter1.UpdateCategoriesList();
        }
    }

With debugger I've seen that after the save button click is clicked the PostBack is invoked the UpdateCategoriesList method is called, but unfortunatly the result is still the same. No update was displayed in the renderd page.

Alan

Imar February 2nd, 2007 11:37 AM

Hi Alan,

Yeah, it's a difficult problem, because the functionality is embedded in User Controls. To promote reuse, you can't strongly couple the controls. So, although you technically could, you shouldn't find a reference in control A to control B and call some method.

Instead, you should base this on Events. The BlogEntry control should fire an Event called OnBlogEntrySaved (or whatever you want to call it). The containing page handles this event (similar to how it handles a Button's click) and then calls RefreshGridView on the other user control.

For an implementation of this, look at the WebShop chapter. The ShoppingCartView.ascx control defines an event called CartUpdated that is used to signal other parts of the application that the shopping cart has changed.

However, if you want to do this much and much simpler, simply redirect to:

Response.Redirect(Request.CurrentExecutionFilePath )

In addition, to preserve the querystring, look at GetNavigateUrl in the BlogEntriesFilter control.

Hope this helps,

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.


All times are GMT -4. The time now is 06:33 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.