2012-09-28 42 views
1

我有一個asp.net網頁,它給了我一些問題。我開發了以下類來爲頁面生成搜索過濾器。搜索過程並不重要,因爲它已經起作用。我想動態生成搜索過濾器。在我的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 

我在設計一種持久化生成控件的方法時遇到問題。任何幫助,將不勝感激。

+0

從我在ASP.Net一點經驗,我不相信你可以「堅持」的控件。每次加載頁面時都必須重新生成它們。另一種方法是使用AJAX之類的東西來消除頁面重新加載以顯示新信息的需要。 – Origin

回答

0

基本上,您所做的任務最好是創建自定義服務器控件。在ASP.NET中存在CompositeControl類,它簡化了開發自己的服務器控件,例如應該包含一些其他服務器控件(如面板,texboxes等)作爲子項。

一個很好的鏈接,開始創建自己的服務器控件是迪諾·埃斯波西託文章:A Crash Course on ASP.NET Control Development: Building Composite Controls