如何以编程方式从数组创建表 [英] how to programatically create table from arrays

查看:117
本文介绍了如何以编程方式从数组创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个名为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

推荐答案

如果要在服务器端执行此操作,建议您使用-

1)asp:Table控件,您可以在其中循环遍历服务器端的数组,并使用要放置在TableCell中的任何控件插入新的tableRow和TableCell元素

If you want to do it on server side i would suggest you to use -

1) asp:Table control, where you can loop thru your arrays at serverside and insert new tableRow and TableCell elements with whatever controls you want to place in your TableCell

<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...


您可以在div标签中添加占位符,然后在占位符.检查div/占位符是否存在

You can add placeholder in div tag and then add table in placeholder. Check if the div/placeholder exist or not

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);
        }
 
    }


现在假设我想在用户更改复选框(打勾或取消勾打)时执行js函数.
我应该在asp.net页或后面的代码中添加此功能的地方
now suppose i want to execute a js function whenever user changes the checkbox (either ticks or unticks it)
where should i add this function in asp.net page or the codebehind


这篇关于如何以编程方式从数组创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