Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating Dynamic Control

Status
Not open for further replies.

IQ

Programmer
Mar 15, 2000
20
0
0
GB
Hi,

I may be missing something obvious, but how do you use the custom validator control to validate a dynamically created control?

I have created a dynamic textbox in Page_Init.
I have created a custom validator control also in Page_Init.

However what code do I need to do to link up the two ?

 
drop a textbox and validator on your page and set it up using the propertys window. Then look at the HTML generated. You can pretty much translate those property settings and use them in your code behind in the page_init
 
//create control
TextBox myControl = new TextBox();
myControl.Id = "ctrl";

//create validator
RequiredFieldValidator myValidator = new RequiredFieldValidator();
//set the "control to validate" to the id of your control
myValidator.ControlToValidate = myControl.Id;


//add both controls to the same container, the Page in this example.
Page.Controls.Add( myControl );
Page.Controls.Add( myValidator );


 
This code is fine for a required field validator. When you are trying to use the custom validator it is necessary to run server code.

When you use the custom validator via the designer you specify the OnServerValidate to point to the server code.

It is this bit that I do not know how to specify.

Any ideas?
 
Same thing, but add a delegate to the event. Type "myValidator.OnServerValidate += " and Visual Studio will pop up an auto-complete that will add a space for the delegate if you press TAB TAB.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top