Handling TextChanged event

  • Thread starter Thread starter Nikhil Patel
  • Start date Start date
N

Nikhil Patel

Hi all,
I need to handle TextChanged event of more than 30 textboxes on my page.
All I want to do in the event handler is get the current value of the
textbox and save it in an appropriate column of a DataSet. So I was thinking
that if each of these textboxes can store their corresponding column name as
its property, I dont have to write 30 different event handlers. How can I do
this?

Thanks...
-Nikhil
 
Hello,

Breaking this down into two parts

1) How to handle multiple textChanged events with ONE method
You have to link up the event hander to the event on each text box.

TextBox1.TextChanged += new System.EventHandler(HandleTextChanged());
TextBox2.TextChanged += new System.EventHandler(HandleTextChanged());
TextBox3.TextChanged += new System.EventHandler(HandleTextChanged());
private void HandleTextChanged(object sender, System.EventArgs e) {}

2) Get the name and value from the textbox, set the dataset values, update
the database.
a. Load the dataset in the PageLoad and store the dataset in a member
variable.

b. Code for TextChanged Event

private void HandleTextChanged(object sender, System.EventArgs e) {

TextBox t = (TextBox) sender;
string strColName = t.ID;
string strValue = t.Text;

// Set the values in the dataset here

}

c. In the PreRender event do the actual updating of the database with the
dataset. You don't want to update in HandleTextChanged because it will get
fired for each textbox that has changed. You only want to update once after
all cached TextChanged events have fired.

Hope that helps,

Tom Krueger
Blue Pen Solutions
http://www.BluePenSolutions.com
 
Nikhil said:
Hi all,
I need to handle TextChanged event of more than 30 textboxes on my page.
All I want to do in the event handler is get the current value of the
textbox and save it in an appropriate column of a DataSet. So I was thinking
that if each of these textboxes can store their corresponding column name as
its property, I dont have to write 30 different event handlers. How can I do
this?

Thanks...
-Nikhil

You have two ways:
1. The simple one, just add it as an attribute on your control, it
should stick (using the Attributes property).
2. The complex one, inherit from TextBox to create your own control that
stores this information as a property but doesn't render it.

Be aware if you do the first one that you absolutely and completely need
to sanitize the input you would get back from your server when reading
the attribute, as it could have been changed by a malicious user, and
you could end up with some wierd problems...
 
Back
Top