我页面中的一个<table>有20行如(<td id=1 runat=server></td>...共有20个)
在后台我从数据库中select出一个DataTable我不知道这个DataTable到底有多少行,但是我要把我找出的这个DataTable每一行中name字段的内容要能在页面中对应行数的td中显示出来。
谁能帮下忙告诉我要怎么做能动态绑定?

解决方案 »

  1.   

    foreach(DateRow dr in DateTable.Rows)为什么不用GridView呢?
      

  2.   

    同意楼上,使用DataGridView,不过如果你真的想使用HTML来绑的话,可以尝试使用foreach来解决,不过效率低下,算法复杂,功能简单。
      

  3.   

    自己循环写,给你个例子:
    function binddata()

        var table=document.getElementById("tabAllot");
        var ntr;
        var trr=table.rows[0];
        var dt=getselectdata();
        for(i=0;i<dt.Rows.length;i++)
        {
            if(i>table.rows.length-2)
            {
                if(table.rows.length%2==1)
                {
                    ntr=document.getElementById("trmode1").cloneNode(3);
                }
                else
                {
                    ntr=document.getElementById("trmode2").cloneNode(3);
                }
                ntr.childNodes[0].firstChild.innerText=dt.Rows[i]["BILLNO"];
                ntr.childNodes[0].firstChild.title=dt.Rows[i]["OPERATIONNO"];
                ntr.childNodes[1].innerText=dt.Rows[i]["STATE"]=="0"?"待分派":dt.Rows[i]["STATE"]=="1"?"已分派":dt.Rows[i]["STATE"]=="2"?"已结束":"未知状态";
                ntr.childNodes[2].innerText=dt.Rows[i]["EQUITNAME"];
                ntr.childNodes[3].innerText=dt.Rows[i]["BRAND"];
                ntr.childNodes[4].innerText=dt.Rows[i]["TYPE"];
                ntr.childNodes[5].innerText=dt.Rows[i]["ERRDESC"];
                ntr.childNodes[6].innerText=dt.Rows[i]["CLIENTNAME"];
                trr.parentNode.appendChild(ntr);
            }
            else
            {
                var tmd=table.rows[i+1];
                tmd.childNodes[0].firstChild.innerText=dt.Rows[i]["BILLNO"];
                tmd.childNodes[0].firstChild.title=dt.Rows[i]["OPERATIONNO"];
                tmd.childNodes[1].innerText=dt.Rows[i]["STATE"]=="0"?"待分派":dt.Rows[i]["STATE"]=="1"?"已分派":dt.Rows[i]["STATE"]=="2"?"已结束":"未知状态";
                tmd.childNodes[2].innerText=dt.Rows[i]["EQUITNAME"];
                tmd.childNodes[3].innerText=dt.Rows[i]["BRAND"];
                tmd.childNodes[4].innerText=dt.Rows[i]["TYPE"];
                tmd.childNodes[5].innerText=dt.Rows[i]["ERRDESC"];
                tmd.childNodes[6].innerText=dt.Rows[i]["CLIENTNAME"];
            }
        }
        
        while(table.rows.length-1>dt.Rows.length)
        {   
            if(table.rows.length>3)
            {
                table.deleteRow(table.rows.length-1);
            }
            else if(dt.Rows.length<2)
            {   
                if(dt.Rows.length==0)
                {
                    table.rows[1].childNodes[0].firstChild.innerText="";
                    table.rows[1].childNodes[0].firstChild.title="";
                    table.rows[1].childNodes[1].innerText="";
                    table.rows[1].childNodes[2].innerText="";
                    table.rows[1].childNodes[3].innerText="";
                    table.rows[1].childNodes[4].innerText="";
                    table.rows[1].childNodes[5].innerText="";
                    table.rows[1].childNodes[6].innerText="";
                }
                table.rows[2].childNodes[0].firstChild.innerText="";
                table.rows[2].childNodes[0].firstChild.title="";
                table.rows[2].childNodes[1].innerText="";
                table.rows[2].childNodes[2].innerText="";
                table.rows[2].childNodes[3].innerText="";
                table.rows[2].childNodes[4].innerText="";
                table.rows[2].childNodes[5].innerText="";
                table.rows[2].childNodes[6].innerText="";
                break;
            }
        }
    }
    ----------------------
    我使用的第1行为 列头,2,3行为模板 因为有些样式设置。你也可以只自己直接插入行和单元格
      

  4.   

    大概就是这样了: 
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                    DisplayData();
            }
            protected void DisplayData()
            {
                DataTable dt = 你的select出来的datatable;
                for (int i = 0; i < dt.Rows.Count; i++)
                {                HtmlTableCell td = this.Page.FindControl("td" + i.ToString()) as HtmlTableCell;
                    if (td != null)
                    {
                        td.InnerHtml = dt.Rows[i]["name"];
                    }
                }
            }
    html<div>
                <table>
                    <tr>
                        <td id="td1" runat="server">
                        </td>
                    </tr>
                    <tr>
                        <td id="td2" runat="server">
                        </td>
                    </tr>
                    <tr>
                        <td id="td3" runat="server">
                        </td>
                    </tr>
                    <tr>
                        <td id="td4" runat="server">
                        </td>
                    </tr>
                </table>
            </div>
      

  5.   

    你应该用gridview去绑定你的datatable,而不是手动写html代码来实现table
      

  6.   

    <table>
    <asp:Repeater id="rp1" runat="server">
    <itemtemplate>
    <tr><td id='<%#Eval("NAME")%>'></td></tr>
    </itemtemplate>
    </asp:Repeater>
    </table>