<asp:Table>的一些应用

本文介绍如何使用ASP.NET中的Table控件构建复杂的数据展示界面,并通过具体示例展示了如何动态填充表格内容,包括使用多种控件进行数据展示及交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 <asp:Table>在>.NET中是非常强大的,这个控件可以填充很多其它控件,下面我来举一些例子。

<xx.aspx>前台页面

<asp:Table ID="Table4" runat="server" CssClass="table_border" >                      
                    <asp:TableRow>
                        <asp:TableCell>Item</asp:TableCell>                       
                        <asp:TableCell>Part Name</asp:TableCell>
                        <asp:TableCell>Snap Shot</asp:TableCell>
                        <asp:TableCell>Project Status</asp:TableCell>
                        <asp:TableCell>Note</asp:TableCell>
                        <asp:TableCell>Part Order Quantity</asp:TableCell>
                        <asp:TableCell>Project Part Order Shipping Date after Sample Approval</asp:TableCell>
                        <asp:TableCell>Material for Samples</asp:TableCell>
                        <asp:TableCell>Materail for Production</asp:TableCell>                                   
                        <asp:TableCell>Shipping Notice</asp:TableCell>              
                                
                    </asp:TableRow>         
   </asp:Table>

 

<xx.aspx.cs>后台页面

  //显示Project Status & Shipping Notice
    protected void Show_StatusNotice(string projectnum)
    {
        TableRow row;
        TableCell cell;
        Image img;

        string strSql = "select * from status_notice ";
        strSql += " where projectnum='" + projectnum + "'";

        DataTable dt = CommonMySql.MySql_Select(strSql);
       
        int count = dt.Rows.Count;
        int k = 0;
        for (int i = 0; i < count; i++) //控制行数
        {
            k = i + 1;
            //新的一行
            row = new TableRow();           

            //添加第0列 item
            cell = new TableCell();
            Label lbl_item = new Label();
            lbl_item.ID = string.Concat("item_", k);
            lbl_item.Text = k.ToString();
            cell.Controls.Add(lbl_item);
            row.Cells.Add(cell);          

            //添加第1列 part number/name
            cell = new TableCell();
            cell.Style.Add("word-wrap", "break-word");//控制table换行
            cell.Width = 50;
            Label lbl_partname = new Label();
            lbl_partname.Width = 50;
            lbl_partname.ID = string.Concat("partname_", k);
            lbl_partname.Text = dt.Rows[i]["partname"].ToString();
            cell.Controls.Add(lbl_partname);
            row.Cells.Add(cell);

            //添加第2列 snap shot
            cell = new TableCell();
            img = new Image();
            img.Width = 50;
            img.Height = 40;
            img.ID = string.Concat("img_", k);
            img.ImageUrl = dt.Rows[i]["snapshot"].ToString();
            cell.Controls.Add(img);
            row.Cells.Add(cell);

            //添加第3列 Status
            cell = new TableCell();
            cell.Width = 320;
            Label lbl_ppt = new Label();
            lbl_ppt.ID = string.Concat("ppt_", k);
            lbl_ppt.Text = "● " + "Tooling Plan PPT ";
            cell.Controls.Add(lbl_ppt);          
            TextBox tb_ppt = new TextBox();
            tb_ppt.Width = 100;
            tb_ppt.ID = string.Concat("tbppt_",k);
            tb_ppt.Text = dt.Rows[i]["ppt"].ToString();
            cell.Controls.Add(tb_ppt);
            CheckBox chk_ppt = new CheckBox();
            chk_ppt.ID = string.Concat("chk_ppt", k);
            chk_ppt.Checked = Convert.ToBoolean(int.Parse(dt.Rows[i]["ppt_finish"].ToString()));
            cell.Controls.Add(chk_ppt);
            Label lbl_pptbr = new Label();
            //lbl_br.ID = string.Concat("br_", k);
            lbl_pptbr.Text = "<br/>";
            cell.Controls.Add(lbl_pptbr);

            Label lbl_design = new Label();
            lbl_design.ID = string.Concat("design_", k);
            lbl_design.Text = "● " + "Tooling Design Completed ";
            cell.Controls.Add(lbl_design);
            TextBox tb_design = new TextBox();
            tb_design.Width = 100;
            tb_design.ID = string.Concat("tbdesign_", k);
            tb_design.Text = dt.Rows[i]["design"].ToString();
            cell.Controls.Add(tb_design);
            CheckBox chk_design = new CheckBox();
            chk_design.ID = string.Concat("chk_design", k);
            chk_design.Checked = Convert.ToBoolean(int.Parse(dt.Rows[i]["design_finish"].ToString()));
            cell.Controls.Add(chk_design);
            Label lbl_designbr = new Label();
            lbl_designbr.Text = "<br/>";
            cell.Controls.Add(lbl_designbr);

            Label lbl_machining = new Label();
            lbl_machining.ID = string.Concat("machining_", k);
            lbl_machining.Text = "● " + "Tooling Machining ";
            cell.Controls.Add(lbl_machining);
            TextBox tb_machining = new TextBox();
            tb_machining.Width = 100;
            tb_machining.ID = string.Concat("tbmachining_", k);
            tb_machining.Text = dt.Rows[i]["machining"].ToString();
            cell.Controls.Add(tb_machining);
            CheckBox chk_machining = new CheckBox();
            chk_machining.ID = string.Concat("chk_machining", k);
            chk_machining.Checked = Convert.ToBoolean(int.Parse(dt.Rows[i]["machining_finish"].ToString()));
            cell.Controls.Add(chk_machining);
            Label lbl_machiningbr = new Label();
            lbl_machiningbr.Text = "<br/>";
            cell.Controls.Add(lbl_machiningbr);

            Label lbl_trail = new Label();
            lbl_trail.ID = string.Concat("trail_", k);
            lbl_trail.Text = "● " + "First Trail ";
            cell.Controls.Add(lbl_trail);
            TextBox tb_trail = new TextBox();
            tb_trail.Width = 100;
            tb_trail.ID = string.Concat("tbtrail_", k);
            tb_trail.Text = dt.Rows[i]["trail"].ToString();
            cell.Controls.Add(tb_trail);
            CheckBox chk_trail = new CheckBox();
            chk_trail.ID = string.Concat("chk_trail", k);
            chk_trail.Checked = Convert.ToBoolean(int.Parse(dt.Rows[i]["trail_finish"].ToString()));
            cell.Controls.Add(chk_trail);
            Label lbl_trailbr = new Label();
            lbl_trailbr.Text = "<br/>";
            cell.Controls.Add(lbl_trailbr);

            Label lbl_send = new Label();
            lbl_send.ID = string.Concat("send_", k);
            lbl_send.Text = "● " + "Send Samples for Approval ";
            cell.Controls.Add(lbl_send);
            TextBox tb_send = new TextBox();
            tb_send.Width = 100;
            tb_send.ID = string.Concat("tbsend_", k);
            tb_send.Text = dt.Rows[i]["send"].ToString();
            cell.Controls.Add(tb_send);
            CheckBox chk_send = new CheckBox();
            chk_send.ID = string.Concat("chk_send", k);
            chk_send.Checked = Convert.ToBoolean(int.Parse(dt.Rows[i]["send_finish"].ToString()));
            cell.Controls.Add(chk_send);
            Label lbl_sendbr = new Label();
            lbl_sendbr.Text = "<br/>";
            cell.Controls.Add(lbl_sendbr);

            row.Cells.Add(cell);

            //添加第4列 Note
            cell = new TableCell();
            cell.Style.Add("word-wrap", "break-word");//控制table换行
            cell.Width = 150;
            Label lbl_note = new Label();
            lbl_note.ID = string.Concat("note_", k);
            lbl_note.Text = dt.Rows[i]["note"].ToString();
            cell.Controls.Add(lbl_note);
            row.Cells.Add(cell);
           
            //添加第5列 Part Order Quantity
            cell = new TableCell();
            Label lbl_partquantity = new Label();
            lbl_partquantity.ID = string.Concat("partquantity_", k);
            lbl_partquantity.Text = dt.Rows[i]["partquantity"].ToString();
            cell.Controls.Add(lbl_partquantity);
            row.Cells.Add(cell);

            //添加第6列 Shipping date
            cell = new TableCell();
            cell.Width = 110;
            TextBox tb_shippingdate = new TextBox();
            tb_shippingdate.Width = 100;
            tb_shippingdate.ID = string.Concat("shippingdate_", k);
            tb_shippingdate.Text = dt.Rows[i]["shippingdate"].ToString();
            cell.Controls.Add(tb_shippingdate);
            row.Cells.Add(cell);

            //添加第7列 Material for Samples
            cell = new TableCell();
            cell.Width = 200;
            Label lbl_procured1 = new Label();
            lbl_procured1.ID = string.Concat("procured1_", k);
            lbl_procured1.Text = "● " + "Procured ";
            cell.Controls.Add(lbl_procured1);
            TextBox tb_procured1 = new TextBox();
            tb_procured1.Width = 100;
            tb_procured1.ID = string.Concat("tbprocured1_", k);
            tb_procured1.Text = dt.Rows[i]["procured1"].ToString();
            cell.Controls.Add(tb_procured1);
            CheckBox chk_procured1 = new CheckBox();
            chk_procured1.ID = string.Concat("chk_procured1", k);
            chk_procured1.Checked = Convert.ToBoolean(int.Parse(dt.Rows[i]["procured1_finish"].ToString()));
            cell.Controls.Add(chk_procured1);
            Label lbl_procured1br = new Label();
            //lbl_br.ID = string.Concat("br_", k);
            lbl_procured1br.Text = "<br/>";
            cell.Controls.Add(lbl_procured1br);

            Label lbl_received1 = new Label();
            lbl_received1.ID = string.Concat("received1_", k);
            lbl_received1.Text = "● " + "Received ";
            cell.Controls.Add(lbl_received1);
            TextBox tb_received1 = new TextBox();
            tb_received1.Width = 100;
            tb_received1.ID = string.Concat("tbreceived1_", k);
            tb_received1.Text = dt.Rows[i]["received1"].ToString();
            cell.Controls.Add(tb_received1);
            CheckBox chk_received1 = new CheckBox();
            chk_received1.ID = string.Concat("chk_received1", k);
            chk_received1.Checked = Convert.ToBoolean(int.Parse(dt.Rows[i]["received1_finish"].ToString()));
            cell.Controls.Add(chk_received1);
            Label lbl_received1br = new Label();
            //lbl_br.ID = string.Concat("br_", k);
            lbl_received1br.Text = "<br/>";
            cell.Controls.Add(lbl_received1br);
            row.Cells.Add(cell);

            //添加第8列 Material for Production
            cell = new TableCell();
            cell.Width = 200;
            Label lbl_procured2 = new Label();
            lbl_procured2.ID = string.Concat("procured2_", k);
            lbl_procured2.Text = "● " + "Procured ";
            cell.Controls.Add(lbl_procured2);
            TextBox tb_procured2 = new TextBox();
            tb_procured2.Width = 100;
            tb_procured2.ID = string.Concat("tbprocured2_", k);
            tb_procured2.Text = dt.Rows[i]["procured2"].ToString();
            cell.Controls.Add(tb_procured2);
            CheckBox chk_procured2 = new CheckBox();
            chk_procured2.ID = string.Concat("chk_procured2", k);
            chk_procured2.Checked = Convert.ToBoolean(int.Parse(dt.Rows[i]["procured2_finish"].ToString()));
            cell.Controls.Add(chk_procured2);
            Label lbl_procured2br = new Label();
            //lbl_br.ID = string.Concat("br_", k);
            lbl_procured2br.Text = "<br/>";
            cell.Controls.Add(lbl_procured2br);

            Label lbl_received2 = new Label();
            lbl_received2.ID = string.Concat("received2_", k);
            lbl_received2.Text = "● " + "Received ";
            cell.Controls.Add(lbl_received2);
            TextBox tb_received2 = new TextBox();
            tb_received2.Width = 100;
            tb_received2.ID = string.Concat("tbreceived2_", k);
            tb_received2.Text = dt.Rows[i]["received2"].ToString();
            cell.Controls.Add(tb_received2);
            CheckBox chk_received2 = new CheckBox();
            chk_received2.ID = string.Concat("chk_received2", k);
            chk_received2.Checked = Convert.ToBoolean(int.Parse(dt.Rows[i]["received2_finish"].ToString()));
            cell.Controls.Add(chk_received2);
            Label lbl_received2br = new Label();
            //lbl_br.ID = string.Concat("br_", k);
            lbl_received2br.Text = "<br/>";
            cell.Controls.Add(lbl_received2br);

            row.Cells.Add(cell);

            //添加第9列 Shiping Notice
            cell = new TableCell();
            cell.Width = 100;
            //Label lbl_exportationmold = new Label();
            //lbl_exportationmold.ID = string.Concat("exportationmold_", k);
            //lbl_exportationmold.Text = dt_tooling.Rows[i]["exportationmold"].ToString();
            //cell.Controls.Add(lbl_exportationmold);
            row.Cells.Add(cell);


            //增加一行
            Table4.Rows.Add(row);
        }
    }

