Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > dropdown list objects

Reply
Thread Tools

dropdown list objects

 
 
Aussie Rules
Guest
Posts: n/a
 
      07-25-2006
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


 
Reply With Quote
 
 
 
 
tdavisjr
Guest
Posts: n/a
 
      07-25-2006

"Aussie Rules" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
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
>
>

 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      07-26-2006
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.
=========
<aspropDownList ID="DropDownList1" runat="server">
<asp:ListItem>aaa</asp:ListItem>
<asp:ListItem>bbb</asp:ListItem>
<asp:ListItem>ccc</asp:ListItem>
</aspropDownList>
==========

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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
ajax cascading dropdown: second dropdown disabled acadam ASP .Net 0 12-27-2006 10:59 AM
bind a dropdown in a column in a datagrid based on the dropdown value selected in another column of the datagrid. vishnu ASP .Net 1 03-25-2006 01:24 PM
populating a dropdown list from arraylist of objects Paul M ASP .Net 1 05-26-2004 11:46 AM
Select dropdown box bleeds into Javascript dropdown menu Mike HTML 1 12-18-2003 09:49 PM



Advertisments