且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将数据绑定到HTML表中,而无需使用asp.net数据绑定控件

更新时间:2022-06-15 03:03:38

我已经尝试过这样.....

Datatable1包含50行分区,其优先顺序如下:.

Pref_no District
1乌贾因
2拉特兰
3博帕尔
.... ......
这样..

我尝试过的代码如下:
i have tried like this.....

Datatable1 contains 50 row of district with their preference order like this..

Pref_no District
1 Ujjain
2 Ratlam
3 Bhopal
.... ......
in this way..

code which i tried is given below:
       int rows = Datatable1.Rows.Count / 10;
        int ck = Datatable1.Rows.Count % 10;
    string str = "<table border=" + 1 + ">"

       int cnt = Datatable1.Rows.Count-1;
        int cnt1 = Datatable1.Rows.Count-1;
       for (int i = 0; i < 10; i++)
      {
            if (ck == 0)
            {
                str += "<tr>";
                for (int j = 0; j < rows; j++)
               {

           if (Datatable1.Rows[cnt1 - (cnt)][0].ToString() != "")
                 {

                      str += "<td>" + Datatable1.Rows[cnt1 - (cnt)][0].ToString() + "</td><td>" + Datatable1.Rows[cnt1 - (cnt)][1].ToString() + "</td>";
                }
                 else if (Datatable1.Rows[cnt1 - (cnt - 1)][1].ToString() != "")
               {

                    str += "<td>" + dt1.Rows[cnt1 - (cnt)][0].ToString() + "</td><td>" + Datatable1.Rows[cnt1 - (cnt)][1].ToString() + "</td>";
                 }
                ;
            cnt--;
               }
                str += "</tr>";
         }
          else
          {
          str += "<tr>";
             for (int j = 0; j < rows+1; j++)
         {

              if (Datatable1.Rows[cnt1 - (cnt)][0].ToString() != "")
              {

                 str += "<td>" + Datatable1.Rows[cnt1 - (cnt)][0].ToString() + "</td><td>" + Datatable1.Rows[cnt1 - (cnt)][1].ToString() + "</td>";
                 }
                 else if (Datatable1.Rows[cnt1 - (cnt - 1)][1].ToString() != "")
                {

                      str += "<td>" + dt1.Rows[cnt1 - (cnt)][0].ToString() + "</td><td>" + Datatable1.Rows[cnt1 - (cnt)][1].ToString() + "</td>";
         }
                  ;
              cnt--;
          }
         str += "</tr>";
           }
}
    str += "</table>";
    Label1.Text = str;


最后,我得到了解决方案&完成要求后,我从ToolBox中获取了Table控件:

Finally i got the solution & completed the requirement , i have taken Table control from ToolBox:

<legend><strong>District Prefrence List</strong> </legend>
 <asp:Table ID="tblData" runat="server" GridLines="Both" Width="100%">
   </asp:Table>


在我写的页面后面的default.aspx.cs代码上:


on default.aspx.cs code behind page i wrote:

ArrayList PreferenceItem = new ArrayList();
ArrayList PreferenceValue = new ArrayList();
for (int i = 0; i < 50; i++)
{
    if (i < Datatable1.Rows.Count)
    {
        PreferenceItem.Add(Datatable1.Rows[i][0].ToString());
        PreferenceValue.Add(Datatable1.Rows[i][1].ToString());
    }
    else
    {
        PreferenceItem.Add(" ");
        PreferenceValue.Add(" ");
    }
}

for (int r = 0; r < 10; r++)
{
    TableRow row = new TableRow();
    for (int c = 0; c < 5; c++)
    {
        TableCell cell = new TableCell();
        cell.Width = Unit.Percentage(5);
        cell.Controls.Add(new LiteralControl(PreferenceItem[((c*10)
           + (r % 10))].ToString()));
        row.Cells.Add(cell);
        cell = new TableCell();
        cell.Width = Unit.Percentage(15);
       cell.Controls.Add(new LiteralControl(PreferenceValue[((c*10)
            + (r % 10))].ToString()));
        row.Cells.Add(cell);
    }
    tblData.Rows.Add(row);
}



遍历数据表并根据数据表中的数据动态创建html表.将行和列以及您的值添加到其中..
像这样尝试:
Hi,
Iterate through datatable and create the html table dynamically according to the data in datatable. Add the rows and columns to it with your values..
Try like this:
Table tbl = new Table();
tbl.Width = Unit.Percentage(100);
TableRow tblRow = new TableRow();
int cellInd = 0;
for (int i = 0; i < DataTable1.Rows.Count; i++)
{
    //Since you need 5 columns
    if (cellInd == 5)
    {
        // Break the row if the column count of that row becomes 5
        tblRow = new TableRow();
        cellInd = 1;
        //TableRow tblRow = new TableRow();
        TableCell tblCell = new TableCell();
        tblCell.Width = Unit.Percentage(20);
        tblCell.Text = dtRole.Rows[i]["MyColumn"] == DBNull.Value? "" :dtRole.Rows[i]["MyColumn"].ToString();
        tblRow.Controls.Add(tblCell);
        tbl.Rows.Add(tblRow);
    }
    else
    {
        //TableRow tblRow = new TableRow();
        TableCell tblCell = new TableCell();
        tblCell.Width = Unit.Percentage(20);
        tblCell.Text = dtRole.Rows[i]["MyColumn"] == DBNull.Value? "" :dtRole.Rows[i]["MyColumn"].ToString();
        tblRow.Controls.Add(tblCell);
        tbl.Rows.Add(tblRow);
        cellInd += 1;
    }
}
//Now add the dynamic table in any controls such like panels or placeholders.
Panel1.Controls.Add(tbl);




--Amit




--Amit