Wednesday, March 21, 2012

listbox selected values

Hi. With VWD i've produced the following code.
<asp:SqlDataSourceID="SqlDataSource2"runat="server"ConnectionString="<%$ ConnectionStrings:50469ConnectionString %>"SelectCommand="SELECT * FROM [ibs] WHERE ([liedID] = @.liedID)">
<SelectParameters>
<asp:ControlParameterControlID="ListBox1"Name="liedID"PropertyName="SelectedValue"Type="Int16"/>
But the query is only returning one row of the table. Even when multiple values were selected in the ListBox1. Could someone tell me how to do?
Thanks, Kin Wei.

That's not really an easy thing to do. There are a number of "issues" to work around.
1) The WHERE clause would need to be changed to an equate to something like IN.
2) Using IN, you can't use a parameter to represent a list of values.
3) There is no way to get the list control to give you a comma delimited list of selected values.
You can get around the #2 issue by dynamically creating an executing SQL using the EXEC command like this:
DECLARE @.sql varchar(8000)
SET @.sql = 'SELECT * FROM ibs WHERE liedID IN (' + @.liedID + ')'
EXEC(@.sql)
You can get around #3 by building the string yourself by:
1)Use autopostbacks to keep track of the selected values, and store them in a hidden field or a session variable as a comma delimited string.
or
2)On postback, get the list of selectedindexes via listbox1.GetSelectedIndices like this:
dim mystring as string=""
for each x as Integer in listbox1.GetSelectedIndices
mystring &= ",'" & cstr(Listbox1.Items(x).Value) & "'"
next
mystring=mystring.trim(",")
HiddenField.text=mystring
|||Thanks for your post. But I'm not really good at this. Where in the code do I have to make the EXEC command?
<%@. Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:t ="urn:schemas-microsoft-com:time">
<head runat="server">
<?import namespace="t"
implementation="#default#time2">
<style>
.time {behavior: url(#default#time2);}
</style>
<title>IBSTEST</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="Panel1" runat="server" Height="50px" Width="125px" Visible="true">
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:50469ConnectionString %>"
SelectCommand="SELECT * FROM [ibs] ORDER BY [liedTitel]"></asp:SqlDataSource>
<asp:ListBox ID="ListBox1" runat="server" DataSourceID="SqlDataSource2" DataTextField="liedTitel"
DataValueField="liedID" Font-Names="Arial" Font-Size="X-Small" Height="300px"
SelectionMode="Multiple" Width="350px"></asp:ListBox><br />
<br />
<asp:Button ID="Button1" runat="server" Text="Play" OnClick="Button1_Click" Height="25px" Width="75px" /></asp:Panel>
<br />
<asp:Panel ID="Panel2" runat="server" Height="50px" Width="349px" Visible="false">
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:50469ConnectionString %>"
SelectCommand="SELECT * FROM [ibs] WHERE ([liedID] = @.liedID) ORDER BY NEWID()">
<SelectParameters>
<asp:ControlParameter ControlID="ListBox1" Name="liedID" PropertyName="SelectedValue"
Type="Int16" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<HeaderTemplate>
<t:seq repeatCount="1">
</HeaderTemplate>
<ItemTemplate>
<t:par>
<t:audio dur='<%# DataBinder.Eval(Container.DataItem, "liedDuur") %>s' src='<%# DataBinder.Eval(Container.DataItem, "liedID") %><%# DataBinder.Eval(Container.DataItem, "liedExtensie") %>' type='<%# DataBinder.Eval(Container.DataItem, "liedType") %>' />
<div dur='<%# DataBinder.Eval(Container.DataItem, "liedDuur") %>s' class="time" timeAction="display">
Titel: <%# DataBinder.Eval(Container.DataItem, "liedTitel") %><br />
Artiest: <%# DataBinder.Eval(Container.DataItem, "liedArtiest") %></div>
<img dur='<%# DataBinder.Eval(Container.DataItem, "liedDuur") %>s' class="time" timeAction="display" src='<%# DataBinder.Eval(Container.DataItem, "liedImage") %>.jpg' />
</t:par>
</ItemTemplate>
<FooterTemplate>
</t:seq>
</FooterTemplate>
</asp:Repeater>
<br />
<br />
<asp:Button ID="Button2" runat="server" Height="25px" OnClick="Button2_Click" Text="Stop"
Width="75px" /></asp:Panel>
<br />
</div>
</form>
</body>
</html>
and the .cs code is
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Panel1.Visible = false;
Panel2.Visible = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
Panel1.Visible = true;
Panel2.Visible = false;
}
}
After your post, I think it's clear what have to be done. But I don't know where to put the code.
Thank you very much,
Kin Wei

No comments:

Post a Comment