I have a page that will populate two dropdownlist by loading items from SQL Server during Page_Load event, so, when opening the page it may take some time to load. Currently, the dropdownlist will load asynchronously but it still will slow down the page loading. How can i load those dropdownlist after the page is completely loaded?
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