【问题标题】:How to access dataset defined in master page from user control on master page?如何从母版页上的用户控件访问母版页中定义的数据集?
【发布时间】:2021-06-23 18:24:18
【问题描述】:

我有一个母版页,其中包含在 VB 代码中定义的数据集。

Public Property Inventory As DataSet = New DataSet

我在Page_Init 中填充数据。

我在同一个母版页上有一个用户控件。如何从Page_Load 中的用户控件代码 (myusercontrol.ascx) 访问数据集?我对内容页面上的用户控件执行此操作没有问题,但当控件位于母版页上时,我无法使其工作。

通常,我只是将其称为Master.Inventory。当控件位于内容页面上时,它可以工作,但是当控件位于母版页上时,我该怎么做呢?我试过Parent.Page.Master.InventoryPage.Master.Inventory,甚至只是Inventory。我得到“未定义库存”或“库存不是...的成员”。

在用户控件中,我只是想执行Dim i As DataSet = [reference goes here] 之类的操作。

【问题讨论】:

    标签: asp.net vb.net user-controls master-pages


    【解决方案1】:

    首先,在您的母版页中创建一个公共属性。在这种情况下myDataSet

    public partial class Site : System.Web.UI.MasterPage
    {
        public DataTable myDataSet { get; set; }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            //add some dummy data to the dataset
            myDataSet = new DataTable();
            myDataSet.Columns.Add("ID", typeof(int));
            myDataSet.Columns.Add("COUNTRY", typeof(string));
    
            myDataSet.Rows.Add(0, "Netherlands");
            myDataSet.Rows.Add(1, "Japan");
            myDataSet.Rows.Add(2, "Country");
        }
    }
    

    然后在 Control 的 Page_Load 中引用 Master 即可访问该属性。

    public partial class WebUserControl1 : System.Web.UI.UserControl
    {
        public Site master;
    
        protected void Page_Load(object sender, EventArgs e)
        {
            //get the current master page
            master = (Site)Page.Master;
    
            //access the public property in the master
            Label1.Text = master.myDataSet.Rows[0][1].ToString();
        }
    }
    

    VB

    我为 VB 使用了代码翻译器,因此它可能不完全正确。但你会明白的。

    Public Class Site
        Inherits System.Web.UI.MasterPage
        
        Public Property myDataSet As DataTable
            Get
            End Get
            Set
            End Set
        End Property
        
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            'add some dummy data to the dataset
            Me.myDataSet = New DataTable
            Me.myDataSet.Columns.Add("ID", GetType(System.Int32))
            Me.myDataSet.Columns.Add("COUNTRY", GetType(System.String))
            Me.myDataSet.Rows.Add(0, "Netherlands")
            Me.myDataSet.Rows.Add(1, "Japan")
            Me.myDataSet.Rows.Add(2, "Country")
        End Sub
    End Class
    

    控制

    Public Class WebUserControl1
        Inherits System.Web.UI.UserControl
        
        Public master As Site
        
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            'get the current master page
            Me.master = CType(Page.Master,Site)
            'access the public property in the master
            Label1.Text = Me.master.myDataSet.Rows(0)(1).ToString
        End Sub
    End Class
    

    【讨论】:

    • 谢谢。我很感激帮助。我最终简化了要求并走了一条不同的路线。我只是直接在母版页中完成所有数据操作,然后直接从母版页对用户控件进行数据绑定。我想有时我只是把事情弄得太复杂了。
    【解决方案2】:

    我最终选择了不同的路线。我直接从母版页完成所有操作。

    我在我的用户控件中放置了一个数据绑定控件(我的恰好是一个DropDownList)。

    然后,我在用户控件myusercontrol.ascx.vb中添加了一个DataBind成员。

    Public Class MyUserControl
        Inherits System.Web.UI.UserControl
    
        Public Property DataSource As DataTable
            Get
                Return MyDataBoundControl.DataSource
            End Get
            Set(ByVal _source As DataTable)
                MyDataBoundControl.DataSource = _source
            End Set
        End Property
    
        Public Sub DataBind
            MyDataBoundControl.DataBind()
        End Sub
    End Class
    

    然后在母版页上site.master

    <MyPrefix:MyUserControl id="MyUserControl" runat="server" />
    

    而在母版页代码后面site.master.vb

    Dim DS As DataTable
    [do some stuff to define and populate DS]
    
    MyUserControl.DataSource = DS
    MyUserControl.DataBind()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-25
      • 2017-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多