dropdown list objects

A

Aussie Rules

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
 
T

tdavisjr

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.
 
S

Steven Cheng[MSFT]

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-us/vbcon/html/vboriwebformsdatabinding.
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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.

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



This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top