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
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
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(va lue)
if not li is nothing then
myDDL.ClearSelection
li.Selected = True
end if
End Set
End Property
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(va
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?
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.
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.
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.SelectedIndexChang ed, AddressOf ddlPageNumberCommand
pnlPageLinks.Controls.Add( autDrop)
When i try to
Public Property SomeValue() As String
Get
Return ddlPageNumber.SelectedValu e
End Get
Set(ByVal value As String)
Dim li As ListItem = ddlPageNumber.Items.FindBy Value(valu e)
If Not li Is Nothing Then
ddlPageNumber.ClearSelecti on()
li.Selected = True
End If
End Set
End Property
it says that the ddlPageNumber is not defined.
What am i doing wrong.
-J
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.SelectedIndexChang
pnlPageLinks.Controls.Add(
When i try to
Public Property SomeValue() As String
Get
Return ddlPageNumber.SelectedValu
End Get
Set(ByVal value As String)
Dim li As ListItem = ddlPageNumber.Items.FindBy
If Not li Is Nothing Then
ddlPageNumber.ClearSelecti
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Public ReadOnly Property GetInsertTitle()
Get
Dim myCntl As Control
myCntl = Page.FindControl("YourCust
Dim lst As DropDownList
Return CType(myCntl.FindControl("
End Get
End Property