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

编程入门 行业动态 更新时间:2024-10-27 12:25:51
本文介绍了将数据绑定到HTML表中,而无需使用asp数据绑定控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你好, 我正在尝试将数据从数据表绑定到html表中,在我的数据表中,它具有50个区名称及其优先顺序的条目. 我想以HTML表格中的优先顺序(以垂直方式而不是水平方式)将此地区列表绑定(即以表格格式,该表格将仅具有10行和5列.(10 * 5 = 50个条目). 因此,请帮助(如果有人有解决方案的话)...我已经尝试了很多,但是数据是水平显示的,我不需要.. 谢谢

Hello , i am trying to bind data into html table from datatable, in my datatable it has 50 entries of district name with their preference order. i want to bind this district list with thier preference order (in vertical manner not horizontal) in HTML Table ( i.e in tabular format which will have 10 rows and 5 columns only.( 10*5=50 entries). so, pls help if any one has it''s solution... i have tried a lot but data is being displayed in horizontal which i don''t need.. Thanks

推荐答案

我已经尝试过这样..... 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

更多推荐

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

本文发布于:2023-10-23 23:12:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1522283.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   数据   控件   HTML   net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!