RaiseEvent between User Controls where there are multiple instances of the same controls on one page ASP.net

StackOverflow https://stackoverflow.com/questions/17975130

Domanda

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.

Here is the ASPX for the parent page:

<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:

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

User Control Input 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:

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

User Control Output 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).

È stato utile?

Soluzione

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.

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

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

First you add a new class called 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.

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.

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top