This is a discussion on How i will get the selected value from Listbox( if selection mode=multiple) within the C# Programming forums, part of the Software Development category; Hi All, How i will get the selected value from Listbox( if selection mode=multiple)..? please.....Could u tell me ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Hi All, How i will get the selected value from Listbox( if selection mode=multiple)..? please.....Could u tell me anybody... Thanx in Advance Ramesh Kumar M |
| Sponsored Links |
| |||
| Hi i am using one Listbox ctrl created our own server ctrl - just inherited the ctrl property selection mode=multiple and binding the data but after submiting the value i am not getting the selected values the code i am used is Code: listboxctrl name - dlistHotelFacilities
Dim hotellist As ListItem
For Each hotellist In dlistHotelFacilities.Items
If hotellist.Selected = True Then
Response.Write(hotellist.Text & "
")
End If
Next Thanks Deeban Last edited by a.deeban : 02-07-2008 at 04:17 AM. |
| |||
| Hi U can do this by page.request.form(id) here the my sample control code '************************************************* ** Code: Imports System.ComponentModel
Imports System.Text
Imports System.Web.UI.WebControls
Imports System.Web.UI
<{0}:WebCustomControl1 runat=server>")> Public Class WebCustomControl1
Inherits System.Web.UI.WebControls.WebControl
Dim ctl As New System.Web.UI.WebControls.ListBox
Public Property MultiSeletion() As Boolean
Get
If Not viewstate("mMultiSelection") Is Nothing Then
Return viewstate("mMultiSelection")
End If
End Get
Set(ByVal Value As Boolean)
viewstate("mMultiSelection") = Value
End Set
End Property
Public Sub SetDataSource(ByVal dt As DataTable)
If dt.Rows.Count > 0 Then
ctl.DataTextField = dt.Columns(1).ColumnName
ctl.DataValueField = dt.Columns(0).ColumnName
ctl.DataSource = dt
ctl.DataBind()
End If
End Sub
Public Function SelectedValues() As String()
Dim str As String = Page.Request.Form("CtlList")
If Not str Is Nothing Then
Dim StrValues() As String
StrValues = str.Split(",")
If Not StrValues Is Nothing Then
Return StrValues
End If
End If
End Function
Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
Me.AddAttributesToRender(output)
Me.EnsureChildControls()
ctl.RenderControl(output)
End Sub
Protected Overrides Sub CreateChildControls()
ctl.ID = "CtlList"
ctl.Width = Unit.Pixel(100)
ctl.Height = Unit.Pixel(100)
If Not viewstate("mMultiSelection") Is Nothing Then
ctl.SelectionMode = ListSelectionMode.Multiple
Else
ctl.SelectionMode = ListSelectionMode.Single
End If
Me.Controls.Add(ctl)
End Sub
End Class here my Test Page code Code: Imports System.Data.SqlClient
Public Class WebForm1
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
Private Sub InitializeComponent()
End Sub
Protected WithEvents WebCustomControl11 As SampleListBox.WebCustomControl1
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Dim sConn As New SqlConnection("Your connection string ")
'Here the Test UserDetails is a sample table
Dim sDa As New SqlDataAdapter("Select UserID,FullName from TestUserDetails", sConn)
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable
Dim ds As New DataSet
sDa.Fill(ds)
dt = ds.Tables(0).Copy
If Not Page.IsPostBack Then
WebCustomControl11.SetDataSource(dt)
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str() As String
str = WebCustomControl11.SelectedValues
If str.Length > 0 Then
Dim i As Integer
For i = 0 To str.Length - 1
Response.Write(str(i).ToString & "
")
Next
End If
End Sub
End Class Sundaram |
| |||
| Hai Deeban, You want to select the multiple items from a multiple selection listbox. Please try this code Code: For i As Integer = 0 To ListBox1.Items.Count - 1
If ListBox1.Items(i).Selected Then
Dim strselecteditems As String = ListBox1.Items(i).Text
Response.Write(strselecteditems)
End If
Next Saravanan.J
__________________ J.Saravanan |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Extracting specific elements from listbox | Kirubhananth | ASP and ASP.NET Programming | 5 | 04-08-2008 10:35 PM |
| Listbox onselected event problem | $enthil | ASP and ASP.NET Programming | 2 | 11-01-2007 10:36 PM |
| How do you check that a selection in an options list has been selected? | Pvinothkumar | HTML, CSS and Javascript Coding Techniques | 1 | 10-17-2007 11:17 PM |
| How to convert read only mode to write only mode | itbarota | Testing Tools | 1 | 08-21-2007 06:23 AM |
| multiple file selection | gattuso | HTML, CSS and Javascript Coding Techniques | 1 | 08-08-2007 05:36 AM |