Wrox Programmer Forums
|
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 February 22nd, 2004, 11:04 PM
Registered User
 
Join Date: Feb 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default asp databound dropdownlist

Hoping someone can help end the misery.

I have a an aspx page that displays customer contact info at the top followed by a databound dropdownlist that contains a list of products the customer has purchased. The bottom of the page contains a label for each month of the current fiscal year showing total sales of the selected product per month.

When the page is first loaded, 3 ID's are passed from the previous page through a querystring to give me the id's required to load each of the 3 sections. Based on the 3rd one, a default product is selected in the dropdownlist and the labels are loaded accordingly.

Here's the problem:
Whenever I click in the dropdownlist to select a different product the page reloads and the customer data is gone, the dropdownlist is empty, and the labels show the previous information.

My OnSelectedIndexChanged event doesn't seem to connect with the dropdownlist. I have assigned hardcoded text to a label in the handler and it never gets set.

How can I refresh my page showing the new data based on the value selected in the dropdownlist?

Here's the code I am working with:

<SCRIPT language="vb" runat="server">
    Dim pgSalesmanID As Integer
    Dim pgCustomerID As Integer
    Dim pgProductID As Integer
    Dim curPos as Integer = 0

SUB Page_Load(sender as Object, e as EventArgs)
   If (Request.QueryString("sID") <> "") then
       pgSalesmanID = Convert.ToInt32(Request.QueryString("sID"))
   End if
   If (Request.QueryString("cID") <> "") then
    pgCustomerID = Convert.ToInt32(Request.QueryString("cID"))
   End if
   If (Request.QueryString("pID") <> "") then
        pgProductID = Convert.ToInt32(Request.QueryString("pID"))
   End if
   If (Request.QueryString("pos") <> "") then
    curPos = Convert.ToInt32(Request.QueryString("pos"))
   End if

   IF NOT Page.isPostBack then
    bindSQL()
   END IF
END SUB

SUB productList_OnChange (sender As Object, e As EventArgs)
   pgProductID = productList.SelectedItem.Value
   bindSQL()
END SUB


SUB bindSQL()
   loadProductList()
END SUB