效果见图片所示

<%@ Page Language="C#" MasterPageFile="~/master.master" AutoEventWireup="true" CodeFile="Manager.aspx.cs" Inherits="Manager" Title="My bss" %> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <div class="page" id="page"> <div id="table" runat=server class="table"> <h2 runat=server id="h2"></h2> <asp:GridView ID="articlelist" runat="server" CellPadding="4" ForeColor="#6C7E4A" GridLines="Horizontal" AllowPaging="True" AutoGenerateColumns="False" Width="100%" OnRowDeleting="articlelist_RowDeleting" > <FooterStyle BackColor="#BCD988" Font-Bold="True" ForeColor="#996600" /> <RowStyle BackColor="#F9F9F9" BorderColor="#BCD988" BorderStyle="Solid" BorderWidth="1px" /> <PagerStyle BackColor="#BCD988" ForeColor="#996600" HorizontalAlign="Left"/> <HeaderStyle BackColor="#BCD988" Font-Bold="True" ForeColor="#996600" HorizontalAlign="Center" VerticalAlign="Middle" /> <AlternatingRowStyle BackColor="White" /> <PagerSettings FirstPageText="首页" LastPageText="尾页" Mode="NextPreviousFirstLast" NextPageText="下一页" PreviousPageText="上一页" /> <Columns> <asp:BoundField DataField="id" HeaderText="文章编号" ReadOnly="True" > <ItemStyle CssClass="hidden" /> <HeaderStyle CssClass="hidden" /> <FooterStyle CssClass="hidden" /> </asp:BoundField> <asp:BoundField DataField="replaynum" HeaderText="回复" /> <asp:BoundField DataField="readnum" HeaderText="阅读" /> <asp:BoundField DataField="title" HeaderText="题目" /> <asp:BoundField DataField="username" HeaderText="作者" /> <asp:BoundField DataField="classname" HeaderText="类别" /> <asp:BoundField DataField="time" HeaderText="发表时间" /> <asp:CommandField HeaderText="管理" ShowDeleteButton="True" /> </Columns> </asp:GridView> </div> <div id="message" runat="server"> <asp:HiddenField ID="classValue" runat="server" /> </div> </div> </asp:Content> 为什么提示我有不是已知元素
06-05
<think>我们正在解决ASP.NET页面中出现“未知元素”错误的问题。根据引用资料,我们可以分析可能的原因:1.引用[1]提到在ASP.NET中使用Ajax调用[WebMethod]时可能因方法定义问题报错,但与本问题关联不大。2.引用[2]指出在IIS中,如果某些文件类型(如.tmp)没有设置MIME类型,会导致IIS返回未知文件类型错误。虽然该错误不是直接关于页面元素,但给我们一个思路:可能是配置问题导致某些元素不被识别。3.引用[3]和[4]分别针对请求超时和权限问题,与当前问题关系不大。根据经验,ASP.NET页面出现“未知元素”错误通常有以下原因:-页面中使用了未注册的服务器控件(可能是自定义控件或第三方控件),或者未在页面中通过`<%@Register%>`指令注册。-在Web.config文件中没有正确配置控件所在的命名空间。-使用了较新版本的ASP.NET控件,但页面声明中使用的版本较低。参考引用[2]中关于IIS配置的思路,我们还可以考虑:-IIS或ASP.NET运行时未能正确解析页面中的标记,可能是由于配置问题。解决方案步骤:1.**检查控件注册**:如果在页面中使用了自定义控件或第三方控件,确保在页面顶部使用`<%@Register%>`指令进行注册。例如:```aspx<%@RegisterTagPrefix="myPrefix"Namespace="MyNamespace"Assembly="MyAssembly"%>```如果控件属于ASP.NET内置控件但出现在较新的版本中,请检查页面顶部的`<%@Page%>`指令中的`targetFramework`属性是否与项目目标框架一致。2.**检查Web.config配置**:在Web.config的`<pages>`节点下的`<controls>`部分注册控件。例如:```xml<system.web><pages><controls><addtagPrefix="asp"namespace="System.Web.UI.WebControls"assembly="System.Web.Extensions,Version=4.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35"/><!--其他控件注册--></controls></pages></system.web>```注意:对于ASP.NET内置控件,通常不需要手动注册,但如果错误指向内置控件(如`asp:ScriptManager`),则可能是配置缺失或版本问题。3.**检查页面声明**:确保页面顶部的`<%@Page%>`指令中的属性正确,特别是`validateRequest`和`enableEventValidation`等。有时,关闭验证可以暂时解决问题,但要注意安全风险(不推荐)。另外,检查页面中是否有错误的标记嵌套或未正确关闭的标签。4.**检查IIS配置**:参考引用[2],确保IIS中相关扩展名(如.aspx)的MIME类型已设置。通常,.aspx扩展名默认会映射到ASP.NET运行时。如果没有,请手动添加:扩展名:.aspxMIME类型:text/html但实际上,.aspx的映射应该是由处理程序模块处理的,而不是简单的MIME类型。因此,重点应检查处理程序映射:-打开IIS管理器,选择网站,找到“处理程序映射”功能。-确保存在针对.aspx的映射,例如:`PageHandlerFactory-Integrated`(集成模式)或`PageHandlerFactory-ISAPI-4.0_32/64bit`(经典模式)。5.**清理和重新生成项目**:有时,错误可能是由于编译问题导致的。尝试清理解决方案并重新生成。6.**检查.NET框架版本**:确保IIS中网站应用程序池的.NET框架版本与项目目标框架一致。7.**查看具体错误信息**:错误信息通常会指出未知元素的具体名称。例如,如果错误是“未知元素'asp:ScriptManager'”,则可能是缺少对ASP.NETAJAX控件的注册。在ASP.NETWebForms中,ScriptManager控件需要安装AJAX控件工具包或在Web.config中注册。对于ASP.NET4.0及更高版本,通常内置支持,但需要检查配置。8.**考虑第三方控件**:如果使用了第三方控件,请确保在项目中引用了正确的程序集,并且注册指令中的程序集名称与引用一致。根据以上步骤,大多数未知元素错误可以解决。如果问题仍然存在,请检查具体的错误信息和页面代码,以便进一步定位。相关问题:1.如何正确注册ASP.NET自定义控件?2.在ASP.NETWebForms中,如何处理页面验证错误?3.IIS中如何处理ASP.NET页面请求?4.ASP.NETWebForms中常见的配置错误有哪些?5.如何确保IIS应用程序池与ASP.NET项目的目标框架一致?</think> ### ASP.NET 页面“未知元素”错误解决方案 #### 错误原因分析 当 ASP.NET 页面出现“未知元素”错误时,通常由以下原因导致: 1. **命名空间未正确配置**:自定义控件或第三方控件未在 `web.config` 中声明命名空间 [^2][^4] 2. **控件注册缺失**:未在页面中通过 `<%@ Register %>` 指令注册控件 3. **程序集未引用**:相关 DLL 文件未正确添加到项目引用 4. **IIS 配置问题**:MIME类型或处理程序映射缺失(如 `.tmp` 文件未配置)[^2] 5. **框架版本不匹配**:控件支持的 .NET 框架版本与项目不一致 #### 解决方案步骤 1. **检查控件注册**(页面级) 在 `.aspx` 文件顶部添加注册指令: ```aspnet <%@ Register TagPrefix="前缀" Namespace="控件命名空间" Assembly="程序集名称" %> ``` 示例: ```aspnet <%@ Register TagPrefix="asp" Namespace="System.Web.UI.WebControls" Assembly="System.Web" %> ``` 2. **配置 web.config**(全局方案) ```xml <configuration> <system.web> <pages> <controls> <add tagPrefix="前缀" namespace="控件命名空间" assembly="程序集名称"/> </controls> </pages> </system.web> </configuration> ``` 注意:若使用第三方控件,需确认 `assembly` 名称与实际 DLL 一致 [^4] 3. **验证 IIS 设置** - 打开 IIS 管理器 → 选择站点 → MIME 类型 - 添加扩展名:`.tmp` → MIME类型:`text/plain` [^2] - 检查处理程序映射:确保 `.aspx` 映射到 `aspnet_isapi.dll` 4. **检查程序集引用** - 解决方案资源管理器 → 右键引用 → 添加引用 - 浏览到控件的 DLL 文件并添加 - 检查生成路径是否包含该 DLL 5. **框架版本一致性** - 项目属性 → 应用程序 → 确保目标框架与控件要求一致 - 若需升级框架: ```xml <system.web> <compilation targetFramework="4.8" /> </system.web> ``` 6. **清理临时文件**(补充方案) - 删除 `%WINDIR%\Microsoft.NET\Framework\版本号\Temporary ASP.NET Files` - 重启 IIS:运行 `iisreset` #### 注意事项 1. 若使用 AJAX 控件(如 `<asp:ScriptManager>`),需额外安装 [ASP.NET AJAX 扩展包](https://www.nuget.org/packages/AspNet.ScriptManager/) 2. 部署后出现错误时,检查服务器是否安装匹配的 .NET Framework 版本 3. 对于权限问题(如拒绝访问),参考引用[4]方法添加 IIS_IUSRS 权限 > 提示:完整解决方案需结合具体错误提示元素名称定位问题。例如错误 `Element 'asp:CustomControl' is not known`,需确认 `CustomControl` 是否来自 `System.Web.Extensions` 等程序集[^1][^3]。 --- ### 相关问题 1. 如何解决 ASP.NET 自定义控件的设计时错误? 2. IIS 部署 ASP.NET 应用出现"无法识别的配置节"错误如何修复? 3. ASP.NET Web Forms 中如何正确注册用户控件? 4. 为什么修改 `web.config` 后需要重启应用池才能生效? 5. ASP.NET 项目迁移到高版本框架需要注意哪些兼容性问题?
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值