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