如何在动态生成的文本框中添加必需的字段验证器 [英] How to add a required field validator in my dynamically generated textboxes

查看:100
本文介绍了如何在动态生成的文本框中添加必需的字段验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨每一个,

如何在动态生成的文本框中添加必填字段验证器?请帮忙。

我的代码是

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

           }
           }
       }

推荐答案

只看到下面的演示代码,用于举例说明和编辑代码。这是在文本框中动态添加 requiredfield validation 的示例。

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





当你动态添加所需的Fieldvalidator时。它会抛出错误: WebForms UnobtrusiveValidationMode需要一个用于jQuery的ScriptResourceMapping ;



我们将以不同的方式解决这个问题:

第一种情况:

在web.config文件中添加以下代码。



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>





或第二个案例

在Application_start事件中的Global.asax文件中添加此代码



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


这篇关于如何在动态生成的文本框中添加必需的字段验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