问题描述
我有两个名为arrRoll []和arrNames []的数组
再说arrRoll = {8001,8002}和arrNames = {"AAAA","BBBB"}
其中包含学校的学生人数和姓名
现在我想在asp.net中生成以下表格
在div标签内
i have two arrays named arrRoll[] and arrNames[]
say arrRoll={8001,8002} and arrNames={"AAAA","BBBB"}
which contain rollnumber and name of students in a school
now i want to generate folowing table in asp.net
inside of a div tag
<div id="tblCont">
<table id="tbl1">
<tr>
<td>Checkbox</td>
<td>Name</span></td>
<tr>
<td><input type="checkbox" name="cbRoll" value="8001" id="cbRoll8001" /></td>
<td>AAAA</td>
</tr>
<tr>
<td><input type="checkbox" name="cbRoll" value="8002" id="cbRoll8002"/></td>
<td>BBBB</td>
</tr>
</table>
</div>
在我的aspx页面上,我有
on my aspx page i have
<div id="tblCont" runat="server">
</div>
现在如何以编程方式将上面的表格插入此div标签中?
设置arrRoll的复选框的值和id以及arrNames
now how do i insert the above table inside of this div tag programatically?
setting the value and id of the checkbox from the arrRoll and the innerText of table cell from arrNames
推荐答案
<asp:Table runat="server" ID="asptable">
for(int i =0; i < ar1.Length; i++)
{
TableRow tr = new TableRow();
TableCell c = new TableCell();
CheckBox cb = new CheckBox();
cb.Attributes.Add("onclick", "alert(\"added\")");
cb.AutoPostBack = true;
cb.CheckedChanged+=new EventHandler(cb_CheckedChanged);
cb.ID = "cbRoll" + ar1[i];
c.Controls.Add(cb);
tr.Cells.Add(c);
TableCell c2 = new TableCell();
c2.Text = ar2[i];
tr.Cells.Add(c2);
asptable.Rows.Add(tr);
}
}
protected void cb_CheckedChanged(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)sender;
// do what ever you want to do...
}
2)或者您可以在服务器端使用HtmlTextWriterTag编写自己的html(例如在其中编写自己的表格标签和所有其他子元素)
如果您在客户端进行操作
1)您可以使用jquery或普通的Java在div标签中添加html元素或在表中添加表行等...
2) or you can use HtmlTextWriterTag at server side to write your own html ( like write your own table tags and all other child elements in it)
if you doing it on client side
1) you can use jquery or normal java to append html elements in your div tag or table rows in your table etc...
private void CreateDynamicTable()
{
PlaceHolder1.Controls.Clear();
int tblRows = arrNames.Length;
int tblCols = 2;
Table tbl = new Table();
PlaceHolder1.Controls.Add(tbl);
for (int i = 0; i < tblRows; i++)
{
TableRow tr = new TableRow();
for (int j = 0; j< tblCols; j++)
{
TableCell tc = new TableCell();
if( j ==0){
CheckBox chk = new CheckBox();
chk.id = "cbroll_" + arrRole[i]
tc.Controls.Add(chk);
}
else
{
Lable lbl = new Lable();
lbl.text = arrNames[i];
tc.Controls.Add(lbl);
}
tc.Controls.Add(txtBox);
// Add the TableCell to the TableRow
tr.Cells.Add(tc);
}
// Add the TableRow to the Table
tbl.Rows.Add(tr);
}
}
这篇关于如何以编程方式从数组创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!