Validators with dynamic form controls

J

Jeremy Ames

I am trying to create required field validators for
controls that I create dynamically. I am pulling
information from a database and building controls based
on that information and creating the IDs for the controls
using a simple counter to reference them with the
required field validators. I think I have got them tied
together just fine, but when I click the button, I see no
error messages.

Below is the code I am using, I hope this helps.

private void BuildEmployeeDetail(int
nEmpId)
{
int [] narValues = new int[15]
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
string [] sarInitals = new string
[15];
LoadInitialLists(narValues,
sarInitals);

string sSql = "SELECT T.TaskDesc,
C.TaskId, C.Complete, C.Initials " +
"FROM
TasksComplete C " +
"JOIN
Tasks T ON (C.TaskId = T.TaskId) " +
"WHERE
RemovalId = " + nEmpId;
cnEmployee = new SqlConnection();
cnEmployee.ConnectionString="Data
Source=(local);" +
"Initial Catalog=EmpDB;" +
"User ID=guest;" +
"Password=";
cmdEmployee = new SqlCommand
(sSql, cnEmployee);
cnEmployee.Open();
drEmployee =
cmdEmployee.ExecuteReader();

int nCnt = 1;

while(drEmployee.Read())
{
// create a row to add to
the existing table
TableRow rowTemplate =
new TableRow();
if (nCnt % 2 != 0)

rowTemplate.BackColor =
System.Drawing.Color.White;
else

rowTemplate.BackColor =
System.Drawing.Color.Silver;

// create cells to add to
the previously created row
TableCell cellCol1 = new
TableCell();
TableCell cellCol2 = new
TableCell();
TableCell cellCol3 = new
TableCell();

// create the
dropdownlist first so the required field validator
// has a control to
validate
DropDownList
dlCopyInitials = new DropDownList();
dlCopyInitials.ID
= "dlInitials" + nCnt;

// enter the description
into the first cell
cellCol1.Text =
Convert.ToString(drEmployee.GetString(0));
RequiredFieldValidator
rvInitials = new RequiredFieldValidator();

rvInitials.ControlToValidate = Convert.ToString
(dlCopyInitials.ID);
rvInitials.ErrorMessage
= "Initials cannot be blank!";
rvInitials.InitialValue
= "12";
rowTemplate.Cells.Add
(cellCol1);

// enter a hidden task id
and check box to the second cell
TextBox txtHiddenTask =
new TextBox();
txtHiddenTask.Visible =
false;
txtHiddenTask.ID = "txt"
+ nCnt;
txtHiddenTask.Text =
Convert.ToString(drEmployee.GetInt32(1));

CheckBox chkComplete =
new CheckBox();
chkComplete.ID = "chk" +
nCnt;
if (drEmployee.GetBoolean
(2))

chkComplete.Checked = true;
cellCol2.Controls.Add
(txtHiddenTask);
cellCol2.Controls.Add
(chkComplete);
cellCol2.HorizontalAlign
= System.Web.UI.WebControls.HorizontalAlign.Center;
rowTemplate.Cells.Add
(cellCol2);

// add a check box to the
third cell

int n = 0;
// add items to the
initials drop down list
do
{

dlCopyInitials.Items.Add(sarInitals[n]);

dlCopyInitials.Items[n].Value = Convert.ToString
(narValues[n++]);
} while(narValues[n] !=
0);


dlCopyInitials.SelectedValue = Convert.ToString
(drEmployee.GetInt32(3));
cellCol3.Controls.Add
(dlCopyInitials);
cellCol3.HorizontalAlign
= System.Web.UI.WebControls.HorizontalAlign.Center;
rowTemplate.Cells.Add
(cellCol3);

// add all of the cells
in the row to the table
tblDetail.Rows.Add
(rowTemplate);

txtHiddenTask.Dispose();
chkComplete.Dispose();
dlCopyInitials.Dispose();
rowTemplate.Dispose();
cellCol1.Dispose();
cellCol2.Dispose();
cellCol3.Dispose();
nCnt += 1;
}

drEmployee.Close();
cnEmployee.Close();
}
 
T

Trebek

Although I am not sure if this issue affects you, if you are using VS2003
and are not seeing validation messages, look at the following Q article:
http://support.microsoft.com/?id=822734. If this does not help your
specific situation, maybe something below will be of interest.

Some possible causes of not seeing appropriate validator behavior could be:

1) Persistence -- how are you loading and persisting your dynamic
controls? They should be loaded during Init (not sure from your example --
too hard to read due to line breaks in news viewer :)) and postbacks need to
be handled so as to not replace your initial controls with new ones.

2) Are your dynamic controls assigned a unique id? Does this id match
the 'ControlToValidate' ID even after postbacks? View the source after the
page loads and match the ids.

