我有一个TextBox,在其中添加一个数字,并在buttonclick上创建新的文本框,具体取决于我在第一个TextBox中输入的数字。
例如,我在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");
}
最佳答案
您需要将RequiredFieldValidator控件添加到页面。即:您需要将此添加到某处:
pnlTextBoxes.Controls.Add(rfv)
关于c# - 在aspx.cs(在后面的代码)asp.net中创建RequiredFieldValidator,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18275125/