请在的RequiredFieldValidator aspx.cs(code后面)asp.net [英] Make RequiredFieldValidator in aspx.cs (code behind) asp.net

查看:87
本文介绍了请在的RequiredFieldValidator aspx.cs(code后面)asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框,其中我添加了一个数字,并在buttonclick我创建取决于我把我的第一个TextBox什么号码新的文本框。

例如我把5中的文本框和preSS按钮,我得到我的网页上5个新的文本框。

现在即时通讯试图让所有的创造了这个文本框多数民众赞成在一个RequiredFieldValidator,但我不能得到它的工作。我还有一个按钮,使插入到我的分贝,这从价值观要求所有的新文本框。

有人可以看看我的code,看是否IM在正确的轨道上,也许帮我修改code所以它会工作。

 保护无效btnGenerateControl_Click(对象发件人,EventArgs的发送)
    {
        诠释计数= Convert.ToInt32(Qty.Text);
        的for(int i = 1; I< =计数;我++)
        {
            标签LBL =新的Label();
            文本框txtbox =新的TextBox();
            RFV的RequiredFieldValidator =新的RequiredFieldValidator();
            字符串的ValidationGroup =组2;
            字符串的ErrorMessage =BITTE Flotten ID eingeben            lbl.Text =Flotten ID+(ⅰ)的ToString()+;
            txtbox.EnableViewState = TRUE;
            rfv.ControlToValidate = txtbox.Text;            rfv.ErrorMessage =的ErrorMessage;
            rfv.ValidationGroup =的ValidationGroup;
            rfv.ForeColor = System.Drawing.Color.Red;
            标签LBL1 =新的Label();
            文本框txtbox1 =新的TextBox();            txtbox1.EnableViewState = TRUE;
            pnlTextBoxes.Controls.Add(LBL);
            pnlTextBoxes.Controls.Add(新LiteralControl(<输入ID ='TXT'名称='FlottenID+ I +类型='文字'/>中));
            pnlTextBoxes.Controls.Add(LBL1);
            lbl1.Text =Bemerkungen:
            pnlTextBoxes.Controls.Add(新LiteralControl(<输入ID ='TXT'名称='信息+ I +类型='文字'/>中));
            pnlTextBoxes.Controls.Add(新LiteralControl(< BR />< BR />中));
        }
    }


感谢您的帮助,该工程的RequiredFieldValidator,但现在在插入在我的数据库从txtbox和txtbox1值是空的。我做什么毛病现在插入。

我有什么现在改变吗?

 保护无效btnGenerateControl_Click(对象发件人,EventArgs的发送)
    {
        诠释计数= Convert.ToInt32(Qty.Text);
        的for(int i = 1; I< =计数;我++)
        {            字符串的ValidationGroup =组2;
            字符串的ErrorMessage =BITTE Flotten ID eingeben            标签LBL =新的Label();
            lbl.Text =Flotten ID+(ⅰ)的ToString()+;            文本框txtbox =新的TextBox();
            txtbox.EnableViewState = TRUE;
            txtbox.ID =TXT+我;            文本框txtbox1 =新的TextBox();
            txtbox.EnableViewState = TRUE;
            txtbox.ID =TXT+我;
            RFV的RequiredFieldValidator =新的RequiredFieldValidator();
            rfv.ControlToValidate = txtbox.ID;
            rfv.ErrorMessage =的ErrorMessage;
            rfv.ID =RFV+我;
            rfv.ValidationGroup =的ValidationGroup;
            rfv.ForeColor = System.Drawing.Color.Red;            标签LBL1 =新的Label();
            lbl1.Text =Bemerkungen:
            pnlTextBoxes.Controls.Add(LBL);
            //pnlTextBoxes.Controls.Add(new LiteralControl(<输入ID ='TXT'名称='FlottenID+ I +类型='文字'/>中));
            pnlTextBoxes.Controls.Add(txtbox);
            pnlTextBoxes.Controls.Add(RFV);
            pnlTextBoxes.Controls.Add(LBL1);
            pnlTextBoxes.Controls.Add(txtbox1);            //pnlTextBoxes.Controls.Add(new LiteralControl(<输入ID ='TXT'名称='信息+ I +类型='文字'/>中));
            pnlTextBoxes.Controls.Add(新LiteralControl(< BR />< BR />中));
        }
    }    保护无效btnAddOrder_Click(对象发件人,EventArgs的发送)
    {
        诠释计数= Convert.ToInt32(Qty.Text);
        的for(int i = 1; I< =计数;我++)
        {            查询字符串=插入订单(客户ID,订购日期,时间,产品ID,专案编号,状态,FlottenID,资讯)VALUES('+ CustID.Text +,+ OrderDate.Text +,+时间。文本+,+ ProductID.Value +,+ ProjectID.Value +,+ Status.Value +,+的Request.Form [FlottenID+ i.ToString ()] +,+的Request.Form [信息+ i.ToString()+');
            字符串QUERY1 =承诺;
            DataLayer.DataConnector DAT =新DataLayer.DataConnector(提供程序= SQLOLEDB;数据源= rzwsrv010;数据库=事件;用户ID =事件;密码=事件;坚持安全信息=假);
            dat.DataInsert(查询);
            dat.DataInsert(QUERY1);
        }        的Response.Redirect(NewOrder.aspx);
    }


解决方案

虽然动态创建文本框,你应该分配 ID 每一个文本框。

 文本框txtbox =新的TextBox();
txtbox.ID =TXT+我; //这个ID可以赋值按照自己的逻辑。

要与做的RequiredFieldValidator

现在同样的需求:

 的RequiredFieldValidator RFV =新的RequiredFieldValidator();
 rfv.ID =RFV+我; //再次分配ID值按自己的逻辑

一旦你做到了这一点,你需要 文本框 ID分配给的ControlToValidate 的RequiredFieldValidator的属性。。在的ControlToValidate 属性actaully取 ID 控制的验证并不会像目前被你所做的Text属性

  rfv.ControlToValidate = txtbox.ID;

现在添加上面生成的2控制你的占位符,你有。你所显示的code目前似乎并不被任何添加这些控件的位置。

最后,如果你想插入把里面的值这个文本框,所以,可能你还需要保持所有这些动态cretaed文本框的ViewState中。一种可能的解决办法是重新生成页面加载所有这些文本框。这将需要对组织的code重新思考。如果你想确保这些文本框回发后保持自己的状态,继续在这里:的http://www.$c$cproject.com/Articles/3684/Retaining-State-for-Dynamically-Created-Controls-i

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

解决方案

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

这篇关于请在的RequiredFieldValidator aspx.cs(code后面)asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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