对于c#我是新手,所以请耐心等待:)。
我在asp.net中创建了一个表单,我正在尝试使用RequiredFieldValidator验证它。 我的问题是输入字段是用for循环生成的。
我的问题是如何验证这些输入字段,是否有某种方法可以像在java中那样“传递对象”?
编辑: 生成字段的表:
<asp:Table id="newPatienttbl"
GridLines="Both"
HorizontalAlign="Center"
Font-Size="8pt"
CellPadding="5"
CellSpacing="0"
Runat="server"/>
我如何生成输入字段。
TableRow row = null;
TableCell cell = null;
TextBox tb = null;
foreach (DataColumn column in patientData.Columns)
{
row = new TableRow();
cell = new TableCell();
if (column.ColumnName.Equals("ZVID"))
cell.Text = "Zorgverzekeraar";
else
cell.Text = column.ColumnName;
row.Cells.Add(cell);
cell = new TableCell();
if (!column.ColumnName.Equals("ZVID"))
{
tb = new TextBox();
cell.Controls.Add(tb);
}
else
{
DropDownList dd = new DropDownList();
dd.ID = column.ColumnName;
dd.DataSource = zorgData;
dd.DataValueField = "ZVID";
dd.DataTextField = "Naam";
dd.DataBind();
cell.Controls.Add(dd);
}
cell.ID = column.ColumnName+"Cell";
row.Cells.Add(cell);
newPatienttbl.Rows.Add(row);
}
答案 0 :(得分:0)
您可以使用以下内容更改代码:
string addedControlId;
if (!column.ColumnName.Equals("ZVID"))
{
tb = new TextBox();
tb.Id= column.ColumnName;
cell.Controls.Add(tb);
addedControlId = tb.Id;
}
else
{
DropDownList dd = new DropDownList();
dd.ID = column.ColumnName;
dd.DataSource = zorgData;
dd.DataValueField = "ZVID";
dd.DataTextField = "Naam";
dd.DataBind();
cell.Controls.Add(dd);
addedControlId = dd.Id;
}
RequiredFieldValidator val = new RequiredFieldValidator{
ControlToValidate = addedControlId,
ErrorMessage = "Shame on you, you did not fill this value"
}
cell.Controls.Add(val);