【问题标题】:How to access value in one Ascx control in another Ascx control on the same page如何在同一页面上的另一个 Ascx 控件中访问一个 Ascx 控件中的值
【发布时间】:2016-08-26 07:06:15
【问题描述】:

我有一个 aspx 页面,其中有两个用户控件,其中一个带有网格视图,另一个带有标签,用于在用户登录时显示用户数据。现在我想使用在第二个用户控件的标签中显示的网格视图。我怎样才能做到这一点。根据每个用户的安全角色,gridview 中的数据会发生变化。任何输入表示赞赏。谢谢

【问题讨论】:

    标签: asp.net vb.net gridview user-controls


    【解决方案1】:

    Gridview 用户控件在获得所需信息时会引发自定义事件。该事件在主页中进行处理,并通过一个公共属性分配给带有标签的 UserControl,该公共属性可以访问控件中嵌入的标签文本。

    默认.aspx

    包含两个用户控件的页面

    <%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="StackOverFlowJunkVB._Default" %>
    <%@ Register Src="~/WebUserControlGridView1.ascx" TagPrefix="uc1" TagName="WebUserControlGridView1" %>
    <%@ Register Src="~/WebUserControlLabel1.ascx" TagPrefix="uc1" TagName="WebUserControlLabel1" %>
    
    <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
        <uc1:WebUserControlGridView1 runat="server" id="WebUserControlGridView1" />
        <uc1:WebUserControlLabel1 runat="server" id="WebUserControlLabel1" />
    </asp:Content>
    

    Default.aspx.vb

    通过 GridView 用户控件引发的事件将文本分配给 Label 用户控件的代码

    Public Class _Default
        Inherits Page
    
        Private Sub WebUserControlGridView1_ReallyImportantLabelTextHandler(sender As Object, e As GridViewLabelEvent) _
          Handles WebUserControlGridView1.ReallyImportantLabelTextHandler
    
            WebUserControlLabel1.ReallyImportLabelText = e.ImportantLabelText
    
        End Sub
    End Class
    

    GridView 用户控件的代码隐藏

    ' Define a custom EventArgs class to pass some really important text
    Public Class GridViewLabelEvent
        Inherits EventArgs
    
        Public Property ImportantLabelText As String
    End Class
    
    ' The user control with a GridView
    Public Class WebUserControlGridView1
        Inherits System.Web.UI.UserControl
    
      Public Event ReallyImportantLabelTextHandler As EventHandler(Of GridViewLabelEvent)
    
      Private Sub GridView1_DataBound(sender As Object, e As EventArgs) Handles GridView1.DataBound
        Dim gvle As New GridViewLabelEvent
        gvle.ImportantLabelText = "This is really important"
        RaiseEvent ReallyImportantLabelTextHandler(Me, gvle)
      End Sub
    End Class
    

    标签用户控件的代码隐藏

    Public Class WebUserControlLabel1
        Inherits System.Web.UI.UserControl
    
        ' Property to assign Label Text
        Public Property ReallyImportLabelText As String
            Get
                Return Label1.Text
            End Get
            Set(value As String)
                Label1.Text = value
            End Set
        End Property
    End Class
    

    【讨论】:

      猜你喜欢
      • 2013-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-16
      • 1970-01-01
      相关资源
      最近更新 更多