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)
{
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