Dissapearing Validators

G

Guest

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,theControl);

// 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 );
}
 
G

Guest

First off sorry about the multiple post, but if I clicked the inform me of replies button it kept informing me that the post had failed.

Secondly I have now solved this problem, but it appears to be due to some strange behaviour regarding table cells.
If I add text to a cell after adding controls to it the text 'overwrites' the controls and hence my validators were dissapearing due to the I had added to the cell for formatting reasons.

Can anyone confirm that this is the correct behaviour as to me it seems a little unusual?


Tom Pearson said:
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,theControl);

// 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 );
}
 

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

Top