[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.aspnet.webcontrols

Control.FormFind not finding controls

(Sarah R)

1/24/2003 7:48:00 PM

I am trying to create a class method which will set a label's cssClass
attribute programatically based on the validity of an associated
validator control. I thought it would be cool to be able to change a
label's (style) class on error, like I used to do in ASP all the time,
programatically. Sorry but the validators suck when it comes to that
because they wrap a font tag around the error message so the color is
not style sheet controlable!

I have the regular aspx page with the label control and validator
control:

<asp:label id="lbPassword" text="Password:" runat="server" />
<asp:textbox id="txtPassword" textmode="password" columns="25"
runat="server" />
<asp:RequiredFieldValidator id="valPassword"
controltovalidate="txtPassword" errormessage="" visible="true"
runat="server" />

In the code behind I import the 'package' of my class (excuse the java
lingo), create the class and then call the method. The method
parameters are the page's form object, and two strings of css classes
in my style sheet.

SetLabel oSL = new SetLabel();
sInfo = oSL.setCssClass(oForm, sLabelStyle, sErrorLabelStyle);

The problem lies in my method. I loop through the controls on the
page, get each label id, a string, parse it for the form inputs name -
a standard prefix, then concactenate it with the standard validation
prefix, and then attempt to find that validation control of that
name/id. But it returns a null object despite all my attempts. In the
code below, I always get "Control does not exist." I'm stumped. I hope
someone can help here.

public string setCssClass(HtmlForm form, string sLabelStyleRegular,
string sLabelStyleError)
{
string labelControlID;
string validationControl;
string[] controlPrefixes = {"txt", "r", "sel", "ck"};
string sInformation = "";

if(form != null)
{
foreach (Control ctlControl in form.Controls)
{
if (ctlControl.GetType().ToString() ==
"System.Web.UI.WebControls.Label")
{
//cast the control as a label
Label tempLabel = ctlControl as Label;

//get the nonprefixed name
labelControlID = tempLabel.ID;
labelControlID =
labelControlID.Substring(2,(labelControlID.Length-2));

//get validation controls id
validationControl = "val" + labelControlID;

//HERE'S THE PROBLEM!
//I've tried ctlControl.FindControl & form.FindControl
// based on information from this group
Control tempControl = form.FindControl(validationControl);

if (tempControl == null)
{
//the idea is to indentify the control here
//check it's .valid property and change the label's cssclass
sInformation = sInformation+"Control does not exist<br>";
}
}
}
}
return sInformation;
}


TIA,
Sarah R