我有点问题。我目前正在制作一个每页都有几个下拉列表的网页。下拉列表的目的是过滤YUI数据表中的信息,以及彼此的信息,例如。不同的地方会有不同的商品等。
我已经创建了一个通用函数来读取数据库中的选项ID和值,但信息可以通过3种方式使用。目前,当页面加载时,我创建一个Asp:Placeholder,我加载下拉列表。如果Ajax请求信息更新选择框,我连接HTML字符串服务器端和response.write它。但是现在我的客户端已经请求查找下拉框中的列表是否太长,我正在使用YUI模式面板和YUI网格。我仍然想使用相同的数据获取子过程,但这次我想将数据作为JSON字符串发回。
目前我有布尔标志来表示它的页面初始加载是为了创建一个占位符,还是用于创建HTML字符串的更新加载,但我更愿意将“信息呈现/格式化”子过程作为参数传递,这使得需要几个布尔标志。
我的代码
Public Shared Sub LoadCoop(ByRef PlaceHolder As Object, ByVal SearchCriteria As String, ByVal Database As String, ByVal InitialLoad As Boolean) Dim SqlConnection As New SqlConnection Dim SqlCommand As New SqlCommand Dim SqlParameter As New List(Of SqlParameter) Dim SqlReader As SqlDataReader = Nothing Dim FilterList As New List(Of FilterObject) Try SqlConnection = CreateDatabaseConnection(ConnectionString) AddSqlParameterToCollection(SqlParameter, "@SearchCriteria", SearchCriteria) AddSqlParameterToCollection(SqlParameter, "@Database", Database) SqlCommand = CreateSqlCommand("[proc_Dynamic_GetCoop]", SqlConnection, SqlParameter) SqlReader = SqlCommand.ExecuteReader If SqlReader.HasRows Then Do While SqlReader.Read Dim TempFilterObject As FilterObject = New FilterObject TempFilterObject.ID = SqlReader("PSCM_COOP_ID") TempFilterObject.Description = SqlReader("PSCM_COOP_ID") FilterList.Add(TempFilterObject) Loop End If If InitialLoad = True Then CreateHTMLSelectContainer(PlaceHolder, FilterList, "Coop") Else CreateHTMLSelectString(FilterList, "Coop") End If Catch ex As Exception HttpContext.Current.Response.Write("ERROR - An error occurred loading the co-op filter. Please contact the system administrators for assistance.") Finally If Not IsNothing(SqlReader) Then SqlReader.Close() SqlReader = Nothing End If If Not IsNothing(SqlCommand) Then SqlCommand.Dispose() SqlCommand = Nothing End If If Not IsNothing(SqlConnection) Then SqlConnection.Close() SqlConnection.Dispose() SqlConnection = Nothing End If End Try End Sub Public Shared Sub CreateHTMLSelectContainer(ByRef PlaceHolder As Object, ByVal FilterList As List(Of FilterObject), ByVal ID As String) Dim ReturnString As String = "" For Each Obj As FilterObject In FilterList ReturnString += "opening option tag" & Obj.Description & "closing option tag" Next Dim Container As New HtmlGenericControl("select") Container.ID = "ddl" & ID Container.Attributes.Add("class", "filtering_fields_select") Container.InnerHtml = ReturnString PlaceHolder.Controls.Add(Container) If FilterList.Count > 20 Then PlaceHolder.Controls.Add(New LiteralControl("lookup image goes here")) End If End Sub Public Shared Sub CreateHTMLSelectString(ByVal FilterList As List(Of FilterObject), ByVal ID As String) Dim ReturnString As String = "opening select tag" Dim Obj As FilterObject = Nothing For Each Obj In FilterList ReturnString += "opening option tag" & Obj.Description & "closing option tag" Next ReturnString += "closing select tag" If FilterList.Count > 20 Then ReturnString += "lookup image goes here" End If HttpContext.Current.Response.Write(ReturnString) End Sub
答案 0 :(得分:0)
delegate关键字允许您声明表示函数的变量类型。然后可以在参数声明中使用它。使用variable_name.Invoke(参数...)调用该函数。
这样的事情:
' declares data type representing one of the possible functions
Public Delegate Sub CreateHTMLFunction(ByRef PlaceHolder As Object, ByVal FilterList As List(Of FilterObject), ByVal ID As String)
Public Shared Sub LoadCoop(ByRef PlaceHolder As Object, ByVal SearchCriteria As String, ByVal Database As String, ByVal HTMLFunction As CreateHTMLFunction)
' last param is function to call
' snip
HTMLFunction.Invoke(PlaceHolder, FilterList, "Coop")
End Sub
Public Sub CreateHTMLSelectContainer(ByRef PlaceHolder As Object, ByVal FilterList As List(Of FilterObject), ByVal ID As String)
' blah blah
End Sub
Public Sub CreateHTMLSelectString(ByRef PlaceHolder As Object, ByVal FilterList As List(Of FilterObject), ByVal ID As String)
' blah blah
End Sub
从调用代码传递参数时,请使用AddressOf function_name:
LoadCoop(..., ..., ..., Addressof CreateHTMLSelectContainer)
但请注意,要传入的所有函数都需要具有完全相同的参数列表 - 它们必须与委托的定义完全匹配。所以你需要将PlaceHolder参数添加到SelectString函数中,即使它没有被使用。