Composite Control con validator che fanno riferimento a controlli esterni...
di Andrea Zani, in .NET, venerdì 5 maggio 2006 ore 20.15
Neanche Lina Wertmuller faceva titoli lunghi come i miei... Comunque... In questi giorni sto realizzando dei web control utili per un progetto che sto seguendo dall'inizio del millennio a questa parte. Per snellire il lavoro mi sono creato dei composite control, ma nell'ultimo caso avevo riscontrato un problema. Banalmente: nel composite control avevo aggiunto un validator - nel dettaglio, un comparevalidator - che doveva far riferimento ad un webcontrol esterno al composite control. Con la normale assegnazione dell'ID del controllo da verificare ottenevo l'errore di controllo non trovato.
Con l'aiuto di Nostromo - che ringrazio - da questo post nel forum, si è scoperto che il problema è nel metodo CheckControlValidationProperty. Nel dettaglio cerca il controllo nel composite control corrente:
protected void CheckControlValidationProperty(string name, string propertyName)
{
Control control1 = <u>this.NamingContainer.FindControl(name);</u>
if (control1 == null)
{
throw new HttpException(HttpRuntime.FormatResourceString("Validator_control_not_found", name, propertyName, this.ID));
}
if (BaseValidator.GetValidationProperty(control1) == null)
{
throw new HttpException(HttpRuntime.FormatResourceString("Validator_bad_control_type", name, propertyName, this.ID));
}
}Per risolvere ho dovuto estendere la classe CompareValidator modificando quel metodo per fare in modo che il controllo lo cercasse nella pagina. Il risultato è il seguente:
public class myCompareValidatorControl:CompareValidator
{
void CheckControlValidationProperty(string name, string propertyName)
{
Control control1 = Page.FindControl(name);
if (control1 == null)
{
throw new HttpException("Validator_control_not_found");
}
if (BaseValidator.GetValidationProperty(control1) == null)
{
throw new HttpException("Validator_bad_control_type");
}
}
protected override bool EvaluateIsValid()
{
string text1 = GetControlValidationValue(base.ControlToValidate);
if (text1.Trim().Length == 0)
{
return true;
}
string text2 = string.Empty;
if (this.ControlToCompare.Length > 0)
{
text2 = GetControlValidationValue(this.ControlToCompare);
}
else
{
text2 = this.ValueToCompare;
}
return BaseCompareValidator.Compare(text1, text2, this.Operator, base.Type);
}
string GetControlValidationValue(string name)
{
Control control1 = Page.FindControl(name);
if (control1 == null)
{
return null;
}
PropertyDescriptor descriptor1 = GetValidationProperty(control1);
if (descriptor1 == null)
{
return null;
}
object obj1 = descriptor1.GetValue(control1);
if (obj1 is ListItem)
{
return ((ListItem) obj1).Value;
}
if (obj1 != null)
{
return obj1.ToString();
}
return string.Empty;
}
protected override bool ControlPropertiesValid()
{
if (this.ControlToCompare.Length > 0)
{
CheckControlValidationProperty(this.ControlToCompare, "ControlToCompare");
if (string.Compare(base.ControlToValidate, this.ControlToCompare, true, CultureInfo.InvariantCulture) == 0)
{
throw new Exception("Validator_bad_compare_control");
}
}
else if ((this.Operator != ValidationCompareOperator.DataTypeCheck) && !BaseCompareValidator.CanConvert(this.ValueToCompare, base.Type))
{
throw new Exception("Validator_value_bad_type");
}
return true;
}
}
Il problema è che il metodo di base CheckControlValidationProperty utilizza molte funzioni interne che ho dovuto riscrivere, tra cui GetControlValidationValue. Ma il risultato c'è, il tutto funziona. E vaiii!
Queste sì che cono soddisfazioni!
Nella stessa categoria
Triplet class non è un oggetto curioso, è utilissimo!!!
Entity Framework e stranezze(*)
Entity Framework. Una tabella due Entity senza discriminazioni
Entity Framework e più tabelle in una entity
L'Entity Framework e le custom class coinvolte nei where...
Entity Framework e l'ereditarietà (Single Table Inheritance)
I più letti del mese



















Per inserire un commento, devi avere un account.
Fai il login e torna a questa pagina, oppure registrati alla nostra community.