Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics 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 February 13th, 2006, 02:54 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 245
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to cast ASP Drop Down List to HtmlSelect?

Hello
I have an <asp:Drop Down List/> that is dynamically populated by Javascript depending on what the previous Drop Down List selection is. However, on "Search" button click, I want the server to take the Selected Value from <asp:Drop Down List/> and pass it to a parameter.
The problem is, when I step through the code in Javascript, the DDL has the property of Form1.ddl.option.value and Form1.ddl.option.text. When I step through the same ddl in Code behind, there is no value for DataTextField and DataValueField.

I was trying to cast the ASP DDL to HtmlSelect, because the Javascript populates the ddl as HtmlSelect. This is an invalid cast and VS.NET 2003 won't let me.

So what can I do???
Help

 
Old February 13th, 2006, 12:56 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

You can't cast it. You could also use <select runat="server">, which would be the HtmlSelect control instead. When rendered, the asp:DropDownList is a <select> element, so I dont' now what the problem is...

Brian
 
Old February 13th, 2006, 12:57 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Oh, and DataTextField and DataValueField are for databinding; the selectedvalue is for the selected value or selecteditem.text for the text, in the code-behind.

Brian
 
Old February 13th, 2006, 11:04 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

There will be an inherent problem if you change the <select> elements contents for a server control. The server control will restore the contents of the option list from viewstate and then apply whatever change was posted. So if you start with the options 1, 2 & 3, then change those options in javascript to 4, 5 & 6, the server side code will ignore the client side change in the list and restore the control with 1, 2 & 3. It will also attempt to apply the change (now you'll have either 4, 5 or 6 selected) but it can't find them in the current list so it will default back to the first option (1). This can cause erroneous index change events or other strange things to happen. If you want to be able to support this client-side option list change behavior, you'll have to use standard non-server form elements and manually capture the post values using the Request.Form collection. This is doing it old school style but it could be done.

-Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
drop down list ashu_gupta75 Excel VBA 1 October 15th, 2007 05:52 PM
drop down list values based on another drop down noor ASP.NET 1.0 and 1.1 Basics 3 July 5th, 2005 09:57 AM
Drop Down List mrideout BOOK: Beginning ASP.NET 1.0 6 March 18th, 2005 03:32 AM





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