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

R

RyanG

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
populated??

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
Get
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"
DataBind()

MyBase.OnInit(e)
End Sub

Protected Overrides Sub Render(ByVal output As
System.Web.UI.HtmlTextWriter)
MyBase.Render(output)
End Sub

End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top