Link to home
Start Free TrialLog in
Avatar of JsonTerre1
JsonTerre1

asked on

DropDownList Value from Custom Control

I have a Custom Control that where I create a DropDownList.  I can not seem to get the value of the DropDownList when the user selects a new value.

The function that is called in the Custom Control:

        Private Sub ddlPageNumberCommand( _
                        ByVal sender As Object, _
                        ByVal e As System.EventArgs)


        End Sub

How do i get the value of the selection.  


-Jason
Avatar of Ramesh Srinivas
Ramesh Srinivas
Flag of United Kingdom of Great Britain and Northern Ireland image

This should work if you have called your dropdown "DropDownList1" in your custom control page.

Public ReadOnly Property GetInsertTitle()
        Get
            Dim myCntl As Control
            myCntl = Page.FindControl("YourCustomControl")

            Dim lst As DropDownList
            Return CType(myCntl.FindControl("DropDownList1"), DropDownList).SelectedItem.Value
        End Get
End Property
Then just call "GetInsertTitle" to retreive the value (you can name it anything you want).
saleek, I have to disagree with you on this one, never should you "dig" into your custom control to get the value, this may work but it is not the right way to do this.

You should expose a property, in the custom control, that will give you the value.

(in custom control
Public Property SomeValue as String
  Get
    Return myDDL.SelectedValue
  End Get
  Set(value as String)
    Dim li as listitem = myDDL.Items.FindByValue(value)
    if not li is nothing then
      myDDL.ClearSelection
      li.Selected = True
    end if
  End Set
End Property
Raterus,

I once asked the same question as JsonTerre1 once and I tried what you have suggested.

However it did not work for me - I could not read the property. So someone else suggested the other method to me.

Perhaps it was because the version given to me is a little different:

Public Property GetSelectedValue() As String
   Get
         Return DDL.SelectedItem.Value
    End Get
       Set(ByVal Value As String)
         DDL.SelectedValue = Value
    End Set
End Property

What code would you use to retreive the property?

The problem with what you've posted is: "ddl.selecteditem.value" will throw an error if there are no values in the dropdownlist to be selected.

also "ddl.selectedvalue = value" will throw an error for the same reason, no items match...

The property code I provided in my last post will account for when this is the case.  SelectedValue will return "" if no item is there, and you can ensure that the listitem is in the dropdownlist first before selecting it.
Avatar of JsonTerre1
JsonTerre1

ASKER

reterus,

Thanks for the help but i can't get this to work.

This is the creation of my ddl in my Custom Control:

            Dim _dd As ArrayList = New ArrayList
            Dim _numOfPages As Integer
            Dim i As Integer

            If RecordCount > PageSize Then
                _numOfPages = (RecordCount / PageSize)
            Else
                _numOfPages = (PageSize / RecordCount)
            End If

            For i = 1 To _numOfPages
                _dd.Add(New ListItem(i.ToString, i.ToString))
            Next i
            Dim autDrop As DropDownList = New DropDownList
            autDrop.DataSource = _dd
            autDrop.DataValueField = "Value"
            autDrop.DataTextField = "Text"
            autDrop.ID = "ddlPageNumber"
            autDrop.DataBind()
            autDrop.SelectedValue = CurrentPageIndex
            autDrop.AutoPostBack = True
            AddHandler autDrop.SelectedIndexChanged, AddressOf ddlPageNumberCommand
            pnlPageLinks.Controls.Add(autDrop)

When i try to


        Public Property SomeValue() As String
            Get
                Return ddlPageNumber.SelectedValue
            End Get
            Set(ByVal value As String)
                Dim li As ListItem = ddlPageNumber.Items.FindByValue(value)
                If Not li Is Nothing Then
                    ddlPageNumber.ClearSelection()
                    li.Selected = True
                End If
            End Set
        End Property


it says that the ddlPageNumber is not defined.

What am i doing wrong.

-J
ASKER CERTIFIED SOLUTION
Avatar of raterus
raterus
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial