如何自动添加文本框 [英] How to automatically add textbox

查看:163
本文介绍了如何自动添加文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这8文本框,但是当我开始认为它是我的网页上很凌乱。所以,我在想,如何实现一个按钮,可以添加文本框上时,用户点击。是否有人在这里有一个想法或解决方案,以帮助我吗?我想AP preciate这一点。谢谢你。

I have these 8 textboxes, but when I start to think that it were very messy on my page. 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!

占位符添加到您的网页;给它DynamicControlHolder或类似的标识。您还需要一个隐藏字段 - DynamicControlCount - 你可以用它来存储你已经添加了动态控制的数

Add a placeholder to your page; give it an id of "DynamicControlHolder" or similar. You'll also want a hidden field - DynamicControlCount - you can use this to store the number of dynamic control's you've added.

这两个最重要的概念是:

The two most important concepts are:

所以,当页面加载的第一次,你可以不喜欢在Page_Init下面的,例如:

So, when the page loads for the first time, you can do something like the following in Page_Init, for example:

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

要控制的第二部分最简单的方法是使用的UpdatePanel 。造成这种情况的主要原因是,当你更新你的控制数,你正在做的这么晚在页面的生命周期,并且,在一个完整的回传,值可能不会对你期望在回传的方式递增。它还查找用户更好,如果整个页面是不是调回只是修改其中的一小部分。

The easiest way to control the second part is to use an 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:

<一个href=\"http://weblogs.asp.net/infinitiesloop/archive/2006/08/25/TRULY-Understanding-Dynamic-Controls-_2800_Part-1_2900_.aspx\"相对=nofollow>真正了解动态控制

您应该做一些解决这个读数,因为视图状态,页面生命周期,客户端渲染和其他因素的结合使最棘手的这一个,但在ASP.Net中最有趣的技术。

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

这篇关于如何自动添加文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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