简体   繁体   English

将HTML表代码转换为asp:table控件

[英]Convert HTML table code to asp:table control

我有一个HTML表代码(以<table>开头,以</ table>结尾),如何使我的ASP网页以编程方式添加此表?

If you have purely HTML code generated in server side, then you can add that HTML code having <Table> into your asp page like this. 如果您在服务器端生成了纯HTML代码,则可以将具有<Table> HTML代码添加到您的asp页中。

  1. Add a placeholder to aspx page like this. 像这样将占位符添加到aspx页面。

<asp:PlaceHolder ID="plc" runat="server" />

  1. In page_load event write this. 在page_load事件中写入此内容。

    String str = " <table><tr><td>TD VALUE</td></tr></table> " ; String str =<table><tr><td>TD VALUE</td></tr></table>;

    plc.Controls.Add(new LiteralControl(str));

This will emit html code , and place them to placeholder. 这将发出html代码,并将其放置到占位符。

ASSUMPTION This is just a readonly html code , that you wan to show in page, you are not intended to get those value in server side code on post back. 假设这只是一个只读的html代码,您希望在页面中显示它们,您无意在回发时从服务器端代码中获取这些值。 Nor there is any editable field inside the generated table. 生成的表内也没有任何可编辑的字段。

Either redo it in asp:table as you already mentioned or add runat="server" to your existing table and you will be able to alter it in codebehind. 如您已经提到的在asp:table重做它,或者将runat="server"添加到现有表中,您将可以在代码隐藏中对其进行更改。

After runat = server you can do things like runat = server您可以执行以下操作

protected void Page_Load(object sender, EventArgs e)

    {

       var str= tbl1.Rows[0].Cells[2].InnerHtml;



    }

But I would probably redo it in <asp:table> if you are using webforms. 但是,如果您使用的是Webforms,我可能会在<asp:table>重做。 See full example here Add rows to a table dynamically 在此处查看完整示例动态向表添加行

if you want to access this table programatically then use 如果要以编程方式访问此表,请使用

<table runat="server" id="YOUR_ID"></TABLE>  

it will then let you access this control through asp.net 然后它将允许您通过asp.net访问此控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM