How to make a require textbox

A

ad

I want to make a custom web control which composite with a TextBox and a
RequiredFieldValidator;
I set the RequiredFieldValidator's ControlToValidate to the TextBox's ID
(like code below)
But when I use the coustom web control in web form, it throw an exception:
The ControlToValidate can not be empty
How can I set the RequiredFieldValidator's ControlToValidate property?


//##########################################################################
######
public class RequireTextBox : Control, INamingContainer
{
private TextBox box;
RequiredFieldValidator rv;


public string Text
{
get
{
EnsureChildControls();
return box.Text;
}
set
{
EnsureChildControls();
box.Text = value;
}
}

protected override void CreateChildControls()
{
box = new TextBox();
box.Width=30;
Controls.Add(box);

rv= new RequiredFieldValidator();
rv.ControlToValidate=box.ID;
Controls.Add(rv);
}

}
 
C

Craig Deelsnyder

I want to make a custom web control which composite with a TextBox and a
RequiredFieldValidator;
I set the RequiredFieldValidator's ControlToValidate to the TextBox's ID
(like code below)
But when I use the coustom web control in web form, it throw an
exception:
The ControlToValidate can not be empty
How can I set the RequiredFieldValidator's ControlToValidate property?
protected override void CreateChildControls()
{
box = new TextBox();
box.Width=30;
Controls.Add(box);

rv= new RequiredFieldValidator();
rv.ControlToValidate=box.ID;
Controls.Add(rv);
}

}

try setting the ID of the box before adding it to the controls collection.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top