473,403 Members | 2,323 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,403 software developers and data experts.

Dissapearing Validators

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
1 1991
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" wrote:
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 #2

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

Similar topics

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...
6
by: Mark | last post by:
We have Validators embedded in an asp table server control. The table server control is necessary and cannot be replaced. We want to apply CSS formatting to the validators, but the validators...
3
by: John Blair | last post by:
Hi, I have validators outside of a datagrid (for adding a new grid row) - however when i click "edit" column and then the "update" column of a grid row that has been edited - my other...
1
by: epigram | last post by:
I'm trying to use the ASP.NET validators to check some client-side business rules. I've got two ASP TextBox controls (call them tbxYear1 and tbxYear2) used to enter a range of years. I've got a...
1
by: Gabriel Lozano-Morán | last post by:
When using the tabstrip control combined with a multipage (several pageview) there is a problem when using validators. The problem is that validation also occurs on the validators that are not on...
1
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...
2
by: Mike Surcouf | last post by:
Hi I have some regex validators on my page set to dynamic and like the way they appear after you tab out of a field and also when you try to postback the form. All OK so far When I register...
3
by: sunburst | last post by:
I will include my code below. Is there anyone who can help me sort out this problem please? when i move from my index to the next roll over button links i am unable to keep the background as...
4
by: Madhur | last post by:
Hello All I am learning how to use ASP.NET Validators and would appreciate if someone could provide me with guidance. I have written very simple ASPX page below with a Dropdown list, a...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.