在asp.net中将bootstrap数据表添加到literal

时间:2014-01-23 09:44:27

标签: asp.net twitter-bootstrap


我正在尝试将bootstrap数据表添加到我的项目中 当我在源代码中尝试以下代码时,它可以正常工作

 <table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered table-hover" id="example">
<thead>
    <tr>
        <th>Rendering engine</th>
        <th>Browser</th>
        <th>Platform(s)</th>
        <th>Engine version</th>
        <th>CSS grade</th>
    </tr>
</thead>
<tbody>
    <tr class="odd gradeX">
        <td>Trident</td>
        <td>Internet
             Explorer 4.0</td>
        <td>Win 95+</td>
        <td class="center"> 4</td>
        <td class="center">X</td>
    </tr>
    <tr class="even gradeC">
        <td>Trident</td>
        <td>Internet
             Explorer 5.0</td>
        <td>Win 95+</td>
        <td class="center">5</td>
        <td class="center">C</td>
    </tr>
    <tr class="odd gradeA">
        <td>Trident</td>
        <td>Internet
             Explorer 5.5</td>
        <td>Win 95+</td>
        <td class="center">5.5</td>
        <td class="center">A</td>
    </tr>
    <tr class="even gradeA">
        <td>Trident</td>
        <td>Internet
             Explorer 6</td>
        <td>Win 98+</td>
        <td class="center">6</td>
        <td class="center">A</td>
    </tr>
    <tr class="odd gradeA">
        <td>Trident</td>
        <td>Internet Explorer 7</td>
        <td>Win XP SP2+</td>
        <td class="center">7</td>
        <td class="center">A</td>
    </tr>
</tbody>


同时当我尝试从后面的代码中执行此操作时,它会失败

string table_contect = "<table cellpadding="+"\0\" cellspacing="+"\0\" border="+"\0\" class="+"\"table table-striped table-bordered table-hover\""+" id="+"\"example\'" +"><thead><tr><th>Rendering engine</th><th>Browser</th><th>Platform(s)</th><th>Engine version</th><th>CSS grade</th></tr></thead>";
        table_contect += " <tbody><tr class=" + "\"odd gradeX\""+ "><td>Trident</td><td>Internet Explorer 4.0</td><td>Win 95+</td><td class=" + "\"center\"" + "> 4</td><td class=" + "\"center\"" + ">X</td></tr><tr class=" + "\"even gradeC\"" + ">";
        table_contect += "<td>Trident</td><td>Internet Explorer 5.0</td><td>Win 95+</td><td class=" + "\"center\"" + ">5</td><td class=" + "\"center\"" + ">C</td></tr><tr class=" + "\"odd gradeA\"" + "><td>Trident</td><td>Internet Explorer 5.5</td>";
        table_contect += "<td>Win 95+</td><td class=" + "\"center\"" + ">5.5</td><td class=" + "\"center\"" + ">A</td></tr><tr class=" + "\"even gradeA\"" + "><td>Trident</td><td>Internet Explorer 6</td><td>Win 98+</td><td class=" + "\"center\"" + ">6</td><td class=" + "\"center\"" + ">A</td>";
        table_contect += "</tr><tr class=" + "\"odd gradeA\"" + "><td>Trident</td><td>Internet Explorer 7</td><td>Win XP SP2+</td><td class=" + "\"center\"" + ">7</td><td class=" + "\"center\"" + ">A</td></tr><tr class=" + "\"even gradeA\"" + "><td>Trident</td><td>AOL browser (AOL desktop)</td><td>Win XP</td>";
        table_contect += "<td class=" + "\"center\"" + ">6</td><td class=" + "\"center\"" + ">A</td></tr><tr class=" + "\"gradeA\"" + "><td>Gecko</td><td>Firefox 1.0</td><td>Win 98+ / OSX.2+</td><td class=" + "\"center\"" + ">1.7</td><td class=" + "\"center\"" + ">A</td></tr><tr class=" + "\"gradeA\"" + "><td>Gecko</td><td>Firefox 1.5</td>";
        table_contect += "<td>Win 98+ / OSX.2+</td><td class=" + "\"center\"" + ">1.8</td><td class=" + "\"center\"" + ">A</td></tr><tr class=" + "\"gradeA\"" + "><td>Gecko</td><td>Firefox 2.0</td><td>Win 98+ / OSX.2+</td><td class=" + "\"center\"" + ">1.8</td><td class=" + "\"center\"" + ">A</td></tr><tr class=" + "\"gradeA\"" + "><td>Gecko</td>";
        table_contect += "<td>Firefox 3.0</td><td>Win 2k+ / OSX.3+</td><td class=" + "\"center\"" + ">1.9</td><td class=" + "\"center\"" + ">A</td></tr><tr class=" + "\"gradeA\"" + "><td>Gecko</td>";
        table_contect += "<td>Camino 1.0</td><td>OSX.2+</td><td class=" + "\"center\"" + ">1.8</td><td class=" + "\"center\"" + ">A</td></tr><tr class=" + "\"gradeA\"" + "><td>Gecko</td><td>Camino 1.5</td><td>OSX.3+</td><td class=" + "\"center\"" + ">1.8</td>";
        table_contect += "<td class=" + "\"center\"" + ">A</td></tr><tr class=" + "\"gradeA\"" + "><td>Gecko</td><td>Netscape 7.2</td><td>Win 95+ / Mac OS 8.6-9.2</td><td class=" + "\"center\"" + ">1.7</td>";
        table_contect += "<td class=" + "\"center\"" + ">A</td></tr><tr class=" + "\"gradeA\"" + "><td>Gecko</td><td>Netscape Browser 8</td><td>Win 98SE+</td><td class=" + "\"center\"" + ">1.7</td>";
        table_contect += "<td class=" + "\"center\"" + ">A</td></tr><tr class=" + "\"gradeA\"" + "><td>Gecko</td><td>Netscape Navigator 9</td><td>Win 98+ / OSX.2+</td><td class=" + "\"center\"" + ">1.8</td>";
        table_contect += "<td class=" + "\"center\"" + ">A</td></tr><tr class=" + "\"gradeA\"" + "><td>Gecko</td><td>Mozilla 1.0</td><td>Win 95+ / OSX.1+</td><td class=" + "\"center\"" + ">1</td><td class=" + "\"center\"" + ">A</td>";
        table_contect += "</tr><tr class=" + "\"gradeA\"" + "><td>Gecko</td><td>Mozilla 1.1</td><td>Win 95+ / OSX.1+</td><td class=" + "\"center\"" + ">1.1</td><td class=" + "\"center\"" + ">A</td></tr></tbody></table>";
        //Response.Write(table_contect);
        Literal1.Text = table_contect;<br>

我得到的表中的行没有标题,过滤器文本框,分页。为什么会这样。

我正在同一页面上尝试。

1 个答案:

答案 0 :(得分:0)

看起来你的第一个字符串连接是无效的,并且不会产生与之前显示的相同的输出。例如,这一位:

"<table cellpadding="+"\0\" cellspacing="+"\0\"...

应阅读:

"<table cellpadding=\"0\" cellspacing=\"0\"...

还有很多不必要的连接在进行中。

如果您想要准确表示您正在生成的内容,最好在调试时转储table_contect的内容。如果您从浏览器查看源代码,浏览器很可能会尝试修复并理解结构不合理的HTML。因此,这不会准确地描述您以编程方式生成的HTML。

您在一个大文字中以编程方式构建它有什么优势?您可以使用System.Web.UI.HtmlControls.HtmlTable来分解它,并单独指定行等。