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

DropdownList problem

Status
Not open for further replies.

rohitpabby

Programmer
Aug 13, 2001
27
IN
Hi there!

I am a new learner of ASP.NET. I have a dropdownlist control which I have bound with a table and all the records are coming fine. But I want to add one more value in the list. E.g. If I have a dropwon for selection of countries. I want that "Select Country" should be the first item in the list and then the values which came from the table.

Please help....

Thanks.

Warm Regards,
Rohit Pabby
 
Here's a work in progress class that I've been building as needed to work with the list box and drop down list web controls. Just compile it, and use the methods to insert new entries, find particular entries, etc... enjoy! :)

------------------------
Code:
    Public Class webcontrols
        Public Class dropDownListUtilities
            Public Sub setState(ByVal state As String, ByRef list As System.Web.UI.WebControls.DropDownList)
                Dim lArray() As String
                If state.ToLower() = "false" Then
                    state = "0"
                ElseIf state.ToLower() = "true" Then
                    state = "1"
                End If
                Dim item As New System.Web.UI.WebControls.ListItem()
                For Each item In list.Items
                    If state = item.Value Then
                        item.Selected = True
                    Else
                        item.Selected = False
                    End If
                Next
                If noneSelected(list) Then
                    list.SelectedIndex = 0
                End If
            End Sub
            Public Function noneSelected(ByVal list As System.Web.UI.WebControls.DropDownList)
                Dim item As New System.Web.UI.WebControls.ListItem()
                Dim output As Boolean = True
                For Each item In list.Items
                    If item.Selected Then
                        output = False
                    End If
                Next
                Return output
            End Function
            Public Function returnIndex(ByVal box As DropDownList, ByVal text As String) As Integer
                Dim output As Integer = 0
                Dim i As Integer = 0
                Dim item As New ListItem()
                For Each item In box.Items
                    If text = item.Text Then
                        output = i
                        Exit For
                    End If
                    i += 1
                Next
                Return output
            End Function
            Public Sub insertEntry(ByRef box As System.Web.UI.WebControls.DropDownList, ByVal txt As String, _
                ByVal value As String, Optional ByVal selectIt As Boolean = False)
                Dim item As New System.Web.UI.WebControls.ListItem()
                item.Text = txt
                item.Value = value
                box.Items.Insert(0, item)
                If selectIt Then
                    box.SelectedIndex = 0
                End If
            End Sub
            Public Sub selectByValue(ByVal value As Object, ByVal list As DropDownList)
                'FIRST, UNSELECT ANY THAT MIGHT BE SELECTED
                list.SelectedIndex = -1
                If Not IsDBNull(value) Then
                    If Not value = String.Empty Then
                        Try
                            list.Items.FindByValue(value).Selected = True
                        Catch
                            list.SelectedIndex = -1
                        End Try
                    Else
                        insertEntry(list, "Select One", "0", True)
                    End If
                Else
                    insertEntry(list, "Select One", "0", True)
                End If
            End Sub
            Public Sub selectByText(ByVal text As String, ByVal list As DropDownList)
                'FIRST, UNSELECT ANY THAT MIGHT BE SELECTED
                list.SelectedIndex = -1
                If Not IsDBNull(text) Then
                    If Not text = String.Empty Then
                        Try
                            list.Items.FindByText(text).Selected = True
                        Catch
                            list.SelectedIndex = -1
                        End Try
                    Else
                        insertEntry(list, "Select One", "0", True)
                    End If
                Else
                    insertEntry(list, "Select One", "0", True)
                End If
            End Sub

        End Class

        Public Class listBoxUtilities
            Public Sub setState(ByVal state As String, ByRef box As System.Web.UI.WebControls.ListBox)
                Dim lArray() As String
                lArray = state.Split(",")
                Dim item As New System.Web.UI.WebControls.ListItem()
                For Each item In box.Items
                    If exists(item.Value, lArray) Then
                        item.Selected = True
                    Else
                        item.Selected = False
                    End If
                Next
            End Sub

            Public Function noneSelected(ByVal box As System.Web.UI.WebControls.ListBox)
                Dim item As New System.Web.UI.WebControls.ListItem()
                Dim output As Boolean = True
                For Each item In box.Items
                    If item.Selected Then
                        output = False
                    End If
                Next
                Return output
            End Function

            Public Function getCSV(ByVal box As System.Web.UI.WebControls.ListBox)
                Dim item As New ListItem()
                Dim output As String
                For Each item In box.Items
                    If item.Selected Then
                        output = output & item.Value & ","
                    End If
                Next
                output = output.Substring(0, output.Length - 1)
                Return output
            End Function

            Public Sub insertEntry(ByRef box As System.Web.UI.WebControls.ListBox, ByVal txt As String, _
                ByVal value As String, Optional ByVal selectIt As Boolean = False)
                Dim item As New System.Web.UI.WebControls.ListItem()
                item.Text = txt
                item.Value = value
                box.Items.Insert(0, item)
                If selectIt Then
                    box.SelectedIndex = 0
                End If
            End Sub
        End Class

        Shared Function exists(ByVal searchString As String, ByVal lArray() As String) As Boolean
            Dim i As Integer
            Dim output As Boolean = False
            For i = 0 To lArray.Length - 1
                If lArray(i) = searchString Then
                    output = True
                End If
            Next
            Return output
        End Function
    End Class
penny1.gif
penny1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top