用户控件之间的RaiseEvent,在一个页面ASP.net上有多个相同控件的实例

时间:2021-12-02 08:20:50

I have a problem where I'm trying to send a string between User Controls, "input.ascx" and "output.ascx", when I have more the one of each User Control.

我有一个问题,我正在尝试在用户控件,“input.ascx”和“output.ascx”之间发送一个字符串,当我有更多的每个用户控件。

Here is the ASPX for the parent page:

这是父页面的ASPX:

<uc:Input runat="server" id="Input1" />
<uc:Input runat="server" id="Input2" />

<uc:Output runat="server" id="Output1" />
<uc:Output runat="server" id="Output2" />

User Control Input ASCX:

用户控制输入ASCX:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />

User Control Input VB.net:

用户控制输入VB.net:

Public Shared Event Button1Click(ByVal s As String)

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim s As String = TextBox1.Text
    RaiseEvent Button1Click(s)
End Sub

User Control Output ASCX:

用户控制输出ASCX:

<%@ Reference Control="~/Input.ascx" %>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

User Control Output VB.net:

用户控制输出VB.net:

Public Sub New()
    AddHandler UCTestUcInput.Button1Click, AddressOf DisplayText
End Sub

Private Sub DisplayText(ByVal s As String)
    Label1.Text = s
End Sub

The problem is when I input into either one of the "input.ascx" it gets displayed in both "output.ascx" when it should only be displayed in the corresponding output (like input1 corresponds with output1).

问题是当我输入“input.ascx”中的任何一个时,它会在“output.ascx”中显示,它只应显示在相应的输出中(就像input1对应于output1)。

1 个解决方案

#1


1  

The solution I found to this problem is from an old article from 2002 on Code Project. Here's the link to that article ASP.NET User Controls - Notify One Control Of Changes In Other Control. After reading that article and looking through sample code, here are the following changes I made to the code above.

我发现这个问题的解决方案来自2002年Code Code上的一篇旧文章。以下是该文章的链接ASP.NET用户控件 - 通知其他控件中的更改控件。阅读该文章并查看示例代码后,以下是对上述代码所做的以下更改。

There are no changes to the ASPX files except the following line in the Output ASPX file should be removed.

除了输出ASPX文件中的以下行应该被删除之外,ASPX文件没有任何更改。

<%@ Reference Control="~/Input.ascx" %>

First you add a new class called LabelChangeEventArgs.

首先,添加一个名为LabelChangeEventArgs的新类。

Public Class LabelChangeEventArgs
    Inherits EventArgs

    Private _LabelStr As String

    Public Property LabelText() As String
        Get
            Return _LabelStr
        End Get
        Set(value As String)
            _LabelStr = value
        End Set
    End Property
End Class

User Control Input VB.net: You add a Delegate and use the new LableChangeEventArgs class to handle the string.

用户控件输入VB.net:添加一个Delegate并使用新的LableChangeEventArgs类来处理字符串。

Public Delegate Sub LabelChangeEventHandler(ByVal sender As Object, ByVal e As LabelChangeEventArgs)

Partial Class UCInput
    Inherits System.Web.UI.UserControl

    Public Event OnLabelChanged As LabelChangeEventHandler

    Protected Sub OnButtonClick(sender As Object, e As EventArgs)
        Dim args As New LabelChangeEventArgs
        args.LabelText = TextBox1.Text
        RaiseEvent OnLabelChanged(Me, args)
    End Sub

End Class

User Control Output VB.net: You make a sub to receive the args and change the label.

用户控件输出VB.net:您创建一个子接收args并更改标签。

Public Sub LabelChange(sender As Object, args As LabelChangeEventArgs)
    If args IsNot Nothing Then
        Label1.Text = args.LabelText
    End If
End Sub

and in the parent page codefile you wire up the controls together.

并在父页面代码文件中将控件连接在一起。

Protected Sub WireHandles() Handles Me.Init
    AddHandler UCInput1.OnLabelChanged, AddressOf UCOutput1.LabelChange
    AddHandler UCInput2.OnLabelChanged, AddressOf UCOutput2.LabelChange
End Sub

Here's a download link to the src files Link

这是src文件链接的下载链接

#1


1  

The solution I found to this problem is from an old article from 2002 on Code Project. Here's the link to that article ASP.NET User Controls - Notify One Control Of Changes In Other Control. After reading that article and looking through sample code, here are the following changes I made to the code above.

我发现这个问题的解决方案来自2002年Code Code上的一篇旧文章。以下是该文章的链接ASP.NET用户控件 - 通知其他控件中的更改控件。阅读该文章并查看示例代码后,以下是对上述代码所做的以下更改。

There are no changes to the ASPX files except the following line in the Output ASPX file should be removed.

除了输出ASPX文件中的以下行应该被删除之外,ASPX文件没有任何更改。

<%@ Reference Control="~/Input.ascx" %>

First you add a new class called LabelChangeEventArgs.

首先,添加一个名为LabelChangeEventArgs的新类。

Public Class LabelChangeEventArgs
    Inherits EventArgs

    Private _LabelStr As String

    Public Property LabelText() As String
        Get
            Return _LabelStr
        End Get
        Set(value As String)
            _LabelStr = value
        End Set
    End Property
End Class

User Control Input VB.net: You add a Delegate and use the new LableChangeEventArgs class to handle the string.

用户控件输入VB.net:添加一个Delegate并使用新的LableChangeEventArgs类来处理字符串。

Public Delegate Sub LabelChangeEventHandler(ByVal sender As Object, ByVal e As LabelChangeEventArgs)

Partial Class UCInput
    Inherits System.Web.UI.UserControl

    Public Event OnLabelChanged As LabelChangeEventHandler

    Protected Sub OnButtonClick(sender As Object, e As EventArgs)
        Dim args As New LabelChangeEventArgs
        args.LabelText = TextBox1.Text
        RaiseEvent OnLabelChanged(Me, args)
    End Sub

End Class

User Control Output VB.net: You make a sub to receive the args and change the label.

用户控件输出VB.net:您创建一个子接收args并更改标签。

Public Sub LabelChange(sender As Object, args As LabelChangeEventArgs)
    If args IsNot Nothing Then
        Label1.Text = args.LabelText
    End If
End Sub

and in the parent page codefile you wire up the controls together.

并在父页面代码文件中将控件连接在一起。

Protected Sub WireHandles() Handles Me.Init
    AddHandler UCInput1.OnLabelChanged, AddressOf UCOutput1.LabelChange
    AddHandler UCInput2.OnLabelChanged, AddressOf UCOutput2.LabelChange
End Sub

Here's a download link to the src files Link

这是src文件链接的下载链接