我有一个文本框,我在其中添加一个数字,然后单击按钮创建新文本框,具体取决于我在第一个文本框中输入的数字。
例如,我在 TextBox 中放入 5 并按下按钮,我在页面上得到 5 个新的文本框。
现在我试图在所有这些创建的文本框上创建一个RequiredFieldValidator,但我无法让它工作。我有另一个按钮可以插入到我的数据库中,并且所有新文本框中的这些值都是必需的。
有人可以看看我的代码,看看我是否走在正确的轨道上,也许可以帮助我修改代码以使其正常工作。
protected void btnGenerateControl_Click(object sender, EventArgs e)
{
int Count = Convert.ToInt32(Qty.Text);
for (int i = 1; i <= Count; i++)
{
Label lbl = new Label();
TextBox txtbox = new TextBox();
RequiredFieldValidator rfv = new RequiredFieldValidator();
string ValidationGroup = "Group2";
string ErrorMessage = "Bitte Flotten ID eingeben";
lbl.Text = " Flotten ID " + (i).ToString() + " ";
txtbox.EnableViewState = true;
rfv.ControlToValidate = txtbox.Text;
rfv.ErrorMessage = ErrorMessage;
rfv.ValidationGroup = ValidationGroup;
rfv.ForeColor = System.Drawing.Color.Red;
Label lbl1 = new Label();
TextBox txtbox1 = new TextBox();
txtbox1.EnableViewState = true;
pnlTextBoxes.Controls.Add(lbl);
pnlTextBoxes.Controls.Add(new LiteralControl("<input id='txt' name='FlottenID" + i + "'type='text' />"));
pnlTextBoxes.Controls.Add(lbl1);
lbl1.Text = " Bemerkungen: ";
pnlTextBoxes.Controls.Add(new LiteralControl("<input id='txt' name='Info" + i + "'type='text' />"));
pnlTextBoxes.Controls.Add(new LiteralControl("<br /><br />"));
}
}
感谢您的帮助,RequiredFieldValidator 现在可以工作,但是在我的数据库中插入时,来自 txtbox 和 txtbox1 的值是空的。我现在插入有问题。
我现在要在那里改变什么?
protected void btnGenerateControl_Click(object sender, EventArgs e)
{
int Count = Convert.ToInt32(Qty.Text);
for (int i = 1; i <= Count; i++)
{
string ValidationGroup = "Group2";
string ErrorMessage = "Bitte Flotten ID eingeben";
Label lbl = new Label();
lbl.Text = " Flotten ID " + (i).ToString() + " ";
TextBox txtbox = new TextBox();
txtbox.EnableViewState = true;
txtbox.ID = "txt" + i;
TextBox txtbox1 = new TextBox();
txtbox.EnableViewState = true;
txtbox.ID = "txt" + i;
RequiredFieldValidator rfv = new RequiredFieldValidator();
rfv.ControlToValidate = txtbox.ID;
rfv.ErrorMessage = ErrorMessage;
rfv.ID = "rfv" + i;
rfv.ValidationGroup = ValidationGroup;
rfv.ForeColor = System.Drawing.Color.Red;
Label lbl1 = new Label();
lbl1.Text = " Bemerkungen: ";
pnlTextBoxes.Controls.Add(lbl);
//pnlTextBoxes.Controls.Add(new LiteralControl("<input id='txt' name='FlottenID" + i + "'type='text' />"));
pnlTextBoxes.Controls.Add(txtbox);
pnlTextBoxes.Controls.Add(rfv);
pnlTextBoxes.Controls.Add(lbl1);
pnlTextBoxes.Controls.Add(txtbox1);
//pnlTextBoxes.Controls.Add(new LiteralControl("<input id='txt' name='Info" + i + "'type='text' />"));
pnlTextBoxes.Controls.Add(new LiteralControl("<br /><br />"));
}
}
protected void btnAddOrder_Click(object sender, EventArgs e)
{
int Count = Convert.ToInt32(Qty.Text);
for (int i = 1; i <= Count; i++)
{
String query = "insert into Orders (CustID, OrderDate, Time, ProductID, ProjectID, Status, FlottenID, Info)values('" + CustID.Text + "','" + OrderDate.Text + "','" + Time.Text + "','" + ProductID.Value + "','" + ProjectID.Value + "','" + Status.Value + "','" + Request.Form["FlottenID" + i.ToString()] + "','" + Request.Form["Info" + i.ToString()] + "')";
String query1 = "commit;";
DataLayer.DataConnector dat = new DataLayer.DataConnector("Provider=SQLOLEDB; data source=rzwsrv010;database=event;user ID=event;password=event; Persist Security Info=False");
dat.DataInsert(query);
dat.DataInsert(query1);
}
Response.Redirect("NewOrder.aspx");
}