"复印"公共对象属性到本地对象

时间:2014-10-24 07:46:35

标签: vb.net

本地对象如何采用公开声明的对象(例如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

1 个答案:

答案 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