The subject line of this post says it all! I am creating a "checkout" page and in the quantity column, I am creating a text box and filling it with the current quantity. This works just fine. Now, I want to allow the user to change the quantity which
triggers a function to run that will update the item total and grand total. I also want to restrict entries in the text box to only allow integers and no decimals.
I thought for sure the following code would work:
//Create a cell and put our data in it
c = new TableCell();
c.Attributes.Add("style", "text-align: center;"); //This will center textbox
r.Cells.Add(c);
TextBox t = new TextBox();
t.Width = 30;
t.Text = v;
t.Attributes.Add("style", "text-align: center;"); //This will center the text within the textbox
t.TextChanged += new EventHandler(t_TextChanged);
c.Controls.Add(t);
//Add a validater for the text box so it only accepts integers
RegularExpressionValidator regVal = new RegularExpressionValidator();
regVal.ControlToValidate = t.Text;
regVal.ErrorMessage = "Invalid Character. Please only use integers.";
regVal.ValidationExpression = "^\\d*";
The debug output never shows "Text Changed" no matter what I try to enter in my text box. I also never get any error message when I try to enter anything that is not an integer. I've done dynamic code like this many times, though never with the "TextChanged"
event (mostly on 'Click' events) and all my other code works... I don't understand why my event doesn't seem to be firing and the RegEx validator doesn't seem to do anything at all... Any ideas??
For the event to fire add the t.AutoPostBack =true;
For the validator you need to add the validator to the control list and also set the proper id of the control instead of t.Text set it to t.ID or t.ClientID
Please remember to mark as Answer if the post helps you out.
For the event to fire add the t.AutoPostBack =true;
For the validator you need to add the validator to the control list and also set the proper id of the control instead of t.Text set it to t.ID or t.ClientID
kingdutka
Member
1 Points
3 Posts
Need Help - Dynamically Created TextBox with TextChanged event handler and RegEx Validator
Feb 08, 2013 06:54 PM|LINK
The subject line of this post says it all! I am creating a "checkout" page and in the quantity column, I am creating a text box and filling it with the current quantity. This works just fine. Now, I want to allow the user to change the quantity which triggers a function to run that will update the item total and grand total. I also want to restrict entries in the text box to only allow integers and no decimals.
I thought for sure the following code would work:
//Create a cell and put our data in it c = new TableCell(); c.Attributes.Add("style", "text-align: center;"); //This will center textbox r.Cells.Add(c); TextBox t = new TextBox(); t.Width = 30; t.Text = v; t.Attributes.Add("style", "text-align: center;"); //This will center the text within the textbox t.TextChanged += new EventHandler(t_TextChanged); c.Controls.Add(t); //Add a validater for the text box so it only accepts integers RegularExpressionValidator regVal = new RegularExpressionValidator(); regVal.ControlToValidate = t.Text; regVal.ErrorMessage = "Invalid Character. Please only use integers."; regVal.ValidationExpression = "^\\d*";void t_TextChanged(object sender, System.EventArgs e) { Debug.Print("Text Changed"); //ShowCart(); }The debug output never shows "Text Changed" no matter what I try to enter in my text box. I also never get any error message when I try to enter anything that is not an integer. I've done dynamic code like this many times, though never with the "TextChanged" event (mostly on 'Click' events) and all my other code works... I don't understand why my event doesn't seem to be firing and the RegEx validator doesn't seem to do anything at all... Any ideas??
Talal Tayyab
Participant
902 Points
132 Posts
Re: Need Help - Dynamically Created TextBox with TextChanged event handler and RegEx Validator
Feb 08, 2013 07:42 PM|LINK
For the event to fire add the t.AutoPostBack =true;
For the validator you need to add the validator to the control list and also set the proper id of the control instead of t.Text set it to t.ID or t.ClientID
kingdutka
Member
1 Points
3 Posts
Re: Need Help - Dynamically Created TextBox with TextChanged event handler and RegEx Validator
Feb 08, 2013 07:57 PM|LINK
Genius! Everything is working now! THANKS!!!!!