We have conditions that will dynamically add TextBoxes to an ASP User Control like this:
if (conditionIsTrue) {
TextBox textField = new TextBox();
Panel.controls.Add(textField);
}
I would like to add an event listener that left-pads the TextBox value when the user unfocuses or is done editing the TextBox.
I have tried the following C# implementation but for some reason the values are not being handled by the listener.
if (conditionIsTrue) {
TextBox textField = new TextBox();
textField.TextChanged += new EventHandler(LeadingZero_Handler);
Panel.controls.Add(textField)
}
public void LeadingZero_Handler(object sender, EventArgs e)
{
int FieldLength = 10;
if((TextBox)sender.Text.Length < FieldLength)
{
(TextBox)sender.Text = (TextBox).sender.Text.PadLeft(FieldLength,'0');
}
}
Is there is a way that I could implement the event listener in JavaScript on the ASP Control, but add the JavaScript listener attribute during/when the TextBox is being dynamically generated in the C# code?
textField.AutoPostBack = true;