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

Multiple selecton for List box

Status
Not open for further replies.

sim133

Technical User
Sep 14, 2004
85
US
Hi all,
How do you do to make your lisxbox control to display your multiple selection at the same time. when i make a single selection it displays the the result but when i do more than on selection it only grabs the result for the first selected field and ignore the second one. for example when i select both MN and WI I am only getting the out put for MN. I am working with Visual Studio 2005 and oracle 9i DB. thanks for the help
I did set the selection mode to Multiple but to no avail.
I appreciate it. Below is the Code.
Code:
%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ListBox ID="ListBox1" runat="server" DataSourceID="SqlDataSource1" DataTextField="CONTID"
            DataValueField="CONTID" SelectionMode=Multiple></asp:ListBox>
        <asp:Button ID="Button1" runat="server" Text="Button" />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
            ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand='SELECT DISTINCT "CONTID" FROM "ADDEND"'>
        </asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ADDEND,CONTID"
            DataSourceID="SqlDataSource2">
            <Columns>
                <asp:BoundField DataField="ADDEND" HeaderText="ADDEND" ReadOnly="True" SortExpression="ADDEND" />
                <asp:BoundField DataField="CONTID" HeaderText="CONTID" ReadOnly="True" SortExpression="CONTID" />
                <asp:BoundField DataField="DATEADD" HeaderText="DATEADD" SortExpression="DATEADD" />
                <asp:BoundField DataField="ADESCR" HeaderText="ADESCR" SortExpression="ADESCR" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
            ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand='SELECT DISTINCT "ADDEND", "CONTID", "DATEADD", "ADESCR" FROM "ADDEND" WHERE ("CONTID" = :CONTID)'>
            <SelectParameters>
                <asp:ControlParameter ControlID="ListBox1" Name="CONTID" PropertyName="SelectedValue"
                    Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
    
    </div>
    </form>
</body>
</html>
 
Where are you trying to get the values, in a button click event? If so, you have to loop through the items in the listbox, and check the selected property to see if it is selected.

Jim
 
This wasnt easy...but ive got a "work around" unless someone can give a solution for multiple paramtered listboxes! Seems the Control Parameter can have only one value at any time. so with a little twisting i got my MSSQL working, hopefully you can transpose this to your situation.

Code:
    <script runat="server">
        Sub getSel(sender As Object, e As EventArgs)
            Dim item As ListItem
            Dim strCC As String
            For Each item In txtVals.Items
                If item.Selected Then
                    strCC = strCC & item.Value & ","
                End If
            Next item
            txtVal.Text = strCC.Substring(0, Len(strCC) - 1)
        End Sub
    </script>

<asp:DataList ID="dlLinks" runat="server" Width="125" RepeatLayout="table" RepeatDirection="vertical"
    RepeatColumns="1" DataSourceID="ds1">
    <ItemTemplate>
        <a href='<%# DataBinder.Eval(Container.DataItem,"linkURL") %>' target="_parent" class="hyper">
            <%# DataBinder.Eval(Container.DataItem , "linkName") %>
        </a>
    </ItemTemplate>
</asp:DataList>

<asp:ListBox ID="txtVals" runat="server" SelectionMode="multiple" AutoPostBack="true"
    OnSelectedIndexChanged="getSel">
    <asp:ListItem Value="1">1</asp:ListItem>
    <asp:ListItem Value="2">2</asp:ListItem>
    <asp:ListItem Value="3">3</asp:ListItem>
</asp:ListBox><br />
<asp:TextBox ID="txtVal" runat="server" Text="1" visible=false></asp:TextBox>

<asp:SqlDataSource ID="ds1" runat="server" SelectCommand="exec('select * from tblLinks where linkOrder IN (' + @listing + ')')"
    SelectCommandType="Text" ConnectionString="your con string">
    <SelectParameters>
        <asp:ControlParameter ControlID="txtVal" Name="listing" Type="string" PropertyName="Text" />
    </SelectParameters>
</asp:SqlDataSource>


im not sure how u control the postbacks yet in 2.0, but..

the initial text value of 1 is set on txtVal so on page load, the db was activated. when i clicked multiple values in the list box, it posted back, (autopostback=true) the text was changed via the getSel() sub and the datalist was correctly updated.

you cannot put clauses in the parameter of MSSQL statement without error (the param cannot be "WHERE 1=2", an "Incorrect Syntax" results), so i used the EXEC(string) statement, this is where i dont know how Oracle will work

="exec('select * from tblLinks where linkOrder IN (' + @listing + ')')"

so that way the whole string is sent into the query, and using IN() allowed me to send in an "array" of sorts as opposed to where 1=2 and 1=3 and 1=2, but you could do that for each of your 50 states i guess within the getSel() sub

anyway, i hope this gets u going and let me know if u need more info on how i got my code working.

good luck.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top