我有一个页面,它会在Page_Load事件期间通过从SQL Server加载项目来填充两个下拉列表,因此,打开页面时可能需要一些时间来加载。目前,下拉列表将异步加载,但仍会降低页面加载速度。如何在页面完全加载后加载这些下拉列表?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadDDL()
End If
End Sub
Public Sub LoadDDL()
Try
Dim cnnStr As String = DBMgr1.asyncADOCnnStr("SQLDS")
'===== Load Scheme DDL =====
Dim cnnScheme As SqlConnection = New SqlConnection(cnnStr)
cnnScheme.Open()
Dim SchemeStr As String = "SELECT " & _
"CASE CODE " & _
"WHEN 'P-PFC' THEN 'PFC' " & _
"ELSE CODE " & _
"END AS [SchemeCode], " & _
"CASE DESCR " & _
"WHEN 'P-PFC' THEN 'P-PFC/C-PFC' " & _
"ELSE CODE " & _
"END AS [SchemeName] " & _
"FROM i_Library WHERE MODULE = 'SYS' AND TYPE = 'Schemes' AND Stat1 = 'ACT' " & _
"AND Code <> 'C-PFC' " & _
"ORDER BY DESCR DESC"
Dim cmdScheme As New SqlCommand(SchemeStr, cnnScheme)
cmdScheme.CommandType = CommandType.Text
Dim arScheme As IAsyncResult = cmdScheme.BeginExecuteReader()
'===== Load Scheme DDL =====
'===== Load Branch DDL =====
Dim cnnBranch As SqlConnection = New SqlConnection(cnnStr)
cnnBranch.Open()
Dim BranchStr As String = "" & _
"SELECT Distinct TeamCode " & _
"FROM v_staff " & _
"ORDER BY TeamCode " & _
" "
Dim cmdBranch As New SqlCommand(BranchStr, cnnBranch)
cmdBranch.CommandType = CommandType.Text
Dim arBranch As IAsyncResult = cmdBranch.BeginExecuteReader()
'===== Load Branch DDL =====
'===== Load Region Code DDL =====
Dim cnnRegion As SqlConnection = New SqlConnection(cnnStr)
cnnRegion.Open()
Dim RegionStr As String = "" & _
"SELECT Distinct RegionCode " & _
"FROM v_staff " & _
"WHERE RegionCode IS NOT NULL " & _
"ORDER BY RegionCode " & _
" "
Dim cmdRegion As New SqlCommand(RegionStr, cnnRegion)
cmdRegion.CommandType = CommandType.Text
Dim arRegion As IAsyncResult = cmdRegion.BeginExecuteReader()
'===== Load Region Code DDL =====
'wait for the Scheme Query to return result
arScheme.AsyncWaitHandle.WaitOne()
Dim drScheme As SqlDataReader = cmdScheme.EndExecuteReader(arScheme)
While drScheme.Read
ddlScheme.DataSource = drScheme
ddlScheme.DataTextField = "SchemeName"
ddlScheme.DataValueField = "SchemeCode"
ddlScheme.DataBind()
ddlScheme.Items.Insert(0, "Select Scheme")
ddlScheme.Items(0).Value = CMM.sExcVal1
End While
'wait for the Branch Query to return result
arBranch.AsyncWaitHandle.WaitOne()
Dim drBranch As SqlDataReader = cmdBranch.EndExecuteReader(arBranch)
While drBranch.Read
ddlBranch.DataSource = drBranch
ddlBranch.DataTextField = "TeamCode"
ddlBranch.DataValueField = "TeamCode"
ddlBranch.DataBind()
ddlBranch.Items.Insert(0, "Select Branch")
ddlBranch.Items(0).Value = CMM.sExcVal1
End While
'wait for the Region Query to return result
arRegion.AsyncWaitHandle.WaitOne()
Dim drRegion As SqlDataReader = cmdRegion.EndExecuteReader(arRegion)
While drRegion.Read
ddlRegionCode.DataSource = drRegion
ddlRegionCode.DataTextField = "RegionCode"
ddlRegionCode.DataValueField = "RegionCode"
ddlRegionCode.DataBind()
ddlRegionCode.Items.Insert(0, "Select Region")
ddlRegionCode.Items(0).Value = CMM.sExcVal1
End While
drScheme.Close()
drBranch.Close()
drRegion.Close()
cnnScheme.Close()
cnnBranch.Close()
cnnRegion.Close()
Catch ex As Exception
MSGMgr.errHandlerSys(ex.Message, lblMsg)
End Try
End Sub
答案 0 :(得分:1)
或许使用ajax方法?
答案 1 :(得分:1)
在你的页面中,编写这个js方法,它需要一个父项和一个子项下拉列表,它将根据父下拉列表的选定值异步填充子下拉列表:
function LoadDataHandle(cmbParent,cmbChild,Type) {
var cmb = document.getElementById(cmbParent);
var mydate = new Date()
mydate.setDate(mydate.getDate())
$.get("GetItems.aspx?ID=" + cmb.value + "&Type=" + Type + "&x=" + mydate.getMilliseconds(), function(data) {
LoadData(data, cmbChild);
});
}
function LoadData(strData, cmbChild) {
var cmb = document.getElementById(cmbChild);
cmb.options.length = 0;
var choose = document.createElement('option');
choose.text = "--- اختر ---";
choose.value = -1;
cmb.add(choose);
if (strData.length > 0) {
var data = strData.split('$');
var i = 0;
for (i = 0; i < data.length - 1; i++) {
var data1 = data[i].split('#');
var item = document.createElement('option');
item.value = data1[0];
item.text = data1[1];
cmb.add(item);
}
}
}
请注意,使用jqyery的$ get方法传入查询字符串中包含parentid的aspx页面。
现在在包含下拉列表的aspx页面的page_load事件中
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
cmdParent.Attributes.Add("onchange", "LoadDataHandle('" & cmdParent.ClientID & "','" & cmdChild.ClientID & "');")
End Sub
我们差不多完成了,请注意,在GetItems.aspx时代,您需要将子项下拉项中应填写的项目写为单个长字符串,其中每个项目用“#”分隔。这是一个这样做的样本:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim ID As Integer = Request.QueryString("ID")
Response.Write(GetDepartment(ID))
End Sub
Private Function GetDepartment(ByVal ID As String) As String
Dim dt As DataTable = 'get from db
If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then
Dim sb As New StringBuilder(dt.Rows.Count * 20)
For Each row As DataRow In dt.Rows
sb.AppendFormat("{0}#{1}$", row(0), row(1))
Next
Return sb.ToString
End If
Return ""
End Function
答案 2 :(得分:0)
我必须这样才能让它发挥作用。
首先,在.aspx文件中,我输入了一个隐藏的输入,该输入具有我想从javascript访问的元素的正确ID:
<input type='hidden' id='dropdownelem1ID' value='<%= DropdownElem1.ClientID %>' />
然后,正如MikeW指出的那样,只需要进行ajax调用,最好使用jQuery,然后填写结果。
为了得到正确的元素,我会使用这样的东西:
$('#' + $('#dropdownelem1ID').val())
答案 3 :(得分:0)
所有用户的下拉内容是否相同,至少有一段时间?如果是这样,您可以将生成的HTML缓存在用户控件中。
否则,除了Ajax / jQuery之外,您还可以使用Silverlight发出WCF调用以检索下拉列表的内容并将其写入DOM。