【问题标题】:transform SQL data into html table with asp用asp将SQL数据转换成html表
【发布时间】:2013-05-05 12:13:33
【问题描述】:

我的数据库中有未知数量的行,其中包含 id 和 text 列。 在我的代码中,我将此数据转换为带有参数 id 和 text 的对象新闻列表。

HTML

<div class="middleDiv" id="mid" runat="server">
</div>

代码:

List<news> news = loadNewsFroamDbs();
String html = "";
for (int i=0;i<news.Count; i++) {
String html1 = " <div class='big'>" +
                        "<div class='id'>" +
                            "<label>" + news.id + "</label>" +
                            "<label>id</label>" +
                        "</div>" +
                        "<div class='text'>" +
                            "<a href='newsDetail.aspx?id=" + news.id + "'>"
                            + news.text + "</a>" +
                        "</div></div>" 
html = html + html1;
mid.InnerHtml = html;
}

当我想在我的页面上启动我的 delete() 函数时,我应该使用什么组件?我不能使用 button+onClick 方法。我必须学习一些有关控制器的知识吗?

【问题讨论】:

  • 您是否有理由不使用像 GridView 这样内置了 Delete 和 Update 等方法的东西?
  • GridView 只是表。这段代码是示例,实际上我有 10 个属性而不是 2 个,我想要特定的格式,我可以用 div 做。
  • RazorEngine 怎么样?
  • 如果我是对的,RazorEngine 是不是有点像 MVC?一方面我必须重新设计我的所有项目,另一方面这个框架给了我更多的选择......我仍然正确吗?
  • @user2270693 是的,它在 MVC 中使用,但您可以在项目中引用该单个 DLL 并按照其主页所示使用它。它是一个模板引擎。您甚至可以在控制台应用程序中使用它。

标签: c# html asp.net sql


【解决方案1】:

我同意这里的每个人的观点,您应该使用 GridView,现在看一下这个快速示例以了解如何使用它,您几乎可以将表格格式化为您想要的任何方式(而您实际上并没有自己输入所有代码 VS 会为你做很多事情)

.ASPX

<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
            GridLines="None">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
                    ShowSelectButton="True" />
            </Columns>
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>

.CS

public class News
    {
        public int id { get; set; }
        public string title { get; set; }
        public string content { get; set; }
        //as many properties as you want
    }
    public partial class bindGridviewToList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            News n1 = new News() { id = 1, content = "content", title = "title" };
            News n2 = new News() { id = 2, content = "content", title = "title" };
            News n3 = new News() { id = 3, content = "content", title = "title" };

            List<News> newsList = new List<News>();

            newsList.Add(n1); newsList.Add(n2); newsList.Add(n3);

            GridView1.DataSource = newsList;
            GridView1.DataBind();
        }
    }

运行此示例,您将看到使用 GridView 是多么容易

【讨论】:

    【解决方案2】:

    梅兰妮是对的。如果要使用 HTML 表格元素,则应使用 GridView

    如果您不打算使用表格,请使用DataList。这可能就是你想要的。如果这仍然不适合您,请查找您可以使用的其他数据控件(查看 DataList 页面上的左侧窗格)。

    编辑:为什么不能使用按钮进行删除?我没有理由不这样做,AFAIK 所有数据控件都允许您使用按钮进行创建、删除和编辑。

    【讨论】:

      猜你喜欢
      • 2014-06-09
      • 2014-01-21
      • 1970-01-01
      • 1970-01-01
      • 2021-05-06
      • 2017-04-01
      • 1970-01-01
      • 2023-03-28
      • 2015-12-05
      相关资源
      最近更新 更多