Make RequiredFieldValidator in aspx.cs (code behind) asp.net

流过昼夜 提交于 2019-12-24 14:26:59

问题


I have a TextBox where i add a number and at a buttonclick i create new textboxes depending on what number i put in my first TextBox.

For example i put 5 in the TextBox and press button i get 5 new textboxes on my page.

Now im trying to make a RequiredFieldValidator on all of this textboxes thats created but i can't get it to work. I have another button that makes an insert to my db and this values from all the new textboxes are required.

Can someone take a look at my code to see if im on the right track and maybe help me to modify the code so it would work.

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 />"));
        }
    }

Thanks for your help, the RequiredFieldValidator works now but at insert in my db the values from the txtbox and txtbox1 are empty. I make something wrong with the insert now.

What do i have to Change there now?

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

回答1:


You need to add the RequiredFieldValidator control to the page. ie: you need to add this somewhere:

pnlTextBoxes.Controls.Add(rfv) 



回答2:


While creating TextBoxes dynamically, you should assign an ID to each textBox.

TextBox txtbox = new TextBox();
txtbox.ID="txt"+i; // This ID can be assigned values as per your own Logic.

Now same needs to be done with the RequiredFieldValidator:

RequiredFieldValidator rfv = new RequiredFieldValidator();
 rfv.ID = "rfv" + i; // again assign ID values as per your own logic

Once you have done this, You need to assign the ID of TextBox to the ControlToValidate property of RequiredFieldValidator. . The ControlToValidate property actaully takes the ID of the Control to validate and NOT something like the Text property as currently done by you.

rfv.ControlToValidate = txtbox.ID;

Now add the above generated 2 controls to your PlaceHolders that you have. The code shown by you currently doesn't seems to be adding these controls any where.

Finally, if you are trying to insert the values put inside this textboxes, So possibly you also need to maintain the ViewState of all such dynamically cretaed textboxes. One possible solution is to regenerate all such textBoxes on page Load. This is going to require some rethinking on organizing your code. Continue here if you want to make sure that these textboxes maintain their state after postback: http://www.codeproject.com/Articles/3684/Retaining-State-for-Dynamically-Created-Controls-i



来源:https://stackoverflow.com/questions/18275125/make-requiredfieldvalidator-in-aspx-cs-code-behind-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!