I have a bit of a problem. I am currently makeing a webpage that has a few dropdown lists on each page. The purpose of the dropdown lists are to filter the information in a YUI datatable, aswell as eachother’s information, eg. a different location will have different commodities etc.
I have made a common function to read the option ID’s and values from the DB, but the information can be used in 3 ways. Currently, when the page loads I create a Asp:Placeholder that I load the Dropdown lists in. If the information is requested by Ajax to update a select box, I concatenate a HTML string server side and response.write it. But now my client has requested to have a lookup for if the list in the dropdown box is too long, which I am doing using a YUI modal panel and a YUI grid. I would still like to use the same data fetching subprocedure, but this time I would like to send the data back as a JSON string.
Currently I have boolean flags to signal whether its the pages initial load to create a placeholder, or an update load to make an HTML string, but I would rather like to pass the “information rendering/formatting” subprocedure as a parameter, this eleminating the need for several boolean flags.
My Code
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
The delegate keyword lets you declare a variable type representing a function. This can then be used in a parameter declaration. The function is called by using variable_name.Invoke(parameters…).
So something like this:
When passing the parameter from the calling code, use AddressOf function_name:
But note all the functions to be passed in will need to have exactly the same parameters list – they must exactly match the definition of the delegate. So you would need to add the PlaceHolder parameter to the SelectString function even if it is not used.