User Control Event Handlers

Discussion in 'ASP .Net' started by rn5a, Oct 8, 2006.

  1. rn5a

    rn5a Guest

    Consider the following user control which resides in Address.ascx:

    <script runat="server">
    Public Property Address() As String
    Get
    Address = txtAddress.Text
    End Get
    Set(ByVal value As String)
    txtAddress.Text = value
    End Set
    End Property

    Public Property Country() As String
    Get
    Country = txtCountry.Text
    End Get
    Set(ByVal value As String)
    txtCountry.Text = value
    End Set
    End Property
    </script>
    <asp:TextBox ID="txtAddress" runat="server"/>
    <asp:TextBox ID="txtCountry" runat="server"/>

    This is how I am using the user control in a ASPX page:

    <%@ Register TagPrefix="UC" TagName="UAddress" Src="Address.ascx" %>
    <script runat="server">
    Sub CopyAddress(obj As Object, ea As EventArgs)
    ..........
    ..........
    End Sub
    </script>
    <form runat="server">
    <UC:UAddress ID="ud" runat="server">
    <asp:CheckBox ID="chkAddress" OnCheckedChanged="CopyAddress"
    AutoPostBack="true" Text="Copy Address" runat="server"/>
    </form>

    The above ASPX page will render the 2 TextBoxes (using the user
    control) & a CheckBox. Note that when the CheckBox is checked/unchecked
    by the user, the sub named 'CopyAddress' gets executed.

    Now I want the 2 TextBoxes to also execute the 'CopyAddress' sub using
    the OnTextChanged event of the 2 TextBoxes but since the 2 TextBoxes
    actually reside in the user control, how do I fire the OnTextChanged
    event handler of the 2 TextBoxes so that the 'CopyAddress' sub gets
    executed?
     
    rn5a, Oct 8, 2006
    #1
    1. Advertisements

  2. If you need a control to fire an event on a parent: Bubble up the event to
    the parent container using RaiseEvent.

    1. Create an event

    Public Event BubbleUpAlreadyRegistered As EventHandler(Of EventArgs)


    2. Raise the event

    RaiseEvent BubbleUpAlreadyRegistered(sender, e)


    3. Handle the event

    Protected Sub RegisterUnit1_BubbleUpUnitAlreadyRegistered(ByVal sender As
    Object, ByVal e As System.EventArgs) Handles
    RegisterUnit1.BubbleUpAlreadyRegistered



    If you need to do something on a control when the parent event is fired:
    Fire the method on the control from code behind.
     
    Cowboy \(Gregory A. Beamer\), Oct 8, 2006
    #2
    1. Advertisements

  3. rn5a

    rn5a Guest

    Where should the event be bubbled up using RaiseEvent - in the ASCX
    page?

    Could you please link me to some articles on this topic?
     
    rn5a, Oct 8, 2006
    #3
  4. rn5a

    rn5a Guest

    Gregory, this is what I came up with (in the ASCX page):

    <script runat="server">
    Public Event TextChanged(ByVal obj As Object)

    Public Sub OnTextChanged(ByVal obj As Object, ByVal ea As
    EventArgs) Handles txtAddress.TextChanged
    RaiseEvent TextChanged(Me)
    End Sub
    </script>

    Now how do I handle the event?

    Please respond
     
    rn5a, Oct 8, 2006
    #4
  5. rn5a

    rn5a Guest

    Gregory, let me explain my problem a bit further. Assume that when a
    user comes to the ASPX page that encapsulates the user control (which
    has 2 TextBoxes) for the first time, by default, the first TextBox is
    pre-filled with the text, say, FRANCE, the second TextBox is pre-filled
    with the text, say, GERMANY & the CheckBox is checked.

    Now suppose the user changes the text in the first TextBox to, say,
    ITALY. Usually the OnTextChanged event of a TextBox gets fired when the
    TextBox loses focus (the OnTextChanged event doesn't fire as the user
    goes on typing though the text is changing in the TextBox). Now since
    the CheckBox is checked, I want that when the first TextBox loses focus
    (say, the user presses the Tab key from the keyboard so that the cursor
    shifts to the second TextBox or the user simply clicks anywhere in the
    ASPX page), the text in the second TextBox should automatically change
    to ITALY.

    This is where I am getting stuck. This is my code in the ASCX page:

    <script runat="server">
    Delegate Sub TextChangedHandler(ByVal obj As Object, ByVal ea As
    EventArgs)
    Public Event TextChanged As TextChangedHandler

    Public Sub ChangedText(ByVal val As TextChangedHandler)
    AddHandler TextChanged, val
    End Sub

    Protected Sub txtAddress_TextChanged(ByVal obj As Object, ByVal ea
    As EventArgs) Handles txtAddress.TextChanged
    RaiseEvent TextChanged(obj, ea)
    End Sub
    </script>

    Now in which sub in the ASPX page do I call the ChangedText sub which
    exists in the ASCX page? I can't call it in the Page_Load sub in the
    ASPX page since I want the event to be raised when the first TextBox
    loses focus. Neither can I call it in the CopyAddress sub because
    clicking the already checked CheckBox will uncheck the CheckBox & under
    such circumstances, I don't want the text in the second TextBox to be
    the same as the text in the first TextBox unless & until the user
    explicitly types the same text in the second TextBox!
     
    rn5a, Oct 8, 2006
    #5
  6. rn5a

    rn5a Guest

    Gregory, I have resolved the issue....so please do not bother to ponder
    over this post......
     
    rn5a, Oct 8, 2006
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.