Wrox Programmer Forums
|
BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0
This is the forum to discuss the Wrox book ASP.NET 2.0 Website Programming: Problem - Design - Solution by Marco Bellinaso; ISBN: 9780764584640
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 6th, 2009, 01:29 PM
Authorized User
 
Join Date: Jun 2009
Posts: 32
Thanks: 17
Thanked 0 Times in 0 Posts
Default Is there a reason why author called DataBind manually?

Hi,


1)
page 246 - Inside GridView.RowDeleted event handler author manually calls gvwCategories.DataBind
page 255 – Inside Page_Load author manually calls gvwArticles.DataBind
page 256 – inside ddlCategories_SelectedIndexChanged() author manually calls gvwArticles.DataBind

In all of the above cases author manually called DataBind instead of letting Asp.Net perform automatic data binding. I’ve omitted the above code from the application in order to see if Asp.Net would perform the data binding automatically, which it did.


a) Now since Asp.Net did perform automatic data binding, is there some other reason why author decided to manually call DataBind in the above methods?




2) page 247 – If user presses Cancel button, then insidedvwCategory_ItemCommand() author manually calls gvwCategories.DataBind().

I don’t see a point in doing that, since user canceled the operation ( by pressing Cancel button ) and thus DB wasn’t changed in any way. Thus I don’t think there is any point in rebinding gvwCategories inside dvwCategory_ItemCommand?! Correct?



thanx
 
Old October 6th, 2009, 04:39 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Does it really bind? Or reconstruct the data from View State?

Do you actually see the deleted item disappear from the list? That's typically the reason to manually call DataBind: to force a refresh of the data.

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old October 7th, 2009, 08:36 PM
Authorized User
 
Join Date: Jun 2009
Posts: 32
Thanks: 17
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Imar View Post
Does it really bind? Or reconstruct the data from View State?

Do you actually see the deleted item disappear from the list? That's typically the reason to manually call DataBind: to force a refresh of the data.

Imar
Yes I’m sure. Here’s how I’ve figured out that gvwCategories.DataBind statement (inside gvwCategories_RowDeleted event handler) isn't really necessary:

1 – first I've delete gvwCategories.DataBind(); statement ( inside gvwCategories_RowDeleted event handler )

Code:
      protected void gvwCategories_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
           gvwCategories.SelectedIndex = -1;
          // gvwCategories.DataBind();
           dvwCategory.ChangeMode(DetailsViewMode.Insert);
        }
2 – I then browsed to ManageCategories.aspx page, which displayed all the available categories


3 – I then opened Sql server and manually added a new entry to tbh_Categories table ( in other words, I’ve created a new category and named it Y ). Now if Asp.Net does perform automatic data binding when some category is deleted, then gvwCategories will also display new category Y

4 – I’ve then deleted “Beer-related articles” category ( by pressing Delete button)

5 – after a postback, gvwCategories also displayed category Y


Thus, if Asp.Net didn’t perform automatic binding, but instead reconstructed the data from viewstate, then gvwCategories wouldn’t also display category Y.



BTW - I did similar checks for code on pages 255 and 256
 
Old October 8th, 2009, 04:34 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Do you disable ViewState somewhere? On the control, the page or in Web.config?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old October 8th, 2009, 11:07 AM
Authorized User
 
Join Date: Jun 2009
Posts: 32
Thanks: 17
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Imar View Post
Do you disable ViewState somewhere? On the control, the page or in Web.config?

Imar
No. I've only omitted the gvwCategories.DataBind; statement, but left all of author's other code as it was ( and as far as i can tell, author hasn't disabled the viewstate )

Last edited by carewithl; October 8th, 2009 at 12:21 PM..
 
Old October 8th, 2009, 06:13 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Good catch. In these cases, the call to DataBind is not actually needed.

This is because whenever the page is created, or recreated after a postback, the following occurs:
  1. The ConfirmInitState method of the Gridview is called.
  2. The DataSourceID property (obtained from the declarative markup) is set to the ID of the ObjectDataSource.
  3. This then causes the OnDataPropertyChanged method to be called.
  4. The OnDataPropertyChanged method sets the RequiresDataBindingProperty to true.
  5. This then forces the DataBind method to be called before the control is rendered.
This is true for any control that inherits from BaseDataBoundControl.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
The Following User Says Thank You to Lee Dumond For This Useful Post:
carewithl (October 9th, 2009)
 
Old October 9th, 2009, 06:58 PM
Authorized User
 
Join Date: Jun 2009
Posts: 32
Thanks: 17
Thanked 0 Times in 0 Posts
Default

alrighty, thank you for your help





Similar Threads
Thread Thread Starter Forum Replies Last Post
Another reason to go more 'traditional'? patch BOOK: Professional CodeIgniter ISBN: 978-0-470-28245-8 0 August 3rd, 2009 03:00 PM
"what is the reason" heerajee Oracle 1 March 24th, 2006 05:52 AM
What's the reason for this error? batli25 .NET Framework 1.x 2 December 5th, 2005 08:47 AM
Unknown reason arnab2410 Pro VB Databases 0 February 9th, 2004 05:09 AM
Code Execution Stops for No Known Reason tcarnahan Access VBA 6 October 28th, 2003 04:00 PM





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