 |
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
 | This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 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
|
|
|
|
|

June 16th, 2008, 08:01 AM
|
|
Authorized User
|
|
Join Date: Jun 2008
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Chapter 6, OnSelectedIndexChanged
After I had worked through Chapter 6, the drop-down list did not switch the themes. When I compared my master page with the author's source code, I found the following attribute-value pair in his definition of the DDL, but missing from mine:
"OnSelectedIndexChanged='lstPreferredTheme_Selecte dIndexChanged'."
When I inserted it into my code, the DDL worked.
Is this explained in the book? If so, where?
What is the rationale for this code?
Does it have to be inserted manually in markup view?
If it can be inserted via smart tags, what is the procedure?
|
|

June 16th, 2008, 08:59 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Typically the default event action of a control such as a button Click or a ddl OnSelectedIndexChanged can be "wired up" by double clicking the control in the design surface. This will generate the handler "wiring" and take you to the handler method in the code-behind.
Alternatively you can go to the properties toolbox for the control, switch to the events view (lightning bolt button) and then double click on the event in the list. This will generate a handler method in the code-behind and wire it up. Is it possible that you did this but selected the wrong event? Can you post the exact code (ddl markup and relevant code-behind) in question?
Different languages treat the "wiring" in different ways. C# using explicit event handler assignments where VB.NET can use the "Handles" keyword.
Also different project types can set up the page in different ways to make it appear a little different in the markup.
-Peter
compiledthoughts.com
|
|

June 16th, 2008, 11:24 AM
|
|
Authorized User
|
|
Join Date: Jun 2008
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Dear Peter,
Thank you for your prompt replies to my questions.
This is the relevant markup from MasterPage.master in the official source code for Chapter 6:
Select a Theme <asp:DropDownList ID="lstPreferredTheme" runat="server"
AutoPostBack="True" OnSelectedIndexChanged="lstPreferredTheme_Selected IndexChanged">
<asp:ListItem>Monochrome</asp:ListItem>
<asp:ListItem>DarkGrey</asp:ListItem>
</asp:DropDownList>
This was my original markup:
Select a Theme <asp:DropDownList ID="lstPreferredTheme" runat="server"
AutoPostBack="True">
<asp:ListItem>Monochrome</asp:ListItem>
<asp:ListItem>DarkGrey</asp:ListItem>
</asp:DropDownList>
The instructions for creating and configuring this DDL are given in the section "Letting the User Select a Theme" on pages 222 to 225 of the book.
I cannot find anything there about the insertion of the missing code (OnSelectedIndexChanged="lstPreferredTheme_Selecte dIndexChanged"), nor can I find any explanation of why this code is needed. Coding the handler itself was not the problem.
When we program the Page_Load function (in the C# code file), we don't have to insert any extra code in the main markup to tell the page to invoke this function whenever it (the page) is loaded. So why do we have to tell a drop-down list, in the main markup, to invoke its own SelectedIndexChanged function?
Could you let me know whether any other controls require this kind of treatment and, if so, which?
Thank you for your trouble.
With best regards,
Roman
|
|

June 16th, 2008, 03:58 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
Take a look at page 223, step 4:
Quote:
quote:Double-click the drop-down list to set up an event handler for the SelectedIndexChanged event. Alternatively, select the DropDownList controlâs Properties Grid and press F4, click the little lightning bolt to switch to the Events list, and then double-click SelectedIndexChanged. Figure 6-20 shows the Properties Grid in Events mode.
The code in the SelectedIndexChanged handler fires as soon as the user makes a new selection in the drop-down list.
|
As soon as you double-click the control, VWD should add the OnSelectedIndexChanged="lstPreferredTheme_Selected IndexChanged" code for you to the drop down list in the markup.
Have you been mixing VB.NET and C# pages? As Peter pointed out, VB.NET uses the handles keyword to hook up handlers.
You don't need to hook up Page_Load for you as the IDE and .NET do it for you. Take a look at the AutoEventWireUp attribute in the Page directive. When set to true, common Page methods like Load and Init are found and called automatically. However, this only works for the Page class, not for other controls. More info:
http://forums.asp.net/p/932513/1096656.aspx
http://odetocode.com/Blogs/scott/arc...2/16/2914.aspx
http://www.google.com/search?hl=en&q=AutoEventWireUp
Hope this helps,
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
|
|

June 16th, 2008, 04:49 PM
|
|
Authorized User
|
|
Join Date: Jun 2008
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Dear Imar,
Thank you very much for a great book and for your detailed response to my question.
I have tried the double-click trick again and this time it worked. I have no idea what happened the first time round. Perhaps I deleted the code in a fit of absentmindedness, or maybe my dog ate it. I'm sorry for wasting people's time with an unnecessary question.
Thank you also for the links to further information.
As far as the languages are concerned, I have only been using C#.
With best regards,
Roman
|
|

June 16th, 2008, 05:02 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
It's probably the dog. I have a cat that I accuse of eating my code as well, as I seem to loose a lot of it lately when she's around... ;)
It it wasn't the dog, maybe you double-clicked the control and then discarded (e.g. closed without saving) or undid the changes in the ASPX file. That way, the handler is in the code behind but it's not setup in Markup View.
Hope you are enjoying the book.
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
|
|
 |