By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,323 Members | 1,291 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,323 IT Pros & Developers. It's quick & easy.

How can you filter data in a Custom DropDownList....

P: n/a
when the value that determines the filter is databound??

I am trying to make a DropDownList for a set of data that I use a lot
throughout my project. So I extended the DropDownList to retrieve
from the database a set of data to populate the itself with.

Now to make this control as reusable as possible I also added a new
property to this Custom DropDownList. With the goal being that this
property would be used with the DataView RowFilter method to filter
the data.

The problem I have run into is that if I try to bind data to this new
property then the list will not get filtered. If I just set the value
in the HTML tag it works fine. But binding a value (via <%#
container.dataitem("MyField")%>) or setting one in the pages
codebehind (via MyDLL.MyProp = "Test") results in the DropDownList
data not being filtered.

I know this is because the lifecycle and the order in which events are
fired, with the DropDownList being populated during the OnInit event.

I had tried doing something like having a method to retrieve the data
fire during the PreRender event. This results in
"System.NullReferenceException: Object variable or With block variable
not set" error being thrown, which I guess is because PreRender is
after the DataBind event??

So my question is, how can I filter this Custom DropDownList's data??
Is there any way to bind the data during another phase of the
lifecycle?? Or is there a way to filter the list after it has been

Any help would be most appreciated.

Custom DDL Code included:

<DefaultProperty("ItemType"), ToolboxData("<{0}:ItemsDDL
runat=server></{0}:ItemsDDL>")> Public Class ItemsDDL
Inherits System.Web.UI.WebControls.DropDownList

Private _Itemtype As String = "all"
Private DS As DataSet
Private DV As DataView

<Bindable(True), Category("Data"), Description("The Item's
Arch-type"), DefaultValue("all")> Property [ItemType]() As String
Return _Itemtype
End Get
Set(ByVal Value As String)
_Itemtype = Value
End Set
End Property

Protected Overrides Sub OnInit(ByVal e As EventArgs)
Dim DbRefs As New DbRefsWS.refservice
DS = New DataSet
DV = New DataView(DS.Tables("ItemRef"))

If ItemType <> "all" Then
DV.RowFilter = "ItemType = '" & ItemType & "'"
End If

DataSource = DV
DataTextField = "ItemName"
DataValueField = "ItemID"

End Sub

Protected Overrides Sub Render(ByVal output As
End Sub

End Class
Nov 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.