Wrox Programmer Forums
|
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 January 24th, 2013, 04:20 PM
Friend of Wrox
 
Join Date: Mar 2011
Posts: 126
Thanks: 39
Thanked 2 Times in 2 Posts
Default Chapter 15 - Exercise #2 - solution(p.750)

Hi Imar,

I noticed that the genre gets selected correctly even though I forgot to enter the line, DropDownList1.DataBind().

I'm a little confused as to why it still works? Am I doing something wrong?

Thanks.
Tulsi
 
Old January 24th, 2013, 04:34 PM
Friend of Wrox
 
Join Date: Mar 2011
Posts: 126
Thanks: 39
Thanked 2 Times in 2 Posts
Default

I forgot to attach the code that I used ...

Code:
if (!Page.IsPostBack)
        {
            string genreId = Request.QueryString.Get("GenreId");

            if (!string.IsNullOrEmpty(genreId))
            {                                
                DropDownList1.SelectedValue = genreId.ToString();                
            }
        }
I noticed that I did not do the FindByValue(genreId) check. When I add this check in, I needed to do the databind.

Just curious as to why my initial code works without the databind?

thanks.
Tulsi
 
Old January 25th, 2013, 03:38 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

.NET calls DataBind for your controls as well during the data binding life cycle stage. When you set the SelectedValue, the control remembers that value and then uses it later when it is binding to preselect the correct item.

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!
 
Old January 25th, 2013, 03:44 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Forgot to answer the FIndByValue part. That requires data binding indeed as when you call it, the item is looked for in the Items collection. Without binding, the Items collection is empty and the item is not found.

With SelectedValue, all the control needs is to store the value locally and use it later when DataBind is called implicitly....

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!
The Following User Says Thank You to Imar For This Useful Post:
Tulsi (January 29th, 2013)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 4, Exercise 2 Solution malie22001 BOOK: Beginning Visual C# 2010 0 May 19th, 2012 11:43 PM
Chapter 14 Exercise 3 and solution. UncleJeff BOOK: Beginning ASP.NET 4 : in C# and VB 2 March 14th, 2011 02:15 PM
Chapter 3 Exercise 4 Solution KurtBergman BOOK: Beginning Visual C# 2010 8 July 16th, 2010 08:13 PM
Chapter 3 - Exercise 4 - Solution gives no output Cryotic BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 2 April 19th, 2010 08:57 AM
Solution to exercise 7, Chapter 2 Nick Y BOOK: Ivor Horton's Beginning Visual C++ 2005 0 May 28th, 2006 04:28 AM





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