What is the best way to generate dynamic textboxes in ASP.Net web forms?

你。 提交于 2021-01-29 04:41:41

问题


I need to generate dynamic textboxes (upto 5-10 ) according to user response. So, what will be the best way to do it as regard performance,speed is concerned.


回答1:


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

for MVC this link might help u... http://www.codeproject.com/Articles/434886/Dynamically-adding-controls-on-a-hierarchical-stru




回答2:


TRy this way

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

Aspx

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

More details for create dynamic textbox form database data count and how to get dynamic textbox value ?: see my blog




回答3:


Here is a very helpful link and explained why
link from here

  • Always add the dynamic control in Page_PreInit()
  • Always assign ID when loading dynamically

For Example

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

OR

protected void Page_PreInit(object sender, EventArgs e) 
{ 
         LinkButton button1 = new LinkButton(); 
         button1.ID = "button1" 
         button1.Text = "button1" 
         PlaceHolder1.Controls.Add(button1); 
}


来源:https://stackoverflow.com/questions/19263533/what-is-the-best-way-to-generate-dynamic-textboxes-in-asp-net-web-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!