ASP.NET:在客户端添加控件

pis*_*hio 6 javascript asp.net

如果我有一个带有表单的页面(想象一个简单的只有TextBoxes和一个提交按钮)并且我想允许用户通过javascript动态地向表单添加更多TextBox,那么处理请求服务器端的最佳方法是什么?

示例:我的页面呈现如下:

<input type = "text" id = "control1" name = "control1" />
<input type = "text" id = "control2" name = "control2" />
<input type = "text" id = "control3" name = "control3" />
<input type = "submit" />
Run Code Online (Sandbox Code Playgroud)

用户触发一些Javascript,页面结果如下:

<input type = "text" id = "control1" name = "control1" />
<input type = "text" id = "control2" name = "control2" />
<input type = "text" id = "control3" name = "control3" />
<input type = "text" id = "control4" name = "control4" />
<input type = "text" id = "control5" name = "control5" />
<input type = "submit" />
Run Code Online (Sandbox Code Playgroud)

处理这种情况的最佳方法是什么,或者更一般地说,使用动态生成的客户端和服务器端输入(例如,如何从数据库中获取的某些数据开始生成服务器端)?

Use*_*ser 0

在服务器端查看 Request.Form["controlN"] ,您将在其中找到旧字段和新字段。

在服务器端,您可以动态添加控件,例如:

foreach (Type object in MyDBCollection)
{
   Controls.Add (new TextBox ());
}
Run Code Online (Sandbox Code Playgroud)