如何使用ASP.NET将mysql数据库中的数据显示到HTML表中

编程入门 行业动态 更新时间:2024-10-26 06:34:29
本文介绍了如何使用ASP.NET将mysql数据库中的数据显示到HTML表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

I had a product_list table from mysql database named: boyscout_pos. I want to display the data from the table into a html table type. For some reason that I don''t like to display it into datagridview and it is better designing table using bootstrap.

我尝试过的事情: 我没有代码尝试解决此问题,因为在进行了一些研究之后,从mysql数据库查看数据的所有样式都使用了datagridview.我没有找到引用.

What I have tried: There is no code I try about this problem because after doing some research, all of the style in viewing the data from mysql database are using datagridview. I didn''t found references.

推荐答案

您可以使用转发器来构建几乎任何想要的html,包括表格.绑定的方式会有所不同,但这是针对DataTable的. You can use a repeater to build pretty much any html you want, including a table. How this works will differ depending on what you''re binding to it, but this is for a DataTable <asp:Repeater runat="server" ID="MyRepeater"> <HeaderTemplate> <table> <tr> <th>ID</th> <th>Name</th> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td> <%# Eval("ID") %> </td> <td> <%# Eval("Name") %> </td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater>

背后的代码

Code behind

DataTable data = new DataTable(); data.Columns.Add("ID", typeof(int)); data.Columns.Add("Name", typeof(string)); data.Rows.Add(1, "John"); data.Rows.Add(2, "Dave"); MyRepeater.DataSource = data; MyRepeater.DataBind();

使用GridView并设置DataSource属性. 在此处查看示例:分步进行将MySQL数据绑定到ASP.NET GridView [ ^ ] 另一种方法是使用PHP: HTML页面中的MySQL数据库 [ ^ ] javascript-如何使用jQuery运行MySQL查询? -堆栈溢出 [ ^ ] Use a GridView and set the DataSource property. See example here: Step By Step Bind MySQL Data To ASP.NET GridView[^] Another way is to use PHP: MySQL database in HTML page[^] javascript - How can I use jQuery to run MySQL queries? - Stack Overflow[^]

如果只需要基本的HTML输出,则Repeater很好.但是,如果要支持对结果进行分页,对结果进行排序,内联编辑或其他各种高级"功能,则必须跳过几个步骤. 这就是为什么我倾向于使用 ListView控件 [ ^ ]: The Repeater is fine if you just want basic HTML output. But if you want to support paging the results, sorting the results, in-line editing, or various other "advanced" features, you''ll have to jump through a few hoops. That''s why I tend to prefer the ListView control[^]: <asp:ListView runat="server" ID="MyListView"> <LayoutTemplate> <table class="table table-striped table-bordered"> <thead> <tr> <th>ID</th> <th>Name</th> </tr> </thead> <tbody> <tr id="itemPlaceholder" runat="server" /> </tbody> </table> </LayoutTemplate> <ItemTemplate> <tr> <td> <%# Eval("ID") %> </td> <td> <%# Eval("Name") %> </td> </tr> </ItemTemplate> </asp:ListView>

更多推荐

如何使用ASP.NET将mysql数据库中的数据显示到HTML表中

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

发布评论

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

>www.elefans.com

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