3) Have you tried stepping thru the page to see if you are receiving
the Page.Validate evt? Are you explicitly calling 'Validate()' to see page
validation state?

hth,

Trebek

Jeremy Ames said:
I am trying to create required field validators for
controls that I create dynamically. I am pulling
information from a database and building controls based
on that information and creating the IDs for the controls
using a simple counter to reference them with the
required field validators. I think I have got them tied
together just fine, but when I click the button, I see no
error messages.

Below is the code I am using, I hope this helps.

private void BuildEmployeeDetail(int
nEmpId)
{
int [] narValues = new int[15]
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
string [] sarInitals = new string
[15];
LoadInitialLists(narValues,
sarInitals);

string sSql = "SELECT T.TaskDesc,
C.TaskId, C.Complete, C.Initials " +
"FROM
TasksComplete C " +
"JOIN
Tasks T ON (C.TaskId = T.TaskId) " +
"WHERE
RemovalId = " + nEmpId;
cnEmployee = new SqlConnection();
cnEmployee.ConnectionString="Data
Source=(local);" +
"Initial Catalog=EmpDB;" +
"User ID=guest;" +
"Password=";
cmdEmployee = new SqlCommand
(sSql, cnEmployee);
cnEmployee.Open();
drEmployee =
cmdEmployee.ExecuteReader();

int nCnt = 1;

while(drEmployee.Read())
{
// create a row to add to
the existing table
TableRow rowTemplate =
new TableRow();
if (nCnt % 2 != 0)

rowTemplate.BackColor =
System.Drawing.Color.White;
else

rowTemplate.BackColor =
System.Drawing.Color.Silver;

// create cells to add to
the previously created row
TableCell cellCol1 = new
TableCell();
TableCell cellCol2 = new
TableCell();
TableCell cellCol3 = new
TableCell();

// create the
dropdownlist first so the required field validator
// has a control to
validate
DropDownList
dlCopyInitials = new DropDownList();
dlCopyInitials.ID
= "dlInitials" + nCnt;

// enter the description
into the first cell
cellCol1.Text =
Convert.ToString(drEmployee.GetString(0));
RequiredFieldValidator
rvInitials = new RequiredFieldValidator();

rvInitials.ControlToValidate = Convert.ToString
(dlCopyInitials.ID);
rvInitials.ErrorMessage
= "Initials cannot be blank!";
rvInitials.InitialValue
= "12";
rowTemplate.Cells.Add
(cellCol1);

// enter a hidden task id
and check box to the second cell
TextBox txtHiddenTask =
new TextBox();
txtHiddenTask.Visible =
false;
txtHiddenTask.ID = "txt"
+ nCnt;
txtHiddenTask.Text =
Convert.ToString(drEmployee.GetInt32(1));

CheckBox chkComplete =
new CheckBox();
chkComplete.ID = "chk" +
nCnt;
if (drEmployee.GetBoolean
(2))

chkComplete.Checked = true;
cellCol2.Controls.Add
(txtHiddenTask);
cellCol2.Controls.Add
(chkComplete);
cellCol2.HorizontalAlign
= System.Web.UI.WebControls.HorizontalAlign.Center;
rowTemplate.Cells.Add
(cellCol2);

// add a check box to the
third cell

int n = 0;
// add items to the
initials drop down list
do
{

dlCopyInitials.Items.Add(sarInitals[n]);

dlCopyInitials.Items[n].Value = Convert.ToString
(narValues[n++]);
} while(narValues[n] !=
0);


dlCopyInitials.SelectedValue = Convert.ToString
(drEmployee.GetInt32(3));
cellCol3.Controls.Add
(dlCopyInitials);
cellCol3.HorizontalAlign
= System.Web.UI.WebControls.HorizontalAlign.Center;
rowTemplate.Cells.Add
(cellCol3);

// add all of the cells
in the row to the table
tblDetail.Rows.Add
(rowTemplate);

txtHiddenTask.Dispose();
chkComplete.Dispose();
dlCopyInitials.Dispose();
rowTemplate.Dispose();
cellCol1.Dispose();
cellCol2.Dispose();
cellCol3.Dispose();
nCnt += 1;
}

drEmployee.Close();
cnEmployee.Close();
}
 
J

Jeremy Ames

Actually, I just found the problem. I had forgotten to
add the validator control to the TableCell. However, I am
calling the BuildEmployeeDetail function during page
load. Is this not the right time to do it? I am testing
for postbacks in the Page_Load event and I only call the
fore-mentioned function if it is not a postback. Please
let me know if that is bad practice because I am still
learning C#.

-----Original Message-----
Although I am not sure if this issue affects you, if you are using VS2003
and are not seeing validation messages, look at the following Q article:
http://support.microsoft.com/?id=822734. If this does not help your
specific situation, maybe something below will be of interest.

