[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

ASP.NET Listbox

Posted on 2009-07-14
18
Medium Priority
?
307 Views
Last Modified: 2012-06-21
Morning,

I have a problem with the listbox control where I have the following code in a button but for some strange reason it will not place the selected item that I have clicked on into the textbox.

Any ideas?
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        TextBox1.Text = "You Selected" & " " & ListBox1.SelectedItem.Text
 
    End Sub

Open in new window

0
Comment
Question by:richard_gar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 3
  • +1
18 Comments
 
LVL 11

Expert Comment

by:Salim Fayad
ID: 24847317
Can you post the code which is in the Page_Load event?
What I guess, is that there is some code on the Page_Load that is setting the TextBox1.Text value each time the page loads.
0
 

Author Comment

by:richard_gar
ID: 24847330
here you go
        Dim conn As New SqlConnection(cp.pConnectionString)
        Dim cmd As New SqlCommand("SELECT * FROM tblSites", conn)
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet("myDataSet")
        da.Fill(ds)
        ListBox1.DataSource = ds
        ListBox1.DataValueField = ds.Tables(0).Columns("companyid").ColumnName
        ListBox1.DataTextField = ds.Tables(0).Columns("sitename").ColumnName
        ListBox1.DataBind()

Open in new window

0
 
LVL 16

Expert Comment

by:sunithnair
ID: 24847369
Have you put this code in Page_Load inside IsPostBack like this?

If Not IsPostBack Then
'Your code
EndIf
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:udaydidigam
ID: 24847398
put these statements in pageload event like this

If  Not Page.IsPastBack
{
Dim conn As New SqlConnection(cp.pConnectionString)
        Dim cmd As New SqlCommand("SELECT * FROM tblSites", conn)
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet("myDataSet")
        da.Fill(ds)
        ListBox1.DataSource = ds
        ListBox1.DataValueField = ds.Tables(0).Columns("companyid").ColumnName
        ListBox1.DataTextField = ds.Tables(0).Columns("sitename").ColumnName
        ListBox1.DataBind()
}
0
 

Author Comment

by:richard_gar
ID: 24847414
yeah I have done that and it is working but my textbox is always showing the first item in the list and not the others when I click on them
0
 
LVL 16

Expert Comment

by:sunithnair
ID: 24847468
Are you setting the SelectedItem somewhere else on the page?
0
 
LVL 4

Expert Comment

by:udaydidigam
ID: 24847486
put the auto post back property true for listbox control
0
 

Author Comment

by:richard_gar
ID: 24847507
below is the whole code of the page.

Also I tried setting the postback to true and that makes no difference
Imports System.Data.SqlClient
Imports System.Data
 
 
Partial Class salesreport
    Inherits System.Web.UI.Page
 
    Dim cp As New Class1
    Dim cnnConnection As New SqlClient.SqlConnection(cp.pConnectionString)
 
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        If Not Page.IsPostBack Then
 
            Dim conn As New SqlConnection(cp.pConnectionString)
            Dim cmd As New SqlCommand("SELECT * FROM tblSites", conn)
            Dim da As New SqlDataAdapter(cmd)
            Dim ds As New DataSet("myDataSet")
            da.Fill(ds)
            ListBox1.DataSource = ds
            ListBox1.DataValueField = ds.Tables(0).Columns("companyid").ColumnName
            ListBox1.DataTextField = ds.Tables(0).Columns("sitename").ColumnName
            ListBox1.DataBind()
 
        End If
 
    End Sub
 
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        Label1.Text = ListBox1.SelectedItem.Text
 
    End Sub
 
End Class

Open in new window

0
 
LVL 16

Expert Comment

by:sunithnair
ID: 24847575
I dont see a problem with your code. It should work as expected.
0
 
LVL 11

Expert Comment

by:Salim Fayad
ID: 24847622
Can you post the aspx code (not the aspx.cs)?
0
 

Author Comment

by:richard_gar
ID: 24848645
the html code in the form
<%@ Page Title="" Language="VB" MasterPageFile="~/Default.master" AutoEventWireup="false" CodeFile="salesreport.aspx.vb" Inherits="salesreport" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="MainContentPlaceHolder" Runat="Server">
                            <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
                            <br />
                            <br />
                            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                            <br />
                            <br />
                            <asp:Button ID="Button1" runat="server" Text="Button" />
                            <br />
</asp:Content>

Open in new window

0
 
LVL 4

Expert Comment

by:udaydidigam
ID: 24848978
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        If Not Page.IsPostBack Then
 
            Dim conn As New SqlConnection(cp.pConnectionString)
            Dim cmd As New SqlCommand("SELECT companyid,sitename FROM tblSites", conn)
            Dim da As New SqlDataAdapter(cmd)
            Dim ds As New DataSet("myDataSet")
            da.Fill(ds)
            ListBox1.DataSource = ds.Tables[0]
            ListBox1.DataValueField = "companyid"
            ListBox1.DataTextField = "sitename"
            ListBox1.DataBind()
 
        End If
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        Label1.Text = ListBox1.SelectedItem.Text
 
    End Sub
 
End Class

This works fine for me

   

 
0
 

Author Comment

by:richard_gar
ID: 24849451
I have copied your code and it is still not working for me.
0
 

Author Comment

by:richard_gar
ID: 24849746
I have just setup another form with just that code and it is still not working for me.

I am using IE8 and I have checked with the compatability mode and still not working.

I am using .net 3.5 too
0
 
LVL 11

Expert Comment

by:Salim Fayad
ID: 24887180
I also tried it and it is working. But what I noticed is that you are using a MasterPage. Can you post the code of the Page_Load of the MasterPage? And if there is any code in the Page or MasterPage in the OnInit event?
0
 

Author Comment

by:richard_gar
ID: 24893036
here is the code in the master page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        'If user has logged in to page direct then kick them to login page
        If IsNothing(Session("username")) Then
            Response.Redirect("login.aspx")
        End If
 
        lblDate.Text = System.DateTime.Now.ToLongDateString
        lblIP.Text = Request.UserHostAddress
        lblUser.Text = Session("username")
        lblLastLogin.Text = Session("lastlogin")
 
        Dim strSQL As String
        strSQL = "SELECT * FROM tblCompany WHERE companyid = '" & Session("companyid") & "'"
        Dim ds As New DataSet
        SQLSelect(ds, strSQL)
        lblCompany.Text = ds.Tables(0).Rows(0)("companyname")
        ds.Dispose()
 
        Dim strSQL1 As String
        strSQL1 = "SELECT * FROM tblSites WHERE companyid = '" & Session("companyid") & "'"
        Dim ds1 As New DataSet
        SQLSelect(ds1, strSQL1)
        lblSites.Text = ds1.Tables(0).Rows.Count
        ds1.Dispose()
 
    End Sub

Open in new window

0
 
LVL 11

Accepted Solution

by:
Salim Fayad earned 2000 total points
ID: 24896880
Rewrite it as following:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        'If user has logged in to page direct then kick them to login page
        If IsNothing(Session("username")) Then
            Response.Redirect("login.aspx")
        End If
 
 If Not IsPostBack Then
        lblDate.Text = System.DateTime.Now.ToLongDateString
        lblIP.Text = Request.UserHostAddress
        lblUser.Text = Session("username")
        lblLastLogin.Text = Session("lastlogin")
 
        Dim strSQL As String
        strSQL = "SELECT * FROM tblCompany WHERE companyid = '" & Session("companyid") & "'"
        Dim ds As New DataSet
        SQLSelect(ds, strSQL)
        lblCompany.Text = ds.Tables(0).Rows(0)("companyname")
        ds.Dispose()
 
        Dim strSQL1 As String
        strSQL1 = "SELECT * FROM tblSites WHERE companyid = '" & Session("companyid") & "'"
        Dim ds1 As New DataSet
        SQLSelect(ds1, strSQL1)
        lblSites.Text = ds1.Tables(0).Rows.Count
        ds1.Dispose()
 
 End If
    End Sub

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question