简体   繁体   中英

Having trouble with dynamic controls on my ASP.Net web site

I have an asp.net web page which is giving me some problems. I developed the following classes to generate search filters for the page. The search process is not important as it already works. I want to dynamically generate the search filters.

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

I am having problems devising a way to persist the generate controls. Any help would be appreciated.

Basically, for task which you do is better to create custom server control. In ASP.NET exist CompositeControl class which simplifies developing your own server controls which for example should include some other server controls (like panels, texboxes and etc) as children.

A very good link to start with creating your own server controls is Dino Esposito article: A Crash Course on ASP.NET Control Development: Building Composite Controls

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM