Results 1 to 4 of 4

Thread: SelectedItem for a DropDownList, SelectedItem ALWAYS 0

  1. #1

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    SelectedItem for a DropDownList, SelectedItem ALWAYS 0

    Why is this value always 0?
    I have searched this forums and haven't found much.
    I have this code:
    VB Code:
    1. Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    2.         Dim objComments As New Comment
    3.         With objComments
    4.             .Name = txtName.Text
    5.             .Email = txtEmail.Text
    6.             .TypeID = cboArea.SelectedIndex
    7.             .Comment = txtComments.Text
    8.             .Update()
    9.         End With
    10.     End Sub
    Where "comments" is a class module that has 4 props and some code to save to the DB. Simple really.

    Regardless of what I select in my combo the selectedindex is always 0, which means I can't get the ID value for the selected item.
    I have ASP.NET Unleased and it's sample code is exactly what I have above

    Woka

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    Haven't messed around with .Net (it refused to install on my computer...) but the old property was 'listindex', or list(row,column).

    listindex was the actual index selected
    list allows you to access the columns (such as the hidden one with the value or the text (visible) column.

    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Hahahaha... You have a point, but alas that is VB6. .NET is very different in that aspect. Most of the controls have completely different interfaces and functionality
    This is what is making it hard for me to learn as I keep trying to code in VB6 and it doesn't recognise the code...Booooooo, bad .NET *slap*

    When dealing with ASP.NET the controls act funny coz they arn'ty on the client, they are on the server. So you have to do some strange method to "post" the value back...can't seem to work out what it is though

    Thanks for your answer.

    Woodf

  4. #4

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Fixed it
    I loaded the values in the Page_Load event, but didn't check if was a post back This reset my combo to the 1st entry everytime I click the send button
    What I have now is:
    VB Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim ds As DataSet
    3.         Dim db As New Database
    4.         Dim objRow As DataRow
    5.         If Not IsPostBack Then
    6.             ds = db.GetDataSet("SELECT * FROM CommentTypes")
    7.             With cboArea
    8.                 .DataSource = ds.Tables(0)
    9.                 .DataTextField() = "Description"
    10.                 .DataValueField = "ID"
    11.                 .DataBind()
    12.             End With
    13.         End If
    14.     End Sub
    I know the code can be tidied up a little more. But it works

    Woof

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width