日期:2014-05-19  浏览次数:20406 次

服务器端如何得到动态htm控件的值,请大家来帮帮忙。
问题详细描述,由于业务需要在页面中生成大量动态控件如 <input   name= 'TxtChild "+   childCounter   +   " '     type= 'text '   id= 'TxtChlid "+   childCounter   +   " '   Class= 'edline '   runat= 'server '> ;问题是当用户修改了以后如何让后台取得这些数据呢?
我也看了很多关于这方面的问题可是由于小弟悟性较差始终都没有解决,请大家帮帮忙!

------解决方案--------------------
这里的runat= 'server '是没有意义的,可以去掉 得到它的值用 Request.Form[ "TxtChild "+childCounter]
------解决方案--------------------

------解决方案--------------------
TextBox tx = new TextBox();
....
....
this.Controls.Add(tx);

------解决方案--------------------
给个简单的例子:
<%@ Page Language= "C# " AutoEventWireup= "true " CodeFile= "Default.aspx.cs " Inherits= "_Default " %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> Untitled Page </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:Panel ID= "Panel1 " runat= "server " Height= "50px " Width= "125px ">
</asp:Panel>
<asp:Button ID= "Button1 " runat= "server " Text= "Button " OnClick= "Button1_Click " />
</div>

</form>
</body>
</html>


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

public partial class _Default : System.Web.UI.Page
{

protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);

if (ViewState[ "DynamicLoadTextbox "] != null)
{
LoadTextBox();
}
}

protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
LoadTextBox();
}

}
private void LoadTextBox()
{
for (int i = 0; i < 10; i++)
{
TextBox input = new TextBox();
input.ID = "input " + i.ToString();
Panel1.Controls.Add(input);
}
ViewState[ "DynamicLoadTextbox "] = true;
}

protected void Button1_Click(object sender, EventArgs e)
{
TextBox tb = Panel1.FindControl( "input0 ") as TextBox;
}
}

------解决方案--------------------
动态控件的runat=server是没用的,客户提交过去的页面信息应该能取到控件Request.Form[ "TxtChild "+childCounter]的值,
不过我建议你放个hiden或者文本框隐藏来保存客户输入的信息,服务器端就去这个控件里的内容,这样就用考虑客户生成了几个控件了,不然控件的ID问题是个麻烦的事

------解决方案--------------------
这里的runat= 'server '是没有意义的,可以去掉

得到它的值用