[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 461
  • Last Modified:

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.
0
BlueEdger
Asked:
BlueEdger
  • 2
  • 2
1 Solution
 
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
 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now