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 November 20th, 2003, 11:17 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default DropDownList control

Hello,

I'm using a drop down list control on the page, and I databind it in this manner:
Code:
DropDownList1.DataSource = objDataSet
DropDownList1.DataMember = "DB"
DropDownList1.DataTextField = "name"
DropDownList1.DataValueField = "name"
DropDownList1.DataBind()
The problem I'm having is that of the 40+ records that are loaded into it, when I go to reference it in any of the ways below:
Code:
DropDownList1.SelectedValue
DropDownList1.SelectedItem.Text
DropDownList1.SelectedItem.Value
DropDownList1.SelectedIndex
I only get the first entry. The selected index is zero, and the values and text values is only the first one, no matter what record I have selected. I've tested the SelectedIndexChanged with the AutoPostback to true, and when the index changed, it did post back. But even then, the value was still 0 for the index.

Why is this happening?

Thanks,

Brian Mains
__________________
Brian
 
Old November 20th, 2003, 11:49 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You should handle changes in a dropdownlist using the dropdownlist's eventhandler instead of checking in page load. It's possible you aren't seeing the change, because .Net hasn't checked the control to see if it has changed yet. I would think it would have, but the easy way to solve this is to use the event handler.

For the dropdownlist, you can specify "onSelectedIndexChange" in the control markup and specify the function to call. Inside the function you can then check the SelectedIndex and use SelectedItem and be assured that it will give you the item you actually selected.

From MSDN help:
<code>
      Sub Selection_Change(sender As Object, e As EventArgs)

         ' Set the background color for days in the Calendar control
         ' based on the value selected by the user from the
         ' DropDownList control.
         Calendar1.DayStyle.BackColor = _
             System.Drawing.Color.FromName(ColorList.SelectedIt em.Value)

      End Sub
</code>

<markup>
               <asp:DropDownList id="ColorList"
                    AutoPostBack="True"
                    OnSelectedIndexChanged="Selection_Change"
                    runat="server">

                  <asp:ListItem Selected="True" Value="White"> White </asp:ListItem>
                  <asp:ListItem Value="Silver"> Silver </asp:ListItem>
                  <asp:ListItem Value="DarkGray"> Dark Gray </asp:ListItem>
                  <asp:ListItem Value="Khaki"> Khaki </asp:ListItem>
                  <asp:ListItem Value="DarkKhaki"> Dark Khaki </asp:ListItem>

               </asp:DropDownList>
</markup>


Peter
------------------------------------------------------
Work smarter, not harder.
 
Old November 21st, 2003, 08:52 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

I do have a OnSelectedIndexChanged event defined for the drop down list. All I had in the page_load event was the data binding information. Any other ideas? Also, where do you specify the data binding information?

Thanks,

Brian
 
Old November 21st, 2003, 10:07 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I bind in page load based on Not IsPostback (usually I create a private method to do the binding to the control and call that from page load).

Is your bind conditional based on postback? If not you could have problems, because every time the page loads, you'll end up rebinding the control. When that happens, viewstate and postback data gets all messed up so the framework doesn't raise the correct events. I'll bet that's what's happening.

You only need to bind once (when Not IsPostback). The viewstate will remember what's in the select(and what value was previously selected. That's how it knows which events (OnSelectedIndexChanged) to raise.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old November 21st, 2003, 01:23 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Thanks, that solved the problem. I should have realized that, but I didn't.





Similar Threads
Thread Thread Starter Forum Replies Last Post
2 table views in DropDownList control yukijocelyn ASP.NET 2.0 Basics 31 October 1st, 2007 10:16 PM
Multi column Dropdownlist control??? MTLedari ASP.NET 2.0 Basics 6 June 7th, 2007 11:40 PM
Finding items in a dropdownlist control johnnycorpse Classic ASP Components 0 May 18th, 2007 04:09 AM
DropDownList in Header of GridView Control ... [email protected] ASP.NET 2.0 Professional 0 December 5th, 2006 12:20 AM
selected value from dropdownlist control netwizard_01 ASP.NET 1.0 and 1.1 Basics 3 January 20th, 2004 09:29 AM





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