473,322 Members | 1,232 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

dropdown list objects

Hi,

I am migrating my skills from winforms to webforms, so sorry for the most
basic of questions

When I add an item into a dropdownlist in winforms, i create an object and
add the object to the dropdownlist using code such as

Dim oObject As New clsObject
oObject.ID = pRow("Style_ID")

oObject.Name = pRow("style_description".ToString)

cboStyle.Items.Add(oObject)
THis code does seem to work in webforms, as its pretty well much exactly
what I have used to populate the dropdownlist, however when I try to get
back the selected items ID using the code below, it fails with the error
'Unable to cast object of type 'System.Web.UI.WebControls.ListItem' to type
'clsObject'.'

The following code is from the dropdownlists selectedindexchanged event.

Dim oObject As clsObject

oObject = DirectCast(sender.SelectedItem, clsObject) ' fails on this line'

intGenre = oObject.ID

Is there meant to be a different way to get back the selected object ?

Thanks
Jul 25 '06 #1
2 2779

"Aussie Rules" <Au*********@nospam.nospamwrote in message
news:e1**************@TK2MSFTNGP02.phx.gbl...
The SelectedItem property is of the type of ListItem, therefore, this cast
from ListItem to your object is not allowed. I don't see how you were able
to add your custom object to this dropdownlist anyways since the Items.Add()
method take either a ListItem or a string as a parameter, then again, if you
are using VB, if you passed in the object, then the VB runtime will probably
call the ToString() method of your object and this is probably why things
appear to work. I'm not sure; but this probably would have given you a
complie time error if you was using C#.

Having said all of that, the answer is no. The list in WinForms is different
from the List in WebForms and you can add custom object to the list.

Hi,

I am migrating my skills from winforms to webforms, so sorry for the most
basic of questions

When I add an item into a dropdownlist in winforms, i create an object and
add the object to the dropdownlist using code such as

Dim oObject As New clsObject
oObject.ID = pRow("Style_ID")

oObject.Name = pRow("style_description".ToString)

cboStyle.Items.Add(oObject)
THis code does seem to work in webforms, as its pretty well much exactly
what I have used to populate the dropdownlist, however when I try to get
back the selected items ID using the code below, it fails with the error
'Unable to cast object of type 'System.Web.UI.WebControls.ListItem' to
type 'clsObject'.'

The following code is from the dropdownlists selectedindexchanged event.

Dim oObject As clsObject

oObject = DirectCast(sender.SelectedItem, clsObject) ' fails on this
line'

intGenre = oObject.ID

Is there meant to be a different way to get back the selected object ?

Thanks

Jul 25 '06 #2
Thanks for tdavisjr's input,

Hi Aussie,

As tdavisjr has mentioned, the ASP.NET Dropdownlist control is quite
different from the winform dropdownlist(this also applies to other controls
exists in both winform and ASP.NET). Actually this diference is due to the
underlying infrastructure and programming model. As for ASP.NET
Dropdownlist, it supports the following means to populate items:

1. statically declare the dropdownlist items in aspx template e.g.
=========
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>aaa</asp:ListItem>
<asp:ListItem>bbb</asp:ListItem>
<asp:ListItem>ccc</asp:ListItem>
</asp:DropDownList>
==========

2. Manualy create ListItem and add it into DropdownList.Items collection.
e.g:
=========
dim item1 as ListItem = new ListItem("item1", "item1")
dim item2 as ListItem= new ListItem("item2", "item2")
DropDownList1.Items.Add(item1)
DropDownList1.Items.Add(item2)
=========

3. populate the dropdownlist through databinding, supply a datasource and
which property (from the datasource record) to bound with the dropdownlist
item's property. e.g.

================
#CategoryClass has two public properties(ID and Name)

dim categories(5) as new CategoryClass;
// initlizing the categories array

DropDownList1.DataTextField= "Name"
DropDownList1.DataValueField = "ID"
DropDownList1.DataSource = categories
DropDownList1.DataBind()
==================

The following msdn reference has mentioned binding a dropdownlist with
datasource control(for ASP.NET 2.0):

#DropDownList Web Server Control Declarative Syntax
http://msdn2.microsoft.com/en-us/library/0dzka5sf.aspx

For your scenario, after you've added items into DropDownList, we can only
access them through the "Items" collection also, and the object in the
collection is of "ListItem" type(we can not convert it to our own custom
class type). The ListItem class have two public properties(Text and Value)
which are the two ones most useful to us. Also, you can access the current
selected Item in the DropDownList control through its "SelectedItem"
property.
In addition, the databinding in ASP.NET web page is quite different from
winform databinding, you can have a look at the following articles:

#Web Forms Data Binding
http://msdn.microsoft.com/library/en...rmsdatabinding.
asp?frame=true

#ASP.NET data binding overview
http://support.microsoft.com/kb/307860/en-us
If you have any more specific questions or problems, you can also provide
us some further code logic or background info so that we can have a look
and provid some further suggestions.

Hope this helps you a little.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to

http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial

response from the community or a Microsoft Support Engineer within 1
business day is

acceptable. Please note that each follow up response may take approximately
2 business days

as the support professional working with you may need further investigation
to reach the

most efficient resolution. The offering is not appropriate for situations
that require

urgent, real-time or phone-based interactions or complex project analysis
and dump analysis

issues. Issues of this nature are best handled working with a dedicated
Microsoft Support

Engineer by contacting Microsoft Customer Support Services (CSS) at

http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Jul 26 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Joseph Barron | last post by:
Here is a SIMPLE problem that I'm trying to solve. It works in Netscape 6.2, but IE6 gives ""No such interface supported." Below are page1.htm and page2.htm . In page1.htm, there are two...
32
by: Continental Translations | last post by:
Can anybody help me? I am trying to create two drop down menus, where the results of the second one vary depending on what was selected in the first one. I am using MS Script Editor in MS Front...
8
by: Kris Rockwell | last post by:
Hello, I have done the following to bind data to a DropDown box: 1. Drag SQLServer table onto web form to create data connection and data adapter. 2. Generate dataset by right-clicking on...
1
by: Paul M | last post by:
hi there, i have an arraylist which holds my Customer object, that contains the customer details, but when i try binding it to a dropdown box, i cant get the values to appear.. this is what i...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
5
by: jung_h_park | last post by:
From: jung_h_park@yahoo.com Newsgroups: microsoft.public.dotnet.framework.aspnet Subject: Dropdown List not retaining its SelectedValue Date: Mon, 26 Jun 2006 21:02:57 -0700 Hello, My...
5
by: Jeff User | last post by:
Hi all The date values in my dropdown list appear as mm/dd/yyyy Specifically, the day always contains 2 digits. I need to set the selected index of the dropdown to the date that equals another...
3
by: er1 | last post by:
Hi all, I have created a double dropdown list. Based on the first list selection, second list populates (this works fine). I have a submit button, which when clicked should run a select query...
3
by: fish919 | last post by:
Hello All, I am creating a date base in access. I want to create a dropdown list box that is connected to another dropdown list box. You start with a dropdown list that has 5 choices and each of...
4
by: zion4ever | last post by:
Hello good people, Please bear with me as this is my first post and I am relative new to ASP. I do have VB6 experience. I have a form which enables users within our company to do an intranet...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.