1

我有一个asp.net 网页,这给我带来了一些问题。我开发了以下类来为页面生成搜索过滤器。搜索过程并不重要,因为它已经起作用了。我想动态生成搜索过滤器。

Imports Microsoft.VisualBasic
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports MEI.SPDocuments.Type


Public Class SearchFilter
    Private WithEvents _genreDropDown As DropDownList
    Private WithEvents _subGenreDropDown As DropDownList
    Private WithEvents _txtValue As TextBox
    Private _txtBoxAutoCompleteExtender As AjaxControlToolkit.AutoCompleteExtender
    Private _filterGenres As Collection(Of String)
    Private _programSubGenres() As String = {"Program ID,GetPrograms", "Territory ID,GetTerritories", _
                                             "Rep Name,GetRepNames", "District ID,GetDistricts", "DM Name,GetDMNames", "Region ID,GetRegions", _
                                             "RM Name,GetRMNames", "Speaker Counter,GetSpeaker", "Vedor ID,GetVendors", "Vendor Name,GetVendorName", _
                                             "Date Range,", "City,", "State,", "Pay To,", "Expense Range,", "PIF ID,GetPifs"}
    Private _speakerSubGenres() As String = {"Speaker Counter,GetSpeaker", "Speaker Last Name,GetSpeakerLNames", "Speaker First Name,GetSpeakerFNames"}
    Private _expenseSubGenres() As String = {"Expense Counter,GetExpenses"}
    Private _vendorSubGenres() As String = {"Vendor ID,GetVendors", "Vendor Name,GetVendorName"}
    Private _trackSubGenres() As String = {"Track Number,GetTracks", "HCP First Name,GetHCPFName", "HCP Last NameGetHCPLName"}

    Public Sub New(ByVal company As CompanyCode, ByVal year As DocumentYearCode)
        _filterGenres = New Collection(Of String)

    _txtValue = New TextBox
    _txtValue.ID = Guid.NewGuid.ToString

    _txtBoxAutoCompleteExtender = New AjaxControlToolkit.AutoCompleteExtender
    With _txtBoxAutoCompleteExtender
        .ID = "AC__" + _txtValue.ID
        .MinimumPrefixLength = 1
        .EnableCaching = False
        .ServicePath = "~/AutoComplete.asmx"
        .ServiceMethod = "PlaceHolder"
        .TargetControlID = _txtValue.ID
        .CompletionListCssClass = "CompletionList"
        .CompletionListHighlightedItemCssClass = "ItemHighlighted"
        .CompletionListItemCssClass = "ListItem"
        .DelimiterCharacters = ""
        .Enabled = True
    End With


    Select Case company
        Case CompanyCode.AbbottAnimalHealth, CompanyCode.AbbottDiabetesCare, CompanyCode.AbbottDiagnosticsDivision, CompanyCode.AbbottMedicalOptics, CompanyCode.AbbottMolecular, _
            CompanyCode.AbbottPointOfCare, CompanyCode.AbbottVascular, CompanyCode.Corporate, CompanyCode.DivAbbottNutrition, CompanyCode.EstablishedProductsDivision, _
            CompanyCode.GlobalPharmaceuticalResearchAndDevelopment, CompanyCode.GlobalStrategicMarketingAndServices, CompanyCode.PharmaseuticalProductsGroup, _
            CompanyCode.ProprietaryPharmaceuticalsDivision, CompanyCode.RegulatoryAffairsPPG
            _filterGenres.Add("Div Docs")
        Case Else
            _filterGenres.Add("Program")
            _filterGenres.Add("Speaker")
            _filterGenres.Add("Expense")
            _filterGenres.Add("Vendor")
    End Select
    _genreDropDown = New DropDownList
    _genreDropDown.AutoPostBack = True
    _genreDropDown.Attributes.Add("runat", "server")
    AddHandler _genreDropDown.SelectedIndexChanged, AddressOf _genreDropDown_ItemChanged

    _subGenreDropDown = New DropDownList
    _subGenreDropDown.AutoPostBack = True
    _subGenreDropDown.Attributes.Add("runat", "server")
    AddHandler _subGenreDropDown.SelectedIndexChanged, AddressOf _subGenreDropDown_ItemChanged
    PopulateDDls()
End Sub

Private Sub PopulateDDls()
    _genreDropDown.Items.Add("")
    For Each s As String In _filterGenres
        _genreDropDown.Items.Add(s)
    Next
End Sub

