Sharing OnTextChanged Event between Multiple TextBoxes

I have a page with multiple textboxes and I have set them up to all share the same OnTextChanged event.  Which all works lovely, but I would like to improve upon this to do certain things dependent on certain textboxes, and so it would be really handy to be able to pick up which textbox generated the event call.  I cant see any way of doing this in C#, does anybody know how to do this?  I really dont want to create loads of different OnTextChanged events.
LVL 1
BlueEdgerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bobbit31Commented:
private void MyText_Changed(object sender, System.EventArgs e)
    {
        Textbox t = (Textbox) sender;
        if (t.name.equals("Text1")) {
          ...
        } else if (t.name.equals("Text2")) {
          ...
        } else {
          ...
        }
    }

just point both textboxes to call the above on change.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hismightinessCommented:
I am not sure you CAN do this in C#.  In VB.Net, it is easy:

Private Sub TextBox1_OnTextChanged(ByVal sender as Object, ByVal e As ServerEventArgs) Handles TextBox1.OnTextChanged, TextBox2.OnTextChanged
   'logic
End Sub
0
hismightinessCommented:
Scratch that.  I just found this on Google:

--------------- BEGIN QUOTE ---------------
In C# you explicitly add the event handler to an object, so adding the same
eventhandler to multiple instances is not a problem, I have not idea how you
would do it in VB.NET. If it's not possible, you could create a seperate
function you could call from each individual event handler.

The following C# code illustrates what I mean;

// add event handlers
textBox1.Leave += new EventHandler(TextBox_Leave);
textBox2.Leave += new EventHandler(TextBox_Leave);

private void TextBox_Leave(object sender, EventArgs e)
{
TextBox tb = sender as TextBox; // if it cannot be cast, it will return
null
if(tb != null)
{
// do something with the textbox
}
}
--------------- END QUOTE ---------------
0
BlueEdgerAuthor Commented:
bobbit31, I get the following error:

'System.Web.UI.WebControls.TextBox' does not contain a definition for 'name'
0
BlueEdgerAuthor Commented:
Its OK, Its t.ID.Equals.

Fussy C#.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.