1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Dropdownlistbox resetting on postback

Discussion in 'C#' started by toggy, Apr 20, 2008.

  1. #1
    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
    SEMrush
     
    toggy, Apr 20, 2008 IP
    SEMrush
  2. entwickler

    entwickler Member

    Messages:
    75
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #2
    set the EnableViewState property of the control to true
     
    entwickler, Apr 20, 2008 IP
  3. toggy

    toggy Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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
     
    toggy, Apr 21, 2008 IP
  4. itcn

    itcn Active Member

    Messages:
    795
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    68
    #4
    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):
     
    itcn, Apr 21, 2008 IP
  5. toggy

    toggy Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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
     
    toggy, Apr 22, 2008 IP
  6. itcn

    itcn Active Member

    Messages:
    795
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    68
    #6
    You've got something else effecting it then - can you paste the entire code?
     
    itcn, Apr 22, 2008 IP
  7. toggy

    toggy Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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 <asp:DropDownList ID="DropDownList1" runat="server">
    15 </asp:DropDownList>
    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
     
    toggy, Apr 22, 2008 IP
  8. toggy

    toggy Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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
     
    toggy, Apr 22, 2008 IP
  9. toggy

    toggy Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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
     
    toggy, Apr 23, 2008 IP
  10. itcn

    itcn Active Member

    Messages:
    795
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    68
    #10
    Sorry I didn't reply earlier, but I'm glad you sorted it out!
     
    itcn, Apr 27, 2008 IP