IT Community - Software Programming, Web Development and Technical Support

How i will get the selected value from Listbox( if selection mode=multiple)

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 ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 02-07-2008, 04:09 AM
Mramesh Mramesh is offline
D-Web Sr.Programmer
 
Join Date: Sep 2007
Location: Chennai
Posts: 106
Mramesh is on a distinguished road
Send a message via MSN to Mramesh
Question How i will get the selected value from Listbox( if selection mode=multiple)

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-07-2008, 04:10 AM
a.deeban a.deeban is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 279
a.deeban is on a distinguished road
Default Re: How i will get the selected value from Listbox( if selection mode=multiple)

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
whats the pblms on code/ how to solve this.

Thanks
Deeban

Last edited by a.deeban : 02-07-2008 at 04:17 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-07-2008, 04:13 AM
Sundaram Sundaram is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Location: chennai
Posts: 117
Sundaram is on a distinguished road
Send a message via MSN to Sundaram Send a message via Yahoo to Sundaram
Default Re: How i will get the selected value from Listbox( if selection mode=multiple)

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
by
Sundaram
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-07-2008, 04:20 AM
SaravananJ SaravananJ is offline
D-Web Programmer
 
Join Date: Aug 2007
Posts: 79
SaravananJ is on a distinguished road
Default Re: How i will get the selected value from Listbox( if selection mode=multiple)

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
By
Saravanan.J
__________________
J.Saravanan
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT -7. The time now is 08:50 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0