Private Sub _genreDropDown_ItemChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    _subGenreDropDown.Items.Clear()
    _subGenreDropDown.Items.Add("")
    Select Case _genreDropDown.SelectedItem.ToString
        Case "Program"
            For Each s As String In _programSubGenres
                Dim li As New ListItem(s.Split(CChar(","))(0), s.Split(CChar(","))(1))

                _subGenreDropDown.Items.Add(li)
            Next
        Case "Speaker"
            For Each s As String In _speakerSubGenres
                Dim li As New ListItem(s.Split(CChar(","))(0), s.Split(CChar(","))(1))

                _subGenreDropDown.Items.Add(li)
            Next
        Case "Expense"
            For Each s As String In _expenseSubGenres
                Dim li As New ListItem(s.Split(CChar(","))(0), s.Split(CChar(","))(1))

                _subGenreDropDown.Items.Add(li)
            Next
        Case "Vendor"
            For Each s As String In _vendorSubGenres
                Dim li As New ListItem(s.Split(CChar(","))(0), s.Split(CChar(","))(1))

                _subGenreDropDown.Items.Add(li)
            Next
        Case "Div Docs"
            For Each s As String In _trackSubGenres
                Dim li As New ListItem(s.Split(CChar(","))(0), s.Split(CChar(","))(1))

                _subGenreDropDown.Items.Add(li)
            Next
    End Select
End Sub

Private Sub _subGenreDropDown_ItemChanged(ByVal Sender As Object, ByVal e As System.EventArgs)
    _txtBoxAutoCompleteExtender.ServiceMethod = _subGenreDropDown.SelectedValue.ToString
    If _subGenreDropDown.SelectedValue.ToString = String.Empty Then _txtBoxAutoCompleteExtender.ServiceMethod = "PlaceHolder"
End Sub

Public Function createMyTableRow() As HtmlTableRow
    Dim myRow As New HtmlTableRow

    myRow.Cells.Add(New HtmlTableCell())
    myRow.Cells.Add(New HtmlTableCell())
    myRow.Cells.Add(New HtmlTableCell())

    myRow.Cells(0).Controls.Add(_genreDropDown)
    myRow.Cells(1).Controls.Add(_subGenreDropDown)
    myRow.Cells(2).Controls.Add(_txtValue)
    myRow.Cells(2).Controls.Add(_txtBoxAutoCompleteExtender)

    Return myRow
End Function

Private Sub newAutoCompleteExtender(ByVal genre As String)
    If _txtValue.Parent.Controls.Count = 2 Then
        _txtValue.Parent.Controls.RemoveAt(1)
    End If


    _txtValue.Parent.Controls.Add(_txtBoxAutoCompleteExtender)
End Sub

End Class


Public Class SearchFilterGroup

Private _searchFilterCollection As Collection(Of SearchFilter)
Private _tableContainer As HtmlTable
Private _company As CompanyCode
Private _year As DocumentYearCode
Public WithEvents _addFilterButton As New Button

Public Sub New(ByVal company As CompanyCode, ByVal year As DocumentYearCode)
    _searchFilterCollection = New Collection(Of SearchFilter)
    _tableContainer = New HtmlTable
    _company = company
    _year = year


    _addFilterButton.Text = "Add Filter"
    _addFilterButton.Attributes.Add("runat", "server")
    _addFilterButton.ID = "btnAddFilter"
    AddHandler _addFilterButton.Click, AddressOf _addFilterButton_Click
End Sub

Public Sub _addFilterButton_Click(ByVal Sender As Object, ByVal e As System.EventArgs)
    _searchFilterCollection.Add(New SearchFilter(_company, _year))
    _tableContainer.Rows.Add(_searchFilterCollection(_searchFilterCollection.Count - 1).createMyTableRow)
End Sub

Public Function table() As HtmlTable
    _tableContainer.Rows.Add(New HtmlTableRow)
    _tableContainer.Rows(0).Cells.Add(New HtmlTableCell)
    _tableContainer.Rows(0).Cells(0).ColSpan = 3

    _tableContainer.Rows(0).Cells(0).Controls.Add(_addFilterButton)
    _addFilterButton_Click(Nothing, Nothing)
    _addFilterButton_Click(Nothing, Nothing)

    Return _tableContainer
End Function
End Class

我在设计一种持久化生成控件的方法时遇到问题。任何帮助,将不胜感激。

4

1 回答 1

0

基本上,对于您所做的任务,最好创建自定义服务器控件。在 ASP.NET 中存在 CompositeControl 类,它简化了开发您自己的服务器控件,例如应该包括一些其他服务器控件(如面板、texbox 等)作为子项。

开始创建自己的服务器控件的一个很好的链接是 Dino Esposito 文章:ASP.NET 控件开发速成课程:构建复合控件

于 2012-10-21T13:06:56.837 回答