Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 25th, 2006, 09:49 PM
Registered User
 
Join Date: Apr 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default DropDownList SelectedIndexChanged problem

Hi all

I am developing an asp.net application in VB on VS2003.
In one of my forms I have a DropDownList called cboDiscount which is populated from SQL database and cboDiscount contains 14 items with appropriate values (0 or 5). It is set to AutoPostBack is set to True and has an associated event cboDiscount_SelectedIndexChanged.

Here is my problem:
When the first or second item (index 0 and 1) is selected the cboDiscount.selectedIndex is 0 or 1 appropriately.
However, when any item > 2 is selected (index > 1), within the event cboDiscount.SelectedIndex is always 1. The SelectedValue And SelectedItem.Text is also same as index 1.

Basically what I want to do is to retreive the Text and Value of the selected item.

As a note the DDL is only populated once.

This is the code where I populate the DDL:
sqlDr = sqlCmd.ExecuteReader()
            cboName.Items.Clear()
            cboName.Items.Add("None")
            cboName.Items.FindByText("None").Value = "0"
            While sqlDr.Read
                If Not IsDBNull(sqlDr.Item("1")) Then
                    cboName.Items.Add(sqlDr.Item("1"))
                    cboName.Items.FindByText(sqlDr.Item("2")).Value = sqlDr.Item("Value")
                End If
            End While

This is the code for the event:
   Private Sub cboDiscount_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboDiscount.SelectedIndexChanged
        Dim intIndex As Integer = cboDiscount.SelectedIndex
        txtDiscountAmt2.Text = cboDiscount.SelectedValue
        txtDiscountType2.Text = cboDiscount.SelectedItem.Text
    End Sub

Any thoughts, ideas or similar problems?
Thanks

 
Old April 26th, 2006, 08:45 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Hi,

So if you look at the source on the page is the drop-down getting populated with the correct index and values?

I use tons of drop-downs in my VB ASP.NET application. Here is an example of how I populate the drop-down. I'm not sure if that's what your problem is or not.

I'm also using SelectedIndexChanged event with autopostback set to true to populate another drop down based on the values of the first drop-down. And even though I'm not really writing any values to text boxes in my application on the event changed, I tried it just to test and it works fine. I can write the correct index values a shown in the source to a text box. Which is what it seems that you are trying to do.

Hope this helps.

Richard

Code:
Sub getAreaID(ByVal FacID As Integer, ByVal RuleID As Integer)
        Dim SQL As String = "SELECT Areas.AreaID, Areas.AreaName FROM Areas [blah, blah, blah];"
        Dim oDA As New SqlDataAdapter(SQL, _oConn)
        Dim oDS As New DataSet
        oDA.Fill(oDS)
        ddlAreaID.DataSource = oDS
        ddlAreaID.DataTextField = "AreaName"
        ddlAreaID.DataValueField = "AreaID"
        ddlAreaID.DataBind()
        ddlAreaID.Items.Insert(0, New ListItem("", 0))
    End Sub


Code:
    Private Sub ddlAreaID_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlAreaID.SelectedIndexChanged
        Response.Write("This is the value: " & ddlAreaID.SelectedValue)
        txtTestValue.Text = ddlAreaID.SelectedIndex
    End Sub




 
Old April 26th, 2006, 08:27 PM
Registered User
 
Join Date: Apr 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you rstelma. However this did not fix the problem.
The problem, in my opinion, is that the values in the DDL had only 0's and 5's. I have read somewhere on the web that identical values may cause problems.

It worked fine when I removed the Values.
I got it working in a different way.

Thanks

 
Old April 27th, 2006, 10:20 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

How's that?






Similar Threads
Thread Thread Starter Forum Replies Last Post
dropdownlist on selectedindexchanged RoniR ASP.NET 1.x and 2.0 Application Design 4 April 6th, 2012 04:50 PM
DropDownList SelectedIndexChanged Event does not davej ASP.NET 2.0 Basics 3 October 12th, 2007 10:20 AM
Problem with Dropdownlist.SelectedIndexChanged haydar BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 0 July 20th, 2007 10:26 AM
dropdownlist problem naresh.net ASP.NET 1.0 and 1.1 Basics 4 July 7th, 2005 01:22 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.