js 动态生成 table的问题
我想问一下,我用js动态生成的table里面为什么不能放控件啊,如 input,或是asp.net的控件,难道不能放吗?
我的方法如下:
function CreateTable(ds) { var maxRow =8; var maxCol = 3; var aaa="test"; var strTbody ="<table <tbody>"; strTbody+="<tr>"; for(var j = 0; j < maxCol; j++) { strTbody+="<td><input id=\"txt"+i+"\" type =\"text\" value ="+aaa+" /></td>"; } strTbody+="</tr>"; strTbody+="</tbody></table>"; var obj = document.getElementById("tableDiv"); obj.innerHTML = strTbody; }
function addRow(){//添加一行var newTr = tableone.insertRow();//添加3列var newTd0 = newTr.insertCell();var newTd1 = newTr.insertCell();var newTd2 = newTr.insertCell();//设置列内容和属性newTd0.className = "LtableCopy";newTd0.height = "25";newTd0.align = "center";newTd0.innerHTML = "<input type='text' name='pickname' />"; newTd1.className = "LtableCopy";newTd1.height = "25";newTd1.align = "center";newTd1.innerHTML = "<input type='text' name='pickrelationship'/>"; newTd2.className = "LtableCopy";newTd2.height = "25";newTd2.align = "center";newTd2.innerHTML = "<input type='text' name='picknumber' />"; }
[解决办法]
Table t1 = new Table();
TableRow[] trList = new TableRow[dtProtal.Rows.Count];
for (int i = 0; i < dtProtal.Rows.Count; i++)
{
//添加第一列CheckBox
CheckBox[] cbList = new CheckBox[dtProtal.Rows.Count];
cbList[i] = new CheckBox();
cbList[i].Text = dtProtal.Rows[i][2].ToString();
cbList[i].ID = "cbID" + i;
if (Convert.ToBoolean(dtProtal.Rows[i][3]))
{
cbList[i].Checked = true;
}
TableCell cellName = new TableCell();
cellName.Controls.Add(cbList[i]);
//添加第二列HtmlSelect
HtmlSelect[] selectList = new HtmlSelect[dtProtal.Rows.Count];
selectList[i] = new HtmlSelect();
selectList[i].ID = "select" + i;
selectList[i].Value = dtProtal.Rows[i][1].ToString();
selectList[i].Attributes.Add("style", "width:80px;");
selectList[i].Attributes.Add("runat", "server");
ListItem itemL = new ListItem();
itemL.Text = "左列";
itemL.Value = "0";
ListItem itemM = new ListItem();
itemM.Text = "中列";
itemM.Value = "1";
ListItem itemR = new ListItem();
itemR.Text = "右列";
itemR.Value = "2";
if (Convert.ToChar(dtProtal.Rows[i][1]) == '0')
itemL.Selected = true;
if (Convert.ToChar(dtProtal.Rows[i][1]) == '1')
itemM.Selected = true;
if (Convert.ToChar(dtProtal.Rows[i][1]) == '2')
itemR.Selected = true;
if (cbSystem.Checked)
selectList[i].Items.Add(itemR);
else
{
selectList[i].Items.Add(itemL);
selectList[i].Items.Add(itemM);
selectList[i].Items.Add(itemR);
}
TableCell cellType = new TableCell();
cellType.Controls.Add(selectList[i]);
//添加第三列HiddenField列
HiddenField[] hiddenList = new HiddenField[dtProtal.Rows.Count];
hiddenList[i] = new HiddenField();
hiddenList[i].ID = "hidden" + i;
hiddenList[i].Value = dtProtal.Rows[i][4].ToString();
TableCell cellHidden = new TableCell();
cellHidden.Controls.Add(hiddenList[i]);
//实例化row,添加列
trList[i] = new TableRow();
trList[i].Controls.Add(cellName);
trList[i].Controls.Add(cellType);
trList[i].Controls.Add(cellHidden);
t1.Controls.Add(trList[i]);
}
Panel1.Controls.Add(t1);