Results 1 to 5 of 5

Thread: Listbox control not preserving selected item on button click.

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Listbox control not preserving selected item on button click.

    Ok so I have two listboxes on my page one for users and another for cases. For some reason the users listbox preserves state, but the cases listbox doesn't.

    They're both set EnableViewState= true

    Code:
                    <asp:ListBox ID="ExistingUsers" runat="server" Width="45%" Height="189px" 
                        CssClass="createdusers" ></asp:ListBox>
                    <asp:ListBox ID="ExistingCases" runat="server" Width="45%" Height="190px" 
                        ></asp:ListBox>
    I tried setting them both to autopostback, but that didn't help. The users listbox would preserve the selected item, but make it the first viewable item in the list. The cases listbox still reset the selected item to the first.

    My page load:

    Code:
    '....
    
            If Not Page.IsPostBack Then
                ''Sort later.
                Dim i As Integer = 10
                For Each usr As String In AttRoleProvider.GetUsersInRole("Role1")
                    Me.ExistingUsers.Items.Add(usr)
                Next
            End If
    
            If Not Page.IsPostBack Then
                Dim ds As DataSet = dbops.ExecuteSQLServerSelectCommand(SQL, Nothing)
                If ds IsNot Nothing Then
                    Dim dt As DataTable = ds.Tables(0)
                    Me.ExistingCases.DataSource = dt
                    Me.ExistingCases.DataTextField = "Text"
                    Me.ExistingCases.DataValueField = "Value"
                    Try
                        Me.ExistingCases.DataBind()
                    Catch ex As Exception
                        Throw ex
                    End Try
                End If
            End If
    
    '....
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Listbox control not preserving selected item on button click.

    Hmmm so it seems that if you bind the DataTextField and the DataValueField that the control will not maintain the selected state. That value does make it easier than have to query again based on the selected text. Is there a way to get this to work with value-binding?
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  3. #3
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: Listbox control not preserving selected item on button click.

    Hi,

    I tested your .aspx markup and created some dummy .cs methods which add items through item collection for users
    and List object with class (with properties Id and Name) as it's type somewhat similar to a data table object.

    I only used one condition to test the if the page state is IsPostBack. I noticed in your code in which your using separate conditions for testing IsPostBack.

    Page load condition using explicit setting and not from a database result.
    Code:
    If Not Page.IsPostBack Then
    
    
        For i As Integer = 0 To 3
            users.Add("user" & i)
            Me.ExistingUsers.Items.Add("user" & i)
            roles.Add(New Roles() With { _
                Key .ID = i, _
                Key .Name = "Role" & i _
            })
        Next
    
    
        Me.ExistingCases.DataSource = roles
        Me.ExistingCases.DataTextField = "Name"
        Me.ExistingCases.DataValueField = "Id"
        Me.ExistingCases.DataBind()
    
    
        Session("users") = users
        Session("roles") = roles
    End If
    button event:
    Code:
    Protected Sub btnShowSelected_Click(sender As Object, e As EventArgs)
        If Me.ExistingCases.SelectedItem IsNot Nothing AndAlso Me.ExistingUsers.SelectedItem IsNot Nothing Then
            Response.Write("User: " & Me.ExistingUsers.SelectedItem.ToString() & ", Role: " & Me.ExistingCases.SelectedItem.ToString())
        End If
    End Sub
    aspx markup
    Code:
    <div>
            <div class="container">
                <div id="Users">
                     <asp:ListBox ID="ExistingUsers" runat="server" EnableViewState="true" Width="150px" Height="189px" ></asp:ListBox>
                </div>
                <div id="Cases">
                    <asp:ListBox ID="ExistingCases" runat="server"  EnableViewState="true"  Width="150px" Height="190px"></asp:ListBox>
                </div>
                 <div id="footer">
                    <asp:Button ID="btnShowSelected" runat="server" Text="Submit" 
                         onclick="btnShowSelected_Click" />
                </div>
            </div>       
        </div>
    The state of the two listbox controls are preserved even on postbacks. Try checking your markup as well. Are you using ajax/update panels or plain server side asp.net?

    Possible issue is that you set autopostback property of the listbox control.
    Last edited by KGComputers; Mar 27th, 2014 at 06:24 AM.
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Listbox control not preserving selected item on button click.

    I"ll test the cases listbox with a custom class with the same property names as the db fields I'm binding to. I'll see if that has a different outcome than binding a datatable.

    I'm positive I have the autopostback equal to false on both controls in the properties panel. I'm not using any ajax for this web app.

    Thank you for the response.
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  5. #5
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: Listbox control not preserving selected item on button click.

    I believe using a List<YourType> or datatable has nothing to do with the issue. It can be the aspx markup or some logic behind your app.
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width