473,394 Members | 1,845 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Validator problem

I create controls and validators dynamically dependent on data at runtime.
I create the control then the relevant validator(s) for it assigning the Control.ID as the control to validate.
These controls and validators are then added to a table for display to the user.
This code did seem to work originally, but now does not amd I am stumped as to why. There have been some code

changes, but not to the methods below and I have pretty much undone all these changes but to no avail.
I assume that the ID I pass will be prefixed with the correct naming container when the User Control the objects

live on is rendered with the page as I don't have a could not find code to validate message. The validator is

simply not appearing at the client side.
I can see the space I put in the cell which contains the validator (see in code) and obviously the cell exists

also. But no validator.

Any help on this would be appreciated.

Cheers

Tom

This is the code below------
/// <summary>
/// Create the required control, validator etc for the parameter
/// </summary>
/// <param name="parameter">The parameter info</param>
private void CreateControl( FunctionParameter parameter, int index )
{
// Get the display name
string displayName = parameter.DisplayName;

// Get the control Id
string controlId = parameter.DataName;

// Create the return value
WebControl theControl = null;

// Create an array of BaseValidators to add to
ArrayList validators = new ArrayList( );

// if the control isn't visible store the required default value
if( !parameter.Visible )
{
// Create a text box to store the default value in
TextBox defaultTextBox = new TextBox( );

// get the default value for this control
if(parameter.Value != null)
defaultTextBox.Text = parameter.Value.ToString();
// hide the textbox
defaultTextBox.Visible = false;
theControl = defaultTextBox;
}

// Create the correct input control
else
{
// Check if this parameter has a list of predefined values
if( (string)parameter.PredefinedValues != "NotSet" )
{
// If we have a list of predefined values we need a list box
DropDownList dropDownList = new DropDownList( );
dropDownList.DataSource = GetListItems( (string)parameter.PredefinedValues

);
dropDownList.DataTextField = "DisplayValue";
dropDownList.DataValueField = "StoredProcValue";
dropDownList.DataBind( );
theControl = dropDownList;
}

// Create the correct type of input control
else
{
// Create the control dependent on type
// Switch on the DataType forced to upper characters
switch( (parameter.DataType).ToUpper( ) )
{
case INT:
// Create a text box control for integer input
TextBox txtBox = new TextBox( );
theControl = txtBox;
// Create a length validator for an int
RegularExpressionValidator theIntValidator = new

RegularExpressionValidator( );
theIntValidator.ControlToValidate = controlId;
theIntValidator.ErrorMessage = displayName + " must

contain integers only";
theIntValidator.Text = "*";
theIntValidator.ValidationExpression = INT_REG_EXP;
validators.Add( theIntValidator );
break;
case DECIMAL:
// Create a text box control for free input
TextBox decimalBox = new TextBox( );
theControl = decimalBox;
// Create a length validator for a decimal
RegularExpressionValidator theDecimalValidator = new

RegularExpressionValidator( );
theDecimalValidator.ControlToValidate = controlId;
theDecimalValidator.ErrorMessage = displayName + " must

contain numeric characters and a simgle point only";
theDecimalValidator.Text = "*";
theDecimalValidator.ValidationExpression =

DECIMAL_REG_EXP;
validators.Add( theDecimalValidator );
break;
case VARCHAR:
case NVARCHAR:
// Create a text box control for the input
TextBox varCharBox = new TextBox( );
theControl = varCharBox;
// Create a length validator for a varchar
RegularExpressionValidator theVarCharValidator = new

RegularExpressionValidator( );
theVarCharValidator.ControlToValidate = controlId;
theVarCharValidator.ErrorMessage = displayName + " must

contain less than " + parameter.DataLength + " alpha characters";
theVarCharValidator.Text = "*";
theVarCharValidator.ValidationExpression = VARCHAR_REG_EXP

+ parameter.DataLength+ "}";
validators.Add( theVarCharValidator );
break;
case BIT:
// Create the drop down list with true and false
DropDownList boolean = new DropDownList( );
ListItem liTrue = new ListItem( "True", "TRUE" );
ListItem liFalse = new ListItem( "False", "FALSE" );
boolean.Items.Add( liTrue );
boolean.Items.Add( liFalse );
theControl = boolean;
break;
case DATETIME:
// Create a text box control for the input
TextBox dateTimeBox = new TextBox( );
theControl = dateTimeBox;
// Create a length validator for a varchar
RegularExpressionValidator theDateTimeValidator = new

RegularExpressionValidator( );
theDateTimeValidator.ControlToValidate = controlId;
theDateTimeValidator.ErrorMessage = displayName + " must

contain a valid date in the format dd/mm/yy";
theDateTimeValidator.Text = "*";
theDateTimeValidator.ValidationExpression =

DATETIME_REG_EXP;
validators.Add( theDateTimeValidator );
break;
default:
// Unkown, throw exception
throw new Exception( "CRITERIA SELECTOR: Unknown Data Type

" + (string)parameter.DataType );
}
}

// Set the id to be that of the DataName
theControl.ID = controlId;

// Give all controls a fixed width for display purposes
theControl.Width = Unit.Pixel( 100 );

// set the text properties
theControl.Font.Size = FontUnit.Point( FONT_SIZE );

// If this is mandatory add a required field validator
if( parameter.Optional != true )
{
RequiredFieldValidator mandatoryValidator = new RequiredFieldValidator( );
mandatoryValidator.ControlToValidate = theControl.ID;
mandatoryValidator.ErrorMessage = displayName + " is a mandatory field";
mandatoryValidator.Text = "*";
validators.Add( mandatoryValidator );
}

// Create any specialist regular expression validators
if( parameter.RegExpression != "" )
{
// Create the regular expression validator
RegularExpressionValidator theValidator = new RegularExpressionValidator(

);
theValidator.ControlToValidate = theControl.ID;
theValidator.ErrorMessage = displayName + ": " +

(string)parameter.RegExpressionErrorMsg;
theValidator.Text = "*";
theValidator.ValidationExpression = (string)parameter.RegExpression;
validators.Add( theValidator );
}
}

this.paramControls.Add(parameter.DataName,theContr ol);

// Add the tool tip
theControl.ToolTip = parameter.Description;

// Need to add the control to the correct table
// So initialise our control to the optional table
Table theTable = tblOptionalParameters;

// Check if the parameter is visible
if( parameter.Visible == false )
{
theTable = tblNotVisible;
}
// If visible check if the parameter is mandatory
else if( parameter.Optional == false )
{
// If so assign it to the mandatory table
theTable = tblMandatoryParameters;
}

// Add the control, label and validator to the correct table
AddToTable( theTable, theControl, validators, displayName, parameter.DataName,

(parameter.DataType).ToUpper( ), index );
}

/// <summary>
/// Add a parameter to the table
/// </summary>
/// <param name="theTable">The table to add the control to</param>
/// <param name="theControl">The control to add</param>
/// <param name="theValidator">The validators</param>
/// <param name="label">The display name</param>
/// <param name="paramName">The stored proc parameter name</param>
/// <param name="type">The parameter type</param>
private void AddToTable( Table theTable, Control theControl, ArrayList validators, string label,

string paramName, string type, int index )
{
// Create the table cells and row
Table paramTable = new Table( );
TableCell paramNameCell = new TableCell( );
TableCell paramTypeCell = new TableCell( );
TableCell paramIndexCell = new TableCell( );
TableCell labelCell = new TableCell( );
TableCell controlCell = new TableCell( );
TableCell validatorCell = new TableCell( );
TableRow theTableRow = new TableRow( );

// Add the parameter name to the table and hide it
paramNameCell.Text = paramName;
paramNameCell.Visible = false;

// Add the parameter type to the table and hide it
paramTypeCell.Text = type;
paramTypeCell.Visible = false;

// Add the parameter index to the table and hide it
paramIndexCell.Text = index.ToString( );
paramIndexCell.Visible = false;

// Add the controls label
labelCell.Text = label;
labelCell.Width = Unit.Pixel( 100 );
labelCell.Font.Size = FontUnit.Point( FONT_SIZE );
labelCell.HorizontalAlign = HorizontalAlign.Right;

// Add the control
controlCell.Controls.Add( theControl );
controlCell.Width = Unit.Pixel( 100 );
controlCell.HorizontalAlign = HorizontalAlign.Left;

// Add the validator(s) if there are any
for( int i = 0; i< validators.Count; ++i )
{
validatorCell.Controls.Add( (Control)validators[ i ] );
}

// Give the validator cell a fixed width for display purposes
validatorCell.Width = Unit.Pixel( 20 );
validatorCell.Text = " ";
validatorCell.Visible = true;

// Add the cells to the row
theTableRow.Cells.Add( paramNameCell );
theTableRow.Cells.Add( paramTypeCell );
theTableRow.Cells.Add( paramIndexCell );
theTableRow.Cells.Add( labelCell );
theTableRow.Cells.Add( controlCell );
theTableRow.Cells.Add( validatorCell );

// Add the row to the table
paramTable.Rows.Add( theTableRow );

// Put this whole table in a single cell
TableCell tableCell = new TableCell( );
tableCell.Controls.Add( paramTable );

// Has the last row got the maximum number of cells
// Create new row if this is the title row or no rows
if( theTable.Rows.Count <= 1 || theTable.Rows[ theTable.Rows.Count -1 ].Cells.Count ==

MAX_CELLS )
{
// If so add a new row
TableRow newRow = new TableRow( );
newRow.Width = Unit.Percentage( 100 );
theTable.Rows.Add( newRow );

}

// Now add the parameter table cell
theTable.Rows[ theTable.Rows.Count -1 ].Cells.Add( tableCell );
}

Jul 21 '05 #1
0 2205

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: johnleemk | last post by:
I download the source for W3C's validator from http://validator.w3.org/source/ to validate pages on my own server. I commented a block of code that checks for non-public hosts so the validator...
8
by: Spartanicus | last post by:
The document at http://homepage.ntlworld.com/spartanicus/custom_dtd.htm uses a custom DTD, the w3c validator validates it but with this warning: "Unknown Parse Mode! The MIME Media Type...
2
by: VSK | last post by:
Hi all, I have a .ascx file with dropdownbox (SSN, EmpName) textbox submit button regular expression validator( controltovalidate is the above textbox) Now i want to change the Regular...
0
by: TN Bella | last post by:
Hi, I am trying to get my compare validator to fire properly...Since I have panels the validator wouldn't work properly, the app would fire right but would insert the data regardless and the...
3
by: Angelos Karantzalis | last post by:
Hi guys, I've a small problem with validators. I'm building a single .aspx file that handles all my form posts. I need to be using ASP.NET validators ( or subclasses thereof ), so what i do to...
8
by: pmud | last post by:
Hi, I am using a compare validator in asp.net application(c# code). This Custom validator is used for comparing a value enterd by the user against the primary key in the SQL database. IF the...
0
by: Tom Pearson | last post by:
I create controls and validators dynamically dependent on data at runtime. I create the control then the relevant validator(s) for it assigning the Control.ID as the control to validate. These...
0
by: Gonza | last post by:
Hi group, i'm trying to add a customvalidator control to a custom web control. The problem is i'm getting a "Unable to find control id..." exception. Here is the code: public class CuitTextBox :...
37
by: Prisoner at War | last post by:
Actually, it doesn't have to be a blockquote...but I'm at my wits' end: I want to make bold several lines of text which have a pair of <br /tags between them...seems like the <b></bdo not "carry...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.