本地对象如何采用公开声明的对象(例如DropDownList)属性?我将使用此方法,因为dropdownlist
中会有很多本地Partial Class
个对象。
Public Class connection
Inherits System.Web.UI.Page
'Connections
Public cn As New SqlConnection
Public cmd As SqlCommand
Public rs As SqlDataReader
Public dt As DataTable = New DataTable()
Public ds As DataSet = New DataSet()
'Public Objects
Public ddlAcadyr As DropDownList = New DropDownList()
Public Sub AcadYr()
Dim adp As SqlDataAdapter = New SqlDataAdapter("SELECT DISTINCT AcadYr FROM TermSchedule", cn)
Dim ds As DataSet = New DataSet()
adp.Fill(ds)
ddlAcadyr.DataSource = ds
ddlAcadyr.DataTextField = "AcadYr"
ddlAcadyr.DataBind()
ddlAcadyr.Items.Insert(0, New ListItem(String.Empty, String.Empty))
ddlAcadyr.SelectedIndex = 0
End Sub
End Class
Partial Class Home
Inherits connection
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Call AcadYr()
ddlPrivateAcadyr = ddlAcadyr '*NOTE: ddlPrivateAcadyr is a private object dropdownlist in class home
End Sub
End Class
答案 0 :(得分:1)
在connection class
中,您可以像这样使用Shared Sub
:
Public Sub SetAcadYrProperties(ByVal ddlAcadYrToCustomize as DropDownList)
Dim adp As SqlDataAdapter = New SqlDataAdapter("SELECT DISTINCT AcadYr FROM TermSchedule", cn)
Dim ds As DataSet = New DataSet()
adp.Fill(ds)
ddlAcadYrToCustomize.DataSource = ds
ddlAcadYrToCustomize.DataTextField = "AcadYr"
ddlAcadYrToCustomize.DataBind()
ddlAcadYrToCustomize.Items.Insert(0, New ListItem(String.Empty, String.Empty))
ddlAcadYrToCustomize.SelectedIndex = 0
End Sub
从Home class
这样调用它:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
SetAcadYrProperties(ddlPrivateAcadyr)
End Sub
如果此解决方案有效,您可以删除ddlAcadyr
中的AcadYr()
和connection class
。