服务器控件动态添加行,并获取值
在原有table的基础上添加行,点击按钮添加一新行。怎么获取新添加行里面textbox中的值?
我下面的代码只能添加一行,每次点击都只有一行。
前台代码:
<table runat="server" id="tabOperate" visible="true" style="width: 400px; text-align: center;">
<tr style="background-color: #CBCBCB; height: 30px;">
<td colspan="3">
若要新增一行請單擊--> <asp:ImageButton runat="server" ID="imgBtnAdd" ImageUrl="~/Images/add.gif" OnClick="imgBtnAdd_Click" />
</td>
</tr>
<tr style="background-color: #A9A9A9; height: 26px;">
<td style="width: 120px;">
LOB
</td>
<td>
Category
</td>
<td>
周數據
</td>
</tr>
</table>
后台代码:
protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
{
HtmlTableRow tr1 = new HtmlTableRow();
HtmlTableCell td1 = new HtmlTableCell();
HtmlTableCell td2 = new HtmlTableCell();
HtmlTableCell td3 = new HtmlTableCell();
TextBox txt1 = new TextBox();
txt1.Text = "";
txt1.ID = "tbLob" + tabOperate.Rows.Count;
TextBox txt2 = new TextBox();
txt2.Text = "";
txt2.ID = "tbCategory" + tabOperate.Rows.Count;
TextBox txt3 = new TextBox();
txt3.Text = "";
txt3.ID = "tbData" + tabOperate.Rows.Count;
td1.Controls.Add(txt1);
td2.Controls.Add(txt2);
td3.Controls.Add(txt3);
tr1.Cells.Add(td1);
tr1.Cells.Add(td2);
tr1.Cells.Add(td3);
tabOperate.Rows.Insert(tabOperate.Rows.Count, tr1);
}
------解决方案--------------------1、给textbox添加焦点事件 失去或得到时触发
比如txt3.Attributes.Add("onfocus", "javascript:Change('" + txtbox.ID + "');");
function ChangeText(id) {
value = document.getElementById("" + id + "").value;
}
2、根据你生成textbox规律遍历所有
我也只想到这么点 不知道对你有没有帮助
------解决方案--------------------
HTML code
<table runat="server" id="tabOperate" border="1" visible="true" style="width: 400px; text-align: center;">
<tr style="height: 30px;">
<td colspan="3">
若要新增一行请单击-->
<asp:Button ID="btnAdd" runat="server" Text="添加" onclick="btnAdd_Click" />
</td>
</tr>
<tr style="height: 26px;">
<td style="width: 120px;">
LOB
</td>
<td>
Category
</td>
<td>
周数据
</td>
</tr>
</table>
------解决方案--------------------
感觉用js灵活点 0 0