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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bind data to a multiselect listbox

Status
Not open for further replies.

shanebrennan1968

Programmer
Jan 22, 2009
10
GB
Hi everyone, I am extremely new to ASP.NET, but I'm learning fast - but I've hit an small problem, and I would appreciate any advice on how to move forward with a small issue.

I have some client details displayed using a FormView which I have bound to a DAL and BLL sucessfully and I am able to store and update info. But not I want to expand 1 of the fields called "GeoArea".

Currently I have a listbox that lists all the available geographic aread (from another data source) and I can easily select 1 field then update the changes. Now I want to expand this to allow me to select multiple regions that this client deals with. I've set the ListBox control to multi-select and it works great, but only remembers the last selected item.

So my question is what is the best way forward in achieving this?
 
you need to loop through the listbox and get the selecteded items
Code:
private IEnumerable<string> SelectedKeys()
{
   foreach(var item in MyListBox.Items)
   {
      if(!item.Selected) continue;
      yield return item.Key;
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks Jmeckley

How would I use this to store the results? Is it just a case of changeing the binding?

Thanks in advance for any help.
 
What do you mean store the results? NOt sure what you are trying to accomplish
 
use this function to pass all the selected keys to your business layer. if you're using a datasource control this will prove difficult. your best bet is to drop the datasource control and bind the results manually

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Basically I have an SQLExpress database called FRA and a bunch of tables one of which is called tblClients. I need a way of identifying which Geographic areas these clients operate in.

Currently I have a field called MainArea - an int - and I use a Combobox to select 1 area - but I'm tying to expand this as many of the clients operate in more than 1 geographic area.

I've setup a basic BLL that I use to populate the list box:

Imports Microsoft.VisualBasic

Imports FRATableAdapters

Public Class GeographicAreasBLL
Private _tblGeographicAreaAdapter As tblGeographicAreaTableAdapter = Nothing
Protected ReadOnly Property Adapter() As tblGeographicAreaTableAdapter
Get
If _tblGeographicAreaAdapter Is Nothing Then
_tblGeographicAreaAdapter = New tblGeographicAreaTableAdapter()
End If
Return _tblGeographicAreaAdapter
End Get
End Property

<System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, True)> _
Public Function GetAllGeographicAreas() As FRA.tblGeographicAreaDataTable
Return Adapter.GetAllGeographicAreas()
End Function
End Class

/------------------------

The <b>GetAllGeographicAreas</b> is just a TableAdaptor query in the DAL's .xsd file:

SELECT ID, GeoArea
FROM tblGeographicArea
ORDER BY GeoArea

My ClientDetails.aspx page is:

<%@ Page Language="VB" MasterPageFile="~/Site.master" AutoEventWireup="false" CodeFile="ClientDetails.aspx.vb" Inherits="Clients_ClientDetails" title="Untitled Page" %>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:FormView ID="FormView1" runat="server"
DataSourceID="ClientsDataSource" DataKeyNames="ID" DefaultMode="Edit">
<EditItemTemplate>
<table class="DetailsTable">
<tr>
<td>ID:</td>
<td><asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>' /></td>
</tr>
<tr>
<td>Name:</td>
<td><asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' /></td>
</tr>
<tr>
<tr>
<td>Geographic Area:</td>
<td>
<asp:ListBox ID="ListBox1" runat="server"
DataSourceID="GeographicAreasDataSource" DataTextField="GeoArea"
DataValueField="ID"
selectedIndex ='<%# Bind("Area") %>'
AppendDataBoundItems="True" SelectionMode="Multiple">
<asp:ListItem Value="">(None)</asp:ListItem>
</asp:ListBox>
<asp:ObjectDataSource ID="GeographicAreasDataSource" runat="server"
InsertMethod="Insert" OldValuesParameterFormatString="original_{0}"
SelectMethod="GetAllGeographicAreas"
TypeName="FRATableAdapters.tblGeographicAreaTableAdapter">
<InsertParameters>
<asp:parameter Name="GeoArea" Type="String" />
</InsertParameters>
</asp:ObjectDataSource>
</td>
</tr>

/-----------------------------------


Now I'm stuck and my knowledge of ASP.NET is limited.

In access I could:

1) Change the MainArea field to a memo or string and use a couple of functions to parse the string to select/save the options in a multi-select combo.
2) Create a second table called "ClientGeoArea" and again use a couple of functions to populate a combo and save the results back into the table.

Basically I need help... :(

Sorry if I've got any terminology wrong - just starting ASP.NET - and trust me to get stuck on my 2nd page.

I've managed to get my User and role access up and running - what I thought was going to be that hard bit - and now I've got stuck on a "simple" ListBox.

Again thanks inadvance of any help & guidance given.
 
ObjectDataSource. this is your problem. you have very little control over how to call the middle tier and it's a pain to alter when changes are required.

scrap the datasource control altogether and code this up yourself. then you have full control over classes and signatures. and you don't need to pollute your business layer with presentation attributes.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
i mean object/sql/odbc/kitchen sink/ datasource controls are terrible controls. delete the object datasource control form the form and wire the form events manually using your code.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks for the Advice Jason.

Do you have, or can guide me, an example of how to retrieve a data from a DB table, bind it to a couple of controls, then do the update?
 
this is .net 101. there are example all over the internet. search for bind collection/datatable to [name of my control]. if the example uses a datasource control move on to the next result.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hi Jason

Sorry I am now confused more than ever :(

I can see what you mean about NOT using the ObjectDataSource after more research about it and reading quite a lot comments it does seem to be as useful as a bag of nails in a balloon factory.

When you say "if the example uses a datasource control move on to the next result." are you refering to the ObjectDataSource? as the only other examples of data retrieval and storeage I can see is to use one of the connectors such as "SqlDataSource". This control looks much better as it gives the programmer a lot more control.

I know I may seem quite thick here but I am completely new to this. I can program the socks off MS Access and VB6, I've even written control system using Assembly language about 14 years ago on the old 8086-30386 processors. But the dotnet framework is completely new to me and it's taking me a long time to get my head around this stuff. Probably my age :)

I know once it "clicks" I'll be away, but until then I'll keep taking the prozac.

Again thanks for your time and patience.

Shane Brennan
 
languages are just syntax. if you can program vb6, vba, etc, then you can program c#, ruby, php, the sysntax is different, but the oop concepts are the same.

now the markup syntax that winforms/webforms uses is very different because you are using xml to define code. which is really objnoxious, in my opinion.

when I talk about next results i'm talking about googling for the answer about binding data to a formview. here is pseudo code
Code:
//in the page load event
if ispostback return;
myformview.datasource = new service().getdataforformview(arguments...);
databind();

//the service object
class service
{
   public DataTable (arguments...)
   {
       results = new datatable();
       using(databaseconnection)
       {
            databaseconnection.open();
            using(databasecommand = databaseconnection.createcommand())
             {
                  databasecommand.text = "sql statememt";
                  databasecommand.addparameterwithvalue("key", value from arugments);
                   //repeat for each parameter
                  results.Load(databasecommand.executereader());
             }
       }
       return results;
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top