Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ListBox control problem

Status
Not open for further replies.

roger9991

Programmer
Apr 18, 2009
23
US
I am used to Visual Foxpro and am trying to learn ASP.net 4. I am having problems with a bound listbox control. The DatasourceID = SqlDataSource1. The listbox shows all the all the rows of a certain field, but when I click on a single selection in the Listbox, nothing happens.
Then I tried changing the select command in the SqlDataSource1 DataControl and chose Control (ListBox1) from the parameter source drop down list. After this, the list box does not even get populated.
I am used to "wizards" in Foxpro and I found ASP.net difficult.
 
what do you expect to happen when you select an item in the list box? if you provide the code we can provide more assistance.

the more you depend on wizards to write code, then less control you have over the application. they are ok for quick and dirty, but if you want anything substantial, I would avoid wizards in favor of writing actual code.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
This is the listbox code:
</asp:GridView>
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource1" DataTextField="LASTNAME" DataValueField="ID"
Height="267px" Rows="20" Width="133px"></asp:ListBox>
</form>
 
First, don't use the datasource controls.
Second, you didn't write any code to handle the selectedindex changed event.
 
Also remember that FoxPro is a windows progamming language, ASP.NET is is for the web. They are very different and it takes time to get used to programming for the web vs windows.
 
this type of work is webforms 101. a google search will return plenty of results.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
I have searched thru Gooogle and obtained hundreds of lines of code, but it seems I only need a line or two of simple code which I cannot seem to find anywhere.
 
first you need to wire the handler to the event
Code:
<asp:ListBox Id="MyListBox" SelectedIndexChanged="Foo" ... />
or
Code:
protected override void OnLoad(EventArgs e)
{
   base.OnLoad(e);
   MyListBox.SelectedIndexChanged += Foo;
}
then you need to define the handler
Code:
protected void Foo(object sender, EventArgs e)
{
   your code goes here.
}

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Thank you, but "your code goes here." is what I did not know what to write. I am sorry but I am used to Foxpro and this is so different.
 
neither do we. this is where you put the code you want executed when an item is selected.

another note about all this: you will need to set autopostback = true on the listbox, otherwise the event handler will not fire.

if you are new to asp.net and webforms I would recommend learning how to use it. has some tutorials, there are also a plethora of books for sale on the topic.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
I have set autopostback = true on the listbox. I want to execute a select statement but the
" Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

End Sub"

does not allow me to enter a select statement to choose only the row on the list box that I clicked.
 
you wouldn't use the same datasource for the list box that you would use to query a single record. This concepts isn't webforms specific, it's common data access strategies.

research ado.net on how to query a database. the fact that you are designing webforms doesn't really matter.

not sure if it will help in your current situation since you are not familiar with asp.net or webforms, but you could read my faq on database connection management (see signature below).

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
I tried using ADO.net but it wants MS SQL database and I am using an Access database
 
yes, that would make perfect sense. sqlserver is not access. same a sqlserver is not oracle, or foxpro, mysql, sqlite or any other relational database. sql server is a specific implementation of a relational database. the sql server drivers a specific to sql server database. if you want to access an ms access database you need to use the oledb drivers.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top