Some possible causes of not seeing appropriate validator behavior could be:

1) Persistence -- how are you loading and persisting your dynamic
controls? They should be loaded during Init (not sure from your example --
too hard to read due to line breaks in news viewer :)) and postbacks need to
be handled so as to not replace your initial controls with new ones.

2) Are your dynamic controls assigned a unique id? Does this id match
the 'ControlToValidate' ID even after postbacks? View the source after the
page loads and match the ids.

3) Have you tried stepping thru the page to see if you are receiving
the Page.Validate evt? Are you explicitly
calling 'Validate()' to see page
validation state?

hth,

Trebek

I am trying to create required field validators for
controls that I create dynamically. I am pulling
information from a database and building controls based
on that information and creating the IDs for the controls
using a simple counter to reference them with the
required field validators. I think I have got them tied
together just fine, but when I click the button, I see no
error messages.

Below is the code I am using, I hope this helps.

private void BuildEmployeeDetail(int
nEmpId)
{
int [] narValues = new int[15]
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
string [] sarInitals = new string
[15];
LoadInitialLists(narValues,
sarInitals);

string sSql = "SELECT T.TaskDesc,
C.TaskId, C.Complete, C.Initials " +
"FROM
TasksComplete C " +
"JOIN
Tasks T ON (C.TaskId = T.TaskId) " +
"WHERE
RemovalId = " + nEmpId;
cnEmployee = new SqlConnection();
cnEmployee.ConnectionString="Data
Source=(local);" +
"Initial Catalog=EmpDB;" +
"User ID=guest;" +
"Password=";
cmdEmployee = new SqlCommand
(sSql, cnEmployee);
cnEmployee.Open();
drEmployee =
cmdEmployee.ExecuteReader();

int nCnt = 1;

while(drEmployee.Read())
{
// create a row to add to
the existing table
TableRow rowTemplate =
new TableRow();
if (nCnt % 2 != 0)

rowTemplate.BackColor =
System.Drawing.Color.White;
else

rowTemplate.BackColor =
System.Drawing.Color.Silver;

// create cells to add to
the previously created row
TableCell cellCol1 = new
TableCell();
TableCell cellCol2 = new
TableCell();
TableCell cellCol3 = new
TableCell();

// create the
dropdownlist first so the required field validator
// has a control to
validate
DropDownList
dlCopyInitials = new DropDownList();
dlCopyInitials.ID
= "dlInitials" + nCnt;

// enter the description
into the first cell
cellCol1.Text =
Convert.ToString(drEmployee.GetString(0));
RequiredFieldValidator
rvInitials = new RequiredFieldValidator();

rvInitials.ControlToValidate = Convert.ToString
(dlCopyInitials.ID);
rvInitials.ErrorMessage
= "Initials cannot be blank!";
rvInitials.InitialValue
= "12";
rowTemplate.Cells.Add
(cellCol1);

// enter a hidden task id
and check box to the second cell
TextBox txtHiddenTask =
new TextBox();
txtHiddenTask.Visible =
false;
txtHiddenTask.ID = "txt"
+ nCnt;
txtHiddenTask.Text =
Convert.ToString(drEmployee.GetInt32(1));

CheckBox chkComplete =
new CheckBox();
chkComplete.ID = "chk" +
nCnt;
if (drEmployee.GetBoolean
(2))

chkComplete.Checked = true;
cellCol2.Controls.Add
(txtHiddenTask);
cellCol2.Controls.Add
(chkComplete);
cellCol2.HorizontalAlign
= System.Web.UI.WebControls.HorizontalAlign.Center;
rowTemplate.Cells.Add
(cellCol2);

// add a check box to the
third cell

int n = 0;
// add items to the
initials drop down list
do
{

dlCopyInitials.Items.Add(sarInitals[n]);

dlCopyInitials.Items[n].Value = Convert.ToString
(narValues[n++]);
} while(narValues[n] !=
0);


dlCopyInitials.SelectedValue = Convert.ToString
(drEmployee.GetInt32(3));
cellCol3.Controls.Add
(dlCopyInitials);
cellCol3.HorizontalAlign
= System.Web.UI.WebControls.HorizontalAlign.Center;
rowTemplate.Cells.Add
(cellCol3);

// add all of the cells
in the row to the table
tblDetail.Rows.Add
(rowTemplate);

txtHiddenTask.Dispose();
chkComplete.Dispose();
dlCopyInitials.Dispose();
rowTemplate.Dispose();
cellCol1.Dispose();
cellCol2.Dispose();
cellCol3.Dispose();
nCnt += 1;
}

drEmployee.Close();
cnEmployee.Close();
}


.
 

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

Similar Threads


Top