Hi all, I have a dropdownlistbox that filldynamically on the page load method. however on postback the box clears back to its default value, i have tried probably everything, even using code from a page that i do have working. My latest effort is using page.ispostback with the page load event, If Not IsPostBack Then Dim dReader As System.Data.SqlClient.SqlDataReader Dim con As SqlConnection Dim com As SqlCommand con = New SqlConnection("Data Source=TWEEDLEDEE\SQLEXPRESS;Initial Catalog=mp3;Integrated Security=True") com = New SqlCommand("SELECT DISTINCT Artist FROM Table_1 ORDER BY Artist", con) con.Open() dReader = com.ExecuteReader() ListBox1.DataSource = dReader ListBox1.DataTextField = "Artist" ListBox1.DataValueField = "Artist" ListBox1.DataBind() dReader.Close() con.Close() End If Any help on this issue would be much appreciated Billy
Thanks for this, unfortunately the EnableViewState is True, with no effect, the control is still filling with the default value. I really dont know what else to try, could this be a db issue? Cheers
I've seen this before. Give the drop down box it's own subroutine on submit, like this: <asp:DropDownList id="ListBox1" runat="server" /> <asp:button id="myListBox1" text="Click" OnCommand="listbox_update" runat="server" /> Code (markup): Then perform it's functions here: Sub listbox_update(sender As Object, e As system.Web.UI.WebControls.CommandEventArgs) 'DO SOMETHING WITH myListBox1.SelectedItem.Value End Sub Code (markup):
Thanks for this, I have tried this and understand the thinking (which all makes perfect sense, even to me) however the selectedvalue, is clearing on post back reverting to default values. Cheers Billy
Here is the asp code: 1 <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" enableviewstate = "true" 2 enableEventValidation="false" Inherits="_Default" %> 3 4 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 5 6 <html xmlns="http://www.w3.org/1999/xhtml"> 7 <head runat="server"> 8 <title>Untitled Page</title> 9 </head> 10 <body> 11 <form id="form1" runat="server"> 12 <div> 13 14 <aspropDownList ID="DropDownList1" runat="server"> 15 </aspropDownList> 16 <br /> 17 <asp:Button ID="Button1" runat="server" Text="Button" /> 18 <br /> 19 <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 20 21 </div> 22 </form> 23 </body> 24 </html> 25 and the complete vb code: 1 Imports System.Data.SqlClient 2 3 Partial Class _Default 4 Inherits System.Web.UI.Page 5 6 7 8 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 9 If Not IsPostBack Then 10 Dim dReader As System.Data.SqlClient.SqlDataReader 11 Dim con As SqlConnection 12 Dim com As SqlCommand 13 con = New SqlConnection("Data Source=TWEEDLEDEE\SQLEXPRESS;Initial Catalog=mp3;Integrated Security=True") 14 com = New SqlCommand("SELECT DISTINCT artist FROM table_1 ORDER BY artist", con) 15 con.Open() 16 dReader = com.ExecuteReader() 17 DropDownList1.DataSource = dReader 18 DropDownList1.DataTextField = "artist" 19 DropDownList1.DataValueField = "artist" 20 DropDownList1.DataBind() 21 dReader.Close() 22 con.Close() 23 24 End If 25 26 Label1.Text = DropDownList1.SelectedValue 27 End Sub 28 29 End Class Hope this helps. Many thanks (Again!!)Billy
Hi all, Again Just discovered something interesting, the control now seems to work but only with longer named records e.g. it will reset on "ac\dc" but will behave normally on "bruce springsteen and the e street band" it does not seem to be related to spaces within the record. (had not noticed this before!) Anyone have any ideas? Cheers
Hi all, Sorted!!!, Just to let you all know that i got this sorted. The problem was the routine i was using to rip the id3 data from my mp3's. The routine returned strings of 30 characters in length, those longer than that it truncated them > no problem but those shorter than that in made them the same length (for some reason, SQL\asp\ddl didnt recognise the blanks at the end of the strings thus sending the ddl back to mama. Once i sussed this it was just a case of stripping the blanks from the end of the strings using a simple InStrRev routine and bingo!!! Thanks for all your inputs, i certainly have learned a lot Cheers Billy