SUB loadProductList()
   Dim cn as SQLConnection
   Dim cmd as SQLCommand
   Dim rdr as SQLDataReader
   Dim sConnectString as String = "connectionString"
   Dim sSql as String = "sp_mySp '8, 250, 11"

   cn = New SQLConnection(sConnectString)
   Try
    cn.Open()
    cmd = New SQLCommand(sSQL, cn)
    rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    productList.DataSource = rdr
    productList.DataBind()

    Catch myException As Exception
    Response.Write("An error has occurred trying to load
           Product: " & myException.ToString())
   Finally
        If Not rdr Is Nothing Then
       rdr.Close()
        End If
        productList.Items.Insert(0, "Select Product")
        If (pgProductID <> 0) then
        productList.SelectedValue = pgProductID
    Else
            productList.SelectedIndex = 0
    End If
   End Try
END SUB

</SCRIPT>


Then my grid:

<ASP:DropDownList id="productList" width="250"
    DataValueField="ProductID"
    DataTextField="ProductCode"
    AutoPostBack="true"
    OnSelectedIndexChanged="productList_OnChange"
    runat="server"/>


I hope my question isn't to confusing. Thanks for any help you can provide.





 
Old February 23rd, 2004, 04:26 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Could there be an error in retrieving the data:

Dim sSql as String = "sp_mySp '8, 250, 11"

There is an additional ' in front of your parameter list. Maybe the event does fire, but results in an error.

Form what I can see here, the OnSelectedIndexChanged should fire, and call productList_OnChange.

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old February 23rd, 2004, 01:02 PM
Registered User
 
Join Date: Feb 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The stored procedure does fire and it populates the dropdownlist without any problems on page load. (the asterix probably got messed up when I formated it in the text box before posting).

The funny thing is that productList_OnChange never seems to fire. I had the productID assigned to a label instead and it never gets set.

I also tried setting the handler from the sub like this:
SUB productList_OnChange (sender As Object, e As EventArgs) handles
    productList.SelectedIndexChanged
...but then my page wouldn't load at all.

I also tried turning off autoPostBack and used a button instead, but I still never get the value the user selected in the dropdownlist.

I also tried switching autowireup on and off. No go..

If I drop the databinding and hardcode the items for the dropdownlist then the event fires and I can retrieve my value. As soon as I databind it doesn't connect.

Does it make a difference if I databind using a dataset verses a reader? I am grasping at straws here.

Thanks for you comment Imar.



 
Old February 23rd, 2004, 02:37 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

This isn't the answer but cleaner code.

SUB bindSQL()
   Dim cn as SQLConnection
   Dim cmd as SQLCommand
   Dim rdr as SQLDataReader
   Dim sConnectString as String = "connectionString"
   Dim sSql as String = "sp_mySp '8, 250, 11"

   cn = New SQLConnection(sConnectString)
   Try
    cn.Open()
    cmd = New SQLCommand(sSQL, cn)
    rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    productList.DataSource = rdr
    productList.DataBind()

    Catch myException As Exception
    Response.Write("An error has occurred trying to load
           Product: " & myException.ToString())
   Finally
        If Not rdr Is Nothing Then
       rdr.Close()
        End If
        productList.Items.Insert(0, "Select Product")
        If (pgProductID <> 0) then
        productList.SelectedValue = pgProductID
    Else
            productList.SelectedIndex = 0
    End If
   End Try
END SUB

 
Old February 23rd, 2004, 04:19 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Try this:

SUB Page_Load(sender as Object, e as EventArgs)

   IF NOT Page.isPostBack then
    bindSQL()
If (Request.QueryString("sID") <> "") then
       pgSalesmanID = Convert.ToInt32(Request.QueryString("sID"))
   End if
   If (Request.QueryString("cID") <> "") then
    pgCustomerID = Convert.ToInt32(Request.QueryString("cID"))
   End if
   If (Request.QueryString("pID") <> "") then
        pgProductID = Convert.ToInt32(Request.QueryString("pID"))
   End if
   If (Request.QueryString("pos") <> "") then
    curPos = Convert.ToInt32(Request.QueryString("pos"))
   End if

   END IF
END SUB


 
Old February 23rd, 2004, 07:47 PM
Registered User
 
Join Date: Feb 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The only thing is that I have to pull in the query string variable before calling bindSQL. In the sample code above it shows the stored procedure variables hardcoded, but they are actually passed using pgProductID.ToString() for each variable. Which loads ok the first round.

I did try wrapping the query strings and bindSQL into the IF NOT page.isPostBack statement. My OnSelectedIndexChanged event still doesn't fire. Additionally, the page has a save button that calls and update stored procedure. For it to fire, update and refresh the page with the saved data, I found I needed to have the query string items outside the test for NOT page.IsPostBack.

I tried clearing my cache too, but I still can't make the dropdownlist connect to the handler for some reason.

 
Old June 2nd, 2008, 09:05 AM
Registered User
 
Join Date: Jun 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to twiger
Default

Make sure that you have EnableViewState set to true if you have a master page.

 
Old June 2nd, 2008, 09:07 AM
Registered User
 
Join Date: Jun 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to twiger
Default

Make sure you have EnableViewState set to true if you have a master page.






Similar Threads
Thread Thread Starter Forum Replies Last Post
asp.net dropdownlist cena BOOK: Beginning ASP.NET 1.0 0 March 19th, 2006 01:21 AM
DropDownList in ASP.NET shurabhavesh ASP.NET 1.0 and 1.1 Basics 3 June 1st, 2005 09:25 AM
asp:dropdownlist question khng ASP.NET 1.0 and 1.1 Basics 3 April 1st, 2005 06:50 AM
ASP.NET Dropdownlist pvasudevan ASP.NET 1.0 and 1.1 Professional 4 August 31st, 2004 11:21 AM
viewstate lost if dropdownlist databound kent ASP.NET 1.0 and 1.1 Basics 5 March 15th, 2004 04:43 PM





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