在由ddlPlant_SelectedIndexChanged
引起的PostBack之后,我需要设置集HttpContext.Current.Session("PlantNumber")
。这需要在ddlPlant在Site.Master中加载之后,但在Default.aspx中的代码需要该值之前发生。
Public Class SiteMaster Inherits MasterPage
Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init
If (Not Page.IsPostBack) Then
ddlPlant.DataSource = myDataSource
ddlPlant.DataBind()
ddlPlant.SelectedValue = "1"
End If
HttpContext.Current.Session("PlantNumber") = ddlPlant.SelectedValue
End Sub
Protected Sub ddlPlant_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlPlant.SelectedIndexChanged
End Sub
End Class
Public Class _Default Inherits Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim units As New List(Of EquipmentModel)
For Each unit As EquipmentModel In getUnits.Out.Results
If (CStr(unit.Plant_ID) = HttpContext.Current.Session("PlantNumber")) Then
units.Add(unit)
End If
Next
gvEquipmentUnit.DataSource = units.OrderBy(Function(n) n.Equipment_ID)
gvEquipmentUnit.DataBind()
End Sub
使用上面的代码,在PostBack之后设置Session("PlantNumber")
时,ddlPlant.SelectedIndex = Nothing,而ddlPlant.SelectedValue是一个空字符串。
我尝试将Session("PlantNumber") = ddlPlant.SelectedValue
行转移到Site.Master的Page_Load,但是在之后运行在Default.aspx.vb中
答案 0 :(得分:0)
我查了一下PreLoad,但显然它对Master页面不起作用。
最终,我决定不使用Session变量,而是直接从任何需要加载值的页面上调用控件(几乎所有这些):
DirectCast(Master.FindControl("ddlPlant"), DropDownList).SelectedValue