Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET 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 January 16th, 2004, 04:09 PM
Authorized User
 
Join Date: Jan 2004
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to rathbird
Default selected item in dropdown not saved on postback

My dropdowns which are populated from db are not saving when form is submitted...all other fields working correctly AND all dropdowns not populated with db working correctly. View source on client-side shows dropdowns with correct values.

code to populate dropdown on code-behind page:

'set room dropdown
        Dim myReader As SqlDataReader
        SqlConnection1.Open()
        myReader = SqlCommand5.ExecuteReader
        room.DataSource = myReader
        room.DataBind()
        myReader.Close()

code on aspx page for dropdown:

<asp:dropdownlist id="room" runat="server" DataValueField="RoomID" DataTextField="Name" DataMember="Rooms" CssClass="small"></asp:dropdownlist>

code to try and insert dataselection from dropdown:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        SqlConnection1.Open()
        SqlCommand1.Parameters.Item("@room").Value = room.SelectedItem.Value
        SqlCommand1.ExecuteNonQuery()
        SqlConnection1.Close()
End Sub

when executing code, i get no errors, when stepping through, value on room.selecteditem.value is always 1 no matter what selection I make. I inserted an onselectionchanged event to fire when a new item was picked from the dropdown, but it never fires, although the new selection is shown client-side.

this is driving me crazy! any ideas???? anyone?

 
Old January 16th, 2004, 04:15 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Most likely what's happening is that you are databinding the dropdownlist every time the page loads. This is not necessary with .net. The ASPX will save all the dropdown list items in viewstate and "reconstruct" the list when you post the form. Part of this reconstruction is setting which item is selected based on the posted information. When you bind each time the page loads you are causing the item you selected to be ignored. You need to bind the control only once, based on the postback state of the page.

If Not IsPostback Then
    'Databind the dropdownlist here
End If

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old December 20th, 2004, 09:27 AM
Registered User
 
Join Date: Dec 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to benvon
Default

I am seeing this same problem.

I have set my code inside the "if not ispostback...", but it still behaves the same way.

I have put this code in the Page_Load and dropDownList_Init with the same results in both places.

Any other hints?

Thanks

 
Old March 6th, 2006, 08:44 AM
Registered User
 
Join Date: Mar 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am experiencing the same problem. I am looking to retrieve the selecteditem of a dropdownlist, but none of the dropdownlist information is being returned on postback. Any ideas?






Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 5 GridView Selected Item stoverje BOOK: Professional ASP.NET 2.0 Design: CSS, Themes, and Master Pages ISBN: 978-0-470-12448-2 2 August 8th, 2008 03:11 PM
How to get selected item from listview? try.test.abc C# 1 January 4th, 2007 02:34 AM
How to set selected item in Dropdownlist? MTLedari ASP.NET 2.0 Basics 4 December 27th, 2006 12:53 AM
How to get selected Item from Listbox? Iceman571 ADO.NET 1 March 25th, 2004 06:33 AM
Getting selected item in dropdown to match XML juliaPGATOUR XSLT 1 December 11th, 2003 05:56 AM





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