我有點問題。我目前正在製作一個網頁,每個網頁上都有一些下拉列表。下拉列表的目的是過濾YUI數據表中的信息,以及彼此的信息,例如。一個不同的位置將有不同的商品等將子過程作爲參數傳遞給VB.Net 2008中的另一個子過程
我已經做了一個共同的功能,從數據庫中讀取選項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
太棒了!謝謝你的答案。 可惜的是,參數必須是相同的,不像實際上只使用它需要的參數的javascript。 – 2012-04-24 11:26:17
那麼你可以使用paramarray來允許一個不確定的參數列表,通過聲明最後一個參數是這樣的: ByVal ParamArray args()As Object 但這很難看,我懷疑它是否可以與委託一起工作。如果你有一個非常不同的函數加載參數列表,它可能值得嘗試。 – 2012-04-24 15:28:26