?
Solved

DropDownList Value from Custom Control

Posted on 2005-05-10
9
Medium Priority
?
315 Views
Last Modified: 2008-03-10
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
0
Comment
Question by:JsonTerre1
  • 3
  • 3
7 Comments
 
LVL 11

Expert Comment

by:Ramesh Srinivas
ID: 13976919
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
0
 
LVL 11

Expert Comment

by:Ramesh Srinivas
ID: 13976922
Then just call "GetInsertTitle" to retreive the value (you can name it anything you want).
0
 
LVL 33

Expert Comment

by:raterus
ID: 13977852
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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 11

Expert Comment

by:Ramesh Srinivas
ID: 13978050
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?

0
 
LVL 33

Expert Comment

by:raterus
ID: 13978211
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.
0
 
LVL 1

Author Comment

by:JsonTerre1
ID: 13993017
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
0
 
LVL 33

Accepted Solution

by:
raterus earned 200 total points
ID: 13995190
Custom Composite controls, like what it looks like you are doing need to have all the child controls created in "CreateChildControls", so I hope the code you've listed that creates the dropdownlist is put in there

also, to ensure that it is created when it comes to accessing properties, we use something like this...

Public Property SomeValue() As String
            Get
                EnsureChildControls()
                Return ddlPageNumber.SelectedValue
            End Get
            Set(ByVal value As String)
                EnsureChildControls()
                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

Hope this helps,
--Michael
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Integration Management Part 2
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question