Answered by:
Simple EmailAddress Textbox custom control

Question
-
User56700650 posted
I'm creating a custom control which contains a textbox, regular expression, and a few other properties, as well as a method Validate().
My problem is that when the server posts back all of the properties are null or their default values.Can anyone point me in the right direction?
<
mc:EmailTextBox ID="EmailTextBox1" runat="server" />protected override void CreateChildControls( ) { this.Controls.Clear( ); this.Controls.Add( this.TxtEmail ); }
public bool Validate( ) { bool isValid = false; if( this.IsRequired ) { if( this.TxtEmail.Text.Length == 0 ) { isValid = false; } } if( !String.IsNullOrEmpty( this.RegularExpression ) ) { Regex reg = new Regex( this.RegularExpression ); if( !reg.IsMatch( this.TxtEmail.Text ) ) <--- is always null or empty { isValid = false; } else { isValid = true; } } else { isValid = true; } return isValid; }
Tuesday, June 17, 2008 1:21 PM
Answers
-
User-16411453 posted
You have to declare this.TxtEmail as private or public in declarations. CreateChildControls just creates controls to be rendered on the page.
Validate has no idea that the control exist.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, June 18, 2008 1:59 PM
All replies
-
User1790760290 posted
CreateChildControls( ) happened after validation, so you may need to move it before validation event like page_init
Tuesday, June 17, 2008 1:27 PM -
User-16411453 posted
Forms require event handlers
OnInit instead of CreateChildControls
Tuesday, June 17, 2008 4:35 PM -
User56700650 posted
For whatever reason if I inherit from TextBox instead of WebControl it works fine.
Wednesday, June 18, 2008 8:59 AM -
User-16411453 posted
You have to declare this.TxtEmail as private or public in declarations. CreateChildControls just creates controls to be rendered on the page.
Validate has no idea that the control exist.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, June 18, 2008 1:59 PM