I have a page with a listbox and a button. I want to have the ability to select multiple values from the listbox and pass them to another page using the button. How is that done? Then the gridview will have multiple values in it.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="lstCrews" runat="server" AutoPostBack="true" SelectionMode="Multiple"
>
<asp:ListItem Text="Q" Value="Argentina"></asp:ListItem>
<asp:ListItem Text="R" Value="Austria"></asp:ListItem>
<asp:ListItem Text="S" Value="Belgium"></asp:ListItem>
<asp:ListItem Text="T" Value="Brazil"></asp:ListItem>
</asp:ListBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>
Button VB.NEt
Partial Class listbox
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim str() As String = New String((6) - 1) {}
Dim i As Integer = 0
Do While (i < lstCrews.Items.Count)
If (lstCrews.Items(i).Selected = True) Then
str(i) = lstCrews.Items(i).Value
Else
str(i) = "0"
End If
i = (i + 1)
Loop
Session("ListBoxValues") = str
Response.Redirect("AddOvertimeHours.aspx?Crews=" + lstCrews.Text)
End Sub
End Class
Private Sub BindGrid()
Dim SelectCrew As String = Request.QueryString("Crews")
Dim cmd As New SqlCommand("SELECT ID, Name,Username,Bldg,Crew,PlantDate,'1/1/1900' As DateEntered,'Regular' as OvertimeType,'No' as AskType, 0 as ChargedHours, 0 as WorkedHours, '' as Comments FROM t_Employee_OT Where Crew='" & SelectCrew & "'")
gvOT.DataSource = Me.ExecuteQuery(cmd, "SELECT")
gvOT.DataBind()
End Sub
Private Function ExecuteQuery(cmd As SqlCommand, action As String) As DataTable
Dim conString As String = ConfigurationManager.ConnectionStrings("CG_ITConnectionString").ConnectionString
Using con As New SqlConnection(conString)
cmd.Connection = con
Select Case action
Case "SELECT"
Using sda As New SqlDataAdapter()
sda.SelectCommand = cmd
Using dt As New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
Case "UPDATE"
con.Open()
cmd.ExecuteNonQuery()
con.Close()
Exit Select
End Select
Return Nothing
End Using
End Function