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

动态增加控件textbox并取值得它的值而且要写入到数据库里面
页面上有一个textbox文本框,输入数字后,点击增加按扭能在页面上增加一组控件,依次是textbox,textbox,checkbox,排成一行,就象是放在一个1行3列的table里面
eg:输入3后就就能增加3组textbox,textbox,checkbox
然后要在后台里面得到这组增加的值,并一组循环写入到数据库里面


------解决方案--------------------
//生成按钮:根据指标数量动态生成
protected void ibtnType_Click(object sender, ImageClickEventArgs e)
{
this.Panel3.Visible = true;
DataSet ds = new DataSet();
ds.Tables.Add();
ds.Tables[0].Columns.Add( "QuestName ");
ds.Tables[0].Columns.Add( "QuestID ");
ds.Tables[0].Columns.Add( "SortID ");
string [] row = { " ", " ", " "};
int Num = int.Parse(this.txtQuestNum.Text);
for (int i = 0; i < Num; i++)
{
ds.Tables[0].Rows.Add(row);
}
this.dlQuest.DataSource = ds;
this.dlQuest.DataBind();
}
------解决方案--------------------
对于LZ的需求个人认为不一定大家非要用服务器控件
1.如果输入的内容提交后不需要回显到页面,那么建议使用JS来动态增加。在后台可以使用Request.Form[ "textbox1 "]来分别取得相应的值。 这么做的好处是页面友好不闪屏。
2.有一种简单但低技术含量的办法,就是提前放上几行定义好的服务器控件放在那里,通过服务器端的Visible或者JS中的display来控制显示和隐藏。
3.使用DataGrid,DataList或Repeater来处理,需要加行时在后台创建空数据行绑定即可,这里要用到模板列。
4.使用服务器端的table来解决,就是 <table id= "t " runat= "server ">
------解决方案--------------------
刚刚我在最前面已经回复了,用JS画控件的方法,一次画一组应该没问题吧,然后每个控件的名字就是用a1,b1,c1 a2,b2,c2这样有规律的命名。既然是动态画控件,所以数量肯定是不定的,把总共画的控件的数量放进一个隐藏域中。在后台取到隐藏域的值,比如是10,那就一个for循环,把a1,b1,c1~a10,b10,c10全部值都可以依次取到,放进数据库
------解决方案--------------------
我一向是动态生成html控件,再在后台用request.form来获取.
------解决方案--------------------
function createRow(rows)
{
document.getElementById( 'tbxPrimeNum ').value=rows;
tb3 = document.getElementById( 'tb3 ');
oldRow = tb3.childNodes[0].childNodes.length;
if(rows> oldRow)
{
for(i=oldRow;i <rows;i++)
{
newRow = tb3.insertRow();
newCell = newRow.insertCell();
newCell.align= "left ";
newCell.innerHTML = " <input type= 'text ' name= 'ProjectName " + i + " ' style= 'width: 200px ' class= 'UnderlineTextBoxLight ' /> ";

newCell = newRow.insertCell();
newCell.align= "left ";
newCell.innerHTML = " <input type= 'text ' onkeypress= 'return valNumDot() ' name= 'ContractAmount " + i + " ' style= 'width:150px ' class= 'UnderlineTextBoxLight ' /> ";

newCell = newRow.insertCell();
newCell.align= "left ";
newCell.innerHTML = " <input type= 'text ' name= 'WageDecisionNumber " + i + " ' style= 'width: 150px ' class= 'UnderlineTextBoxLight ' /> ";

newCell = newRow.insertCell();
newCell.align= "left ";
newCell.innerHTML = " <input type= 'text ' onkeypress= 'return valDate() ' onclick= 'display(this) ' id= 'WageLockDate " + i + " ' name= 'WageLockDate " + i + " ' style= 'width: 150px ' class= 'UnderlineTextBoxLight ' /> ";

}
}