简体   繁体   English

如何自动添加文本框

[英]How to automatically add textbox

I have these 8 textboxes, but when I start to think that it were very messy on my page. 我有这8个文本框,但是当我开始认为它在我的页面上非常混乱时。 So, I am thinking that how to implement a button which can add textbox when user click on it. 所以,我在想如何实现一个按钮,当用户点击它时可以添加文本框。 Does anyone here have an idea or solution to assist me? 这里有人有想法或解决方案来帮助我吗? I would appreciate that. 我很感激。 Thank you. 谢谢。

Probably the best way to do this is to place both your text boxes in a user control. 可能最好的方法是将文本框放在用户控件中。 This user control can then encapsulate all of the logic belonging to both the text boxes; 然后,该用户控件可以封装属于两个文本框的所有逻辑; it can calculate the difference between the two values, for example, encapsulate all of the validation for both etc etc. You only need to write this bit once! 它可以计算两个值之间的差异,例如,封装两个等的所有验证等。你只需要写一次这个位!

Add a placeholder to your page; 在页面中添加占位符; give it an id of "DynamicControlHolder" or similar. 给它一个“DynamicControlHolder”或类似的id。 You'll also want a hidden field - DynamicControlCount - you can use this to store the number of dynamic control's you've added. 您还需要一个隐藏字段 - DynamicControlCount - 您可以使用它来存储您添加的动态控件的数量。

The two most important concepts are: 两个最重要的概念是:

  • Add the dynamic controls at the right time 合适的时间添加动态控件
  • Try and maintain the id's between postback so ASP.Net automatically populates the values from the ViewState for you. 尝试并维护回发之间的ID,以便ASP.Net自动为您填充ViewState中的值。 Otherwise, you'll have to manage this yourself. 否则,你必须自己管理。

So, when the page loads for the first time, you can do something like the following in Page_Init, for example: 因此,当页面第一次加载时,您可以在Page_Init中执行以下操作,例如:

{
  MyUserControl control = Page.LoadControl("~/path_to_my_usercontrol");
  control.ID = "MyUserControl1";
  DynamicControlCount.Value = "1";

  DynamicControlHolder.Controls.Add(control);
}

If you then have a button on your page, "Add Control", then, in the click handler: 如果您在页面上有一个按钮,则“添加控件”,然后在单击处理程序中:

{
  int controlCount = Convert.ToInt32(DynamicControlCount.Value);
  controlCount++;

  //This section will add as many controls as i.
  for(i = 1; i <= controlCount; i++)
  {
      MyUserControl control = Page.LoadControl("~/path_to_my_usercontrol");
      control.ID = String.Format("MyUserControl{0}", i);          
      DynamicControlHolder.Controls.Add(control);           
  }

  DynamicControlCount.Value = Convert.ToString(controlCount); //Note this is problematic
}

The easiest way to control the second part is to use an UpdatePanel . 控制第二部分的最简单方法是使用UpdatePanel The main reason for this is that when you update the count of controls you have, you are doing this late in the lifecycle of the page, and, in a full postback, the value may not increment in the way you expect on postback. 这样做的主要原因是,当您更新控件的数量时,您在页面的生命周期中执行此操作,并且在完整的回发中,该值可能不会以您对回发的预期方式增加。 It also looks better for the user if the whole page isn't posted back just to revise a small section of it. 如果整个页面没有回发只是为了修改它的一小部分,它对用户来说看起来也更好。

These are just a few ideas to get you started. 这些只是让您入门的一些想法。 A good reference for this kind of thing is here: 这方面的一个很好的参考是:

Truly Understanding Dynamic Controls 真正理解动态控制

You should do some reading around this because the combination of viewstate, page lifecycle, client side rendering and other factors make this one of the trickiest, but most interesting techniques in ASP.Net 你应该做一些阅读,因为viewstate,页面生命周期,客户端渲染和其他因素的组合使这成为ASP.Net中最棘手但最有趣的技术之一

If you consider using jquery, things could be very simple: 如果你考虑使用jquery,事情可能很简单:

<div id="text_box_container"> </div>

<input type="button" value="text box inserter" id="text_box_inserter" />

now the javascript/jquery part 现在是javascript / jquery部分

$('#text_box_inserter').click(function(){
    $('#text_box_container').append('<input type="text" />');
})

This will create a Textbox and add it to the div with id="divFirstName" . 这将创建一个文本框并将其添加到id="divFirstName"div The div should have runat="server" div应该有runat="server"

TextBox tbFirstName = new TextBox();
tbFirstName.ID = "tbFirstName";
tbFirstName.Attributes.Add("Name", "tbFirstName");

divFirstName.Controls.Add(tbFirstName);

Then just place the code in your click event. 然后将代码放在点击事件中。 (I know my naming sucks, it's purely to give a better understanding). (我知道我的命名很糟糕,纯粹是为了更好地理解)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM