Click here to Skip to main content
15,922,894 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Every one,
How to add a required field validator in my dynamically generated textboxes? Please help.
My code is
protected void Button1_Click(object sender, EventArgs e)
   {
       if (txtQuantity.Text != "")
       {
           for (int i = 0; i <= Convert.ToInt16(txtQuantity.Text) / 3; i++)
           {
               Label la = new Label();
               la.Text = ddlProductName.SelectedItem.Value + (i+1).ToString();
               la.ID = "abc1" + i;

               PlaceHolder1.Controls.Add(la);
               //la = FindControl(String.Format(i)) as Label;

               TextBox txtbox = new TextBox();
               // txtbox.Text = "Textbox - " + i.ToString();

               PlaceHolder1.Controls.Add(new LiteralControl("<input id='txt' name='Textbox" + i + "'type='text'  />"));
               PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
               //RequiredFieldValidator val = new RequiredFieldValidator();
               //TextBox txt = PlaceHolder1.FindControl("<input id='txt' name='Textbox" + i + "'type='text'  />") as TextBox;
               //val.ControlToValidate = txt.Text;
               //val.ErrorMessage = "Enter serial No";
               //PlaceHolder1.Controls.Add(val);
           }
           for (int j = (Convert.ToInt16(txtQuantity.Text) / 3)+1 ; j <= Convert.ToInt16(txtQuantity.Text)*2/3; j++)
           {
               Label la1 = new Label();
               la1.Text = ddlProductName.SelectedItem.Value + (j+1).ToString();
               la1.ID = "abc2" + j;

               PlaceHolder2.Controls.Add(la1);
               //la = FindControl(String.Format(i)) as Label;
               TextBox txtbox1 = new TextBox();
               // txtbox.Text = "Textbox - " + i.ToString();
                 PlaceHolder2.Controls.Add(new LiteralControl("<input id='txt' name='Textbox" + j + "'type='text'  />"));
               PlaceHolder2.Controls.Add(new LiteralControl("<br />"));
               //RequiredFieldValidator val = new RequiredFieldValidator();
               //val.ControlToValidate = "<input id='txt' name='Textbox" + j + "'type='text'  />";
               //val.ErrorMessage = "Enter serial No";
               //PlaceHolder2.Controls.Add(val);
           }
           for (int k = (Convert.ToInt16(txtQuantity.Text)*2/3)+1; k < Convert.ToInt16(txtQuantity.Text) ; k++)
           {
               Label la2 = new Label();
               la2.Text = ddlProductName.SelectedItem.Value + (k + 1).ToString();
               la2.ID = "abc3" + k;

               PlaceHolder3.Controls.Add(la2);
               //la = FindControl(String.Format(i)) as Label;
               TextBox txtbox1 = new TextBox();
               // txtbox.Text = "Textbox - " + i.ToString();
               PlaceHolder3.Controls.Add(new LiteralControl("<input id='txt' name='Textbox" + k + "'type='text'  />"));
               PlaceHolder3.Controls.Add(new LiteralControl("<br />"));

           }
           }
       }
Posted
Updated 9-Jun-14 2:26am
v2

1 solution

Just see the following demo code which is done to give one example and edit your code. This is a example for to add dynamically requiredfield validation in textbox.
protected void Page_Load(object sender, EventArgs e)
   {
       for (int i = 0; i < 2; i++)
       {
           TextBox txt = new TextBox();
           txt.ID = "txt" + i;
           Panel1.Controls.Add(txt);
           RequiredFieldValidator rfv = new RequiredFieldValidator();
           rfv.ID = "rfv" + i;
           rfv.ControlToValidate = "txt" + i;
           rfv.ErrorMessage = "should not blank";
           Panel1.Controls.Add(rfv);
       }
   }


When you will add required Fieldvalidator dynamically. it will throw error :" WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for jQuery ";

We will resolve this on different way:
First case:
Add below code inside web.config file.
<configuration>
 <appsettings>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
  </appsettings> 
</configuration>


OR Second Case
Add this code in Global.asax file inside Application_start event
void Application_Start(object sender, EventArgs e)
   {
       // Code that runs on application startup
       ScriptManager.ScriptResourceMapping.AddDefinition("jquery", new ScriptResourceDefinition
       {
           Path = "~/scripts/jquery-1.4.1.min.js",
           DebugPath = "~/scripts/jquery-1.4.1.js",
           CdnPath = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1.min.js",
           CdnDebugPath = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1.js"
       });
   }


Use any one of First case or second case it will work.
I hope this will help you very much
 
Share this answer
 
v2
Comments
[no name] 9-Jun-14 9:19am    
One more thing required field validator will show error message at the time submit. For test purpose you can add one submit button to testing page

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900