ASP.NET在表的特定单元格中插入文本 [英] ASP.NET insert text in specific cell of table

查看:126
本文介绍了ASP.NET在表的特定单元格中插入文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在表格的特定单元格中插入任何文本.在运行时

how to insert any text in a particular cell of table. at runtime

the table is not of Database but a normal table on webpage.

推荐答案

如果要在客户端执行此操作,则需要编写Javascript来分配属性<将某些td元素的c0>(表示一个表格单元格)设置为某个值.

在服务器端,您可以使用类System.Web.UI.WebControls.Table.单个单元格由类System.Web.UI.WebControls.TableHeaderCell的实例表示;其文本由属性System.Web.UI.WebControls.TableHeaderCell.Text表示.您可以通过属性System.Web.UI.WebControls.Table.Rows从表中访问单个单元格,访问一行时,可以通过其属性System.Web.UI.WebControls.TableRow.Cells访问单个行的单元格.

请注意 http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.table.aspx [ http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.table.rows.aspx [ http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.tablerow.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.tablerow.cells.aspx [ http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.tablecell.aspx [ ^ ].

—SA
If you want to do it in client side, you need to write Javascript which assign the property innerHtml of some td element (representing a table cell) to some value.

On server side you can use the class System.Web.UI.WebControls.Table. An individual cell is represented by the instance of the class System.Web.UI.WebControls.TableHeaderCell; and its text is represented by the property System.Web.UI.WebControls.TableHeaderCell.Text. You can access individual cells from the table via the property System.Web.UI.WebControls.Table.Rows and, when you access a row, you can access individual row''s cell via its property System.Web.UI.WebControls.TableRow.Cells.

Pay attention for the note from http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.table.aspx[^]:

It is important to remember that any programmatic addition or modification of table rows or cells will not persist across posts to the server. This is because table rows and cells are controls of their own, and not properties of the Table control. To persist any changes to the table, rows and cells must be reconstructed after each postback. In fact, if substantial modifications are expected, it is recommended that a DataList, DataGrid, or GridView control be used instead of the Table control. As a result, the Table class is primarily used by control developers.
See also:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.table.rows.aspx[^],
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.tablerow.aspx[^],
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.tablerow.cells.aspx[^],
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.tablecell.aspx[^].

—SA


老实说,您需要提供有关所拥有内容和页面编码方式的更多详细信息,以便我们提供有用的答案.让我给您具体说明为什么.

如果您的表具有静态结构(即行和列永远不变,仅单元格中的文本),并且希望在页面加载时插入文本,那么最简单的方法可能是使用原始HTML在定义表格的布局(ASPX)页中,并且在每个单元格内都有asp:Literal标记.

I honestly think you need to provide more details about what you have and the way the page is coded, for us to provide helpful answers. Let me give you specific examples of why.

If you have a table with a static structure (i.e. the rows and columns never change, just the text in the cells), and you want the text to be inserted when the page is loaded, then your simplest approach might be to have raw HTML in your layout (ASPX) page that defines the table, and have asp:Literal tags inside each cell.

<table>
<tr>
<td><asp:Literal name="cell11" runat="Server" /></td>
<td><asp:Literal name="cell12" runat="Server" /></td>
</tr>
<tr>
<td><asp:Literal name="cell21" runat="Server" /></td>
<td><asp:Literal name="cell22" runat="Server" /></td>
</tr>
</table>



然后,在代码隐藏页面中,将声明与每个文字元素关联的变量,并在Page_Load方法中,可以为这些文字中的每一个分配值.这将用每个值填充单元格.

但是,如果您的表将具有可变的列和行,那么最好在代码隐藏页面的函数中构造整个对象.有很多花哨的"方法可以做到这一点,但是老实说,如果您感觉更舒适,可以总是只编写生成HTML文本并将其保存到随后显示在页面上的字符串的代码.

最后,如果通过在运行时"表示您希望能够在不重新加载页面的情况下更新表单元格中的文本,那么现在您已处在Javascript领域:这实际上不是ASP.NET问题全部!

因此,请提供更多有关您到目前为止所拥有的信息以及您正在努力实现的目标的更多信息,我们将为您提供更多帮助!



Then, in your code-behind page you will declare the variables associated with each literal element, and in the Page_Load method you can assign values to each of these literals. That will fill the cells with each value.

HOWEVER, if your table is going to have variable columns and rows, then you are better off constructing the entire thing in a function in the code-behind page. There are "fancy" ways of doing this, but honestly if you feel more comfortable you can always just write code that generates the HTML text and saves it to a string that is then displayed on the page.

FINALLY, if by "at runtime" you mean you want to be able to update the text in the cell of a table without reloading the page, then now you are in Javascript territory: it''s not really an ASP.NET question at all!

So please provide a little more information about what you have so far, and exactly what you are trying to achieve, and we can be more helpful!


这篇关于ASP.NET在表的特定单元格中插入文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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