Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
|
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB 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 April 14th, 2012, 11:52 PM
Authorized User
 
Join Date: Dec 2011
Posts: 26
Thanks: 1
Thanked 4 Times in 3 Posts
Default Ch 15 exercise 2 don't need to call DataBind

Hi Imar,
In the solution for Ch. 15 exercise 2, you state that "Because the DropDownList control hasn't been data bound yet, it doesn't contain any items. Therefore you need to call DataBind() first."

However, I have found that this code works just fine for me in Reviews.aspx.cs

int _genreId;

protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Request.QueryString.Get("Ge nreId")) && Page.IsPostBack == false)
{
_genreId = Convert.ToInt32(Request.QueryString.Get("GenreId") );
DropDownList1.SelectedValue = Convert.ToString(_genreId);
}
}

I have run some tests and find that it seems to work for new inserts, editing review without changing genre, and editing review and changing review's genre. Am I missing something here? Is there a case where this isn't going to work? Or are you incorrect about needing to DataBind? Why does this work when I am not calling DropDownList1.DataBind();?

Thanks.
 
Old April 15th, 2012, 04:11 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Both solutions are correct, and both would work. By setting the SelectedValue property directly, the control remembers this value, so when it comes to data binding (done by the framework automatically), it knows which item to preselect.

By calling DataBind explicitly, the items are loaded into the control so FindByValue works as well. You can use either solution; however, by calling DataBind you get the actual items in the control at the time of Page_Load, giving you the opportunity to look at the items and add or change them. If you don't need that behavior (as in this exercise where you're only interested in preseleting the correct item) your solution is perfect.

Cheers,
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!





Similar Threads
Thread Thread Starter Forum Replies Last Post
My solution to exercise 6, on ch.2 - help rjm BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 2 July 28th, 2010 05:50 PM
Ch 11 Pg459 Exercise 5 C# brivad BOOK: Beginning ASP.NET 1.0 0 September 10th, 2007 08:59 AM
Ch 28 exercise using Eclipse keeotee BOOK: Beginning JavaServer Pages 1 April 2nd, 2006 12:09 PM
Ch. 18 Exercise 1, is there a change? horseatingweeds BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 4 November 29th, 2005 05:33 PM
CH.15 NEED HELP stacy BOOK: Beginning ASP 3.0 1 January 21st, 2004 03:37 AM





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