views:

168

answers:

1

Hello,

I have a user control that contains a textbox. The user control is itself contained in another user control (parent).

When the text is changed (OnTextChanged event) I need a way to tell the parent user control.

So I would have something alongs the lines of :

<uc1:MyUserControl OnChanged="DoSomething" runat="server" ID="MyUserControl1">

and the OnChanged eventhandler would fire when the textbox's OnTextBoxChanged event fires.

Any idea or clue?

Thanks!

+1  A: 

Assuming a text box called ControlTextBox I have the following code behind my web user control:

public partial class MyUserControl : System.Web.UI.UserControl
{
    public event EventHandler Changed;

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void ControlTextBox_Changed(object sender, EventArgs e)
    {
        OnChanged();
    }

    protected virtual void OnChanged()
    {
        if (Changed != null)
            Changed(this, EventArgs.Empty);
    }
}

and the following in the ascx file:

<%@ Control Language="C#" AutoEventWireup="true" 
    CodeFile="MyUserControl.ascx.cs" Inherits="MyUserControl" %>

<asp:TextBox runat="server" ID="ControlTextBox" 
    OnTextChanged="ControlTextBox_Changed" AutoPostBack="true"/>

Then the code you give above gets fired correctly.

samjudson