1

我需要根据用户响应生成动态文本框(最多 5-10 个)。那么,就性能和速度而言,最好的方法是什么。

4

3 回答 3

0

试试这种方式

for (int j = 0; j < 10; j++)
            {                  

              //Create Dynamic textboxes with required field validator .

                TextBox tbChildFirstName = new TextBox();
                Label lblChildFirstName = new Label();
                lblChildFirstName.ID = "lblChildFirstName" + j;
                lblChildFirstName.Text = "Children FirstName";
                lblChildFirstName.Width = 200;
                lblChildFirstName.SkinID = "Outlabel";
                tbChildFirstName.ID = "txtChildFirstName" + j;
                tbChildFirstName.Text = "Hi hello";
                pnlChildDetail.Controls.Add(lblChildFirstName);
                pnlChildDetail.Controls.Add(tbChildFirstName);
                RequiredFieldValidator rqf = new RequiredFieldValidator();
                rqf.ID = "rqf" + i;
                pnlChildDetail.Controls.Add(rqf);
                rqf.ControlToValidate = "txtChildFirstName" + j;
                rqf.ErrorMessage = "Children FirstName is required";
                rqf.Display = ValidatorDisplay.Dynamic;
                rqf.ValidationGroup = "EligibilityCheck";
                rqf.SetFocusOnError = true;
                rqf.Text = "*";
                tbChildFirstName.Attributes.Add("runat", "server");
                pnlChildDetail.Controls.Add(GetLiteral("<br/>"));
                pnlChildDetail.Controls.Add(GetLiteral("<br/>"));

            }

public Literal GetLiteral(string text)
    {
        Literal rv = default(Literal);
        rv = new Literal();
        rv.Text = text;
        return rv;
    }

象素

<asp:Panel ID="pnlChildDetail" Style="text-align: left; padding-left: 10px" Width="100%"
        runat="server">
    </asp:Panel>

有关创建动态文本框表单数据库数据计数以及如何获取动态文本框值的更多详细信息?:请参阅我的博客

于 2013-10-09T05:15:03.330 回答
0

这是一个非常有用的链接,并解释了为什么
从这里链接

  • 始终在 Page_PreInit() 中添加动态控件
  • 动态加载时始终分配 ID

例如

protected void Page_PreInit(object sender, EventArgs e) 
{ 
        Control c= LoadControl("./WebUserControl2.ascx"); 
        i=i+1; 
        c.ID= i.ToString(); 
        PlaceHolder1.Controls.Add(c); 
} 

或者

protected void Page_PreInit(object sender, EventArgs e) 
{ 
         LinkButton button1 = new LinkButton(); 
         button1.ID = "button1" 
         button1.Text = "button1" 
         PlaceHolder1.Controls.Add(button1); 
}
于 2013-10-09T05:07:27.780 回答
0
int n=5;
for (int i=0;i<n;i++)
{
TextBox MyTextBox=new TextBox();
//Assigning the textbox ID name 
MyTextBox.ID = "tb" +""+ ViewState["num"] + i;
MyTextBox.Width = 540;
MyTextBox.Height = 60;
MyTextBox.TextMode = TextBoxMode.MultiLine;
this.Controls.Add(MyTextBox);
}

对于 MVC,此链接可能对您有所帮助... http://www.codeproject.com/Articles/434886/Dynamically-adding-controls-on-a-hierarchical-stru

于 2013-10-09T05:08:43.887 回答