dropdown list objects

Discussion in 'ASP .Net' started by Aussie Rules, Jul 25, 2006.

  1. Aussie Rules

    Aussie Rules Guest

    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
     
    Aussie Rules, Jul 25, 2006
    #1
    1. Advertising

  2. Aussie Rules

    tdavisjr Guest

    "Aussie Rules" <> wrote in message
    news:...
    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
    >
    >
     
    tdavisjr, Jul 25, 2006
    #2
    1. Advertising

  3. 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.
     
    Steven Cheng[MSFT], Jul 26, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul M
    Replies:
    1
    Views:
    772
    Rick Spiewak
    May 26, 2004
  2. vishnu
    Replies:
    1
    Views:
    1,230
    Patrick.O.Ige
    Mar 25, 2006
  3. Mike
    Replies:
    1
    Views:
    824
  4. acadam
    Replies:
    0
    Views:
    766
    acadam
    Dec 27, 2006
  5. 7stud
    Replies:
    11
    Views:
    711
    Dennis Lee Bieber
    Mar 20, 2007
Loading...

Share This Page