Custom Control needs ControlToValidate

S

Steven Nagy

Hi all,

I have a custom user control in an ASP.NET app (.NET 2.0).
This control needs a public property called "ControlToValidate" which
accepts strings.
I need to be able to declare it much as you would with a validator
control, like this:


What is important is that my custom control is able to get the
ClientID of the control passed in.
The above code would only pass "myTextbox" as a string, not the actual
textbox control.

I am curious how to do this. If I were to user a
RequiredFieldValidator, I could set ControlToValidate as per my above
usage. So its this behaviour I want to replicate.

I used reflector to view the RequiredFieldValidator's implementation
of ControlToValidate and it simply inherits from BaseValidator. I
tried using this inheritance also, but kept getting compile errors
because BaseValidator ultimately inherits from Control, not
UserControl (sif that should matter).

Googling brought me no love. I suspect there is an attribute I need to
use, possibly a TypeConverter somewhere, just not sure how.

Any help would be greatly appreciated.

Cheers,
Steven
 
M

MasterGaurav \(www.edujini-labs.com\)

What is important is that my custom control is able to get the
ClientID of the control passed in.
The above code would only pass "myTextbox" as a string, not the actual
textbox control.


Control ctrl = base.NamingContainer.FindControl(this.ControlToValidate);
if(ctrl != null)
{
TextBox box = ctrl as TextBox;
if(box != null)
{
string value = box.Text;
}
}


HTH

--
Happy Hacking,
Gaurav Vaish | www.mastergaurav.com
www.edujini-labs.com
http://eduzine.edujini-labs.com
-----------------------------------------
 
S

Steven Nagy

Control ctrl = base.NamingContainer.FindControl(this.ControlToValidate);
if(ctrl != null)
{
TextBox box = ctrl as TextBox;
if(box != null)
{
string value = box.Text;
}

}

HTH

Ok so in your example, the string that gets passed is something like
"txtUserName".
However, at runtime the textbox id is actually
"MainForm_SomeSubConrol_txtUserName".
Will this code still work 100% if the form has two "txtUserName"
textboxes?
I haven't used "base.NamingContainer" before; is this the container
that is holding the current control?
How would your example work in this case:

<asp:panel id="pnlSomeStuff" runat="server">
<asp:Textbox id="txtUserName" runat="server" />

.... because the textbox is not in the same scope as the custom
control.

I appreciate your help,
Steven Nagy
 
M

MasterGaurav \(www.edujini-labs.com\)

Ok so in your example, the string that gets passed is something like
"txtUserName".
However, at runtime the textbox id is actually
"MainForm_SomeSubConrol_txtUserName".
Will this code still work 100% if the form has two "txtUserName"
textboxes?

I missed out your "Anyone?" posting...

The textbox-ID is never "MainForm_SomeSubControl_txtUserName". It is the
value of "ClientID".
I haven't used "base.NamingContainer" before; is this the container
that is holding the current control?

Yes, NamingContainer is the container holding the current control.
How would your example work in this case:

<asp:panel id="pnlSomeStuff" runat="server">
<asp:Textbox id="txtUserName" runat="server" />


... because the textbox is not in the same scope as the custom
control.

In this case, you can use (from inside uc1:MyControl):

this.Page.FindControl("txtUserName")

Note that UserControl also implements INamingContainer. So, for the
uc1-control, the lookup would be within MyControl since that's the
NamingContainer for the control.

Btw, for all these complex-resolving issues, I would recommend that you
design a WebControl rather than UserControl (because UserControl becomes the
naming container).

Anyway, Page.FindControl("...") should give you the expected results.


--
Happy Hacking,
Gaurav Vaish | www.mastergaurav.com
www.edujini-labs.com
http://eduzine.edujini-labs.com
-----------------------------------------
 

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,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top