Custom Composite control error

G

Guest

What is wrong with this code?
I get no errors on compilation of my assembly

But On compilation on my Webpage i keep getting this error
"Unable to find control id 'FreeBox1' referenced by the 'ControlToValidate'
property of ''"



public class FreeBox : TextBox{

private Label lblTitle = new Label();
private RequiredFieldValidator req ;

public string Title {
get { return lblTitle.Text; }
set { lblTitle.Text = value; }
}
public string ReturnValue {
get { return this.Text; }
set { this.Text = value; }
}

protected override void OnInit(EventArgs e) {
req = new RequiredFieldValidator();
req.ControlToValidate = this.ID;
req.ErrorMessage = "*";
Controls.Add(req);
}

protected override void Render(HtmlTextWriter writer) {
lblTitle.RenderControl(writer);
base.Render(writer);
req.RenderControl(writer);
}

}

original code from this tutorial
"http://www.codeproject.com/aspnet/textboxwithvalidator.asp"
 
L

Laurent Bugnion

Hi,
What is wrong with this code?
I get no errors on compilation of my assembly

But On compilation on my Webpage i keep getting this error
"Unable to find control id 'FreeBox1' referenced by the 'ControlToValidate'
property of ''"

req.ControlToValidate = this.ID;

<snip>

You should use ClientID instead of ID. The ID is the value set by the
developer in the ASPX code or in the code-behind for the control.
However, if you have more than one control on the same page, there is a
risk that the ID won't be unique. This is why the compiler "computes" a
unique ID, which can be accessed using ClientID. Typically, it will be
something like "parentId_myId", where "parentId" is the parent's ID, and
"myId" is the control's ID.

HTH,
Laurent
 

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,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top