在asp.net中创建一个表

时间:2012-11-20 17:04:23

标签: html asp.net html-table insert

我在代码隐藏中编写html表标记时遇到问题。

我有以下代码:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;

public partial class Default4 : System.Web.UI.Page
{int i;
    public string strAdmin;
    public string strPass;
    public string strLname;
    public string strEmail;
    public string strFname;
    public string str;
    public string dynTable = "";  

    protected void Page_Load(object sender, EventArgs e)
    {

        string dbPath = Server.MapPath(@"App_Data") + "/my_site.mdb";
        string connectionString = @"Data Source='" + dbPath + "';Provider='Microsoft.Jet.OLEDB.4.0';";
        OleDbConnection con = new OleDbConnection(connectionString);
        con.Open();
        string QueryString = "SELECT * FROM tbl_users";
        OleDbCommand cmd = new OleDbCommand(QueryString, con);
        OleDbDataAdapter da = new OleDbDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds, "tbl");
        con.Close();

        for (i = 0; i < ds.Tables[0].Rows.Count; i++)
        {dynTable += "<table  border=\"1\">";  

            dynTable += "<tr>";  


            strFname = ds.Tables[0].Rows[i]["first_name"].ToString();
            strLname = ds.Tables[0].Rows[i]["last_name"].ToString();
            strEmail = ds.Tables[0].Rows[i]["user_email"].ToString();
            strPass = ds.Tables[0].Rows[i]["user_password"].ToString();
            strAdmin = ds.Tables[0].Rows[i]["is_admin"].ToString();
            str += String.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td></tr>", strFname, strLname, strEmail, strPass, strAdmin);
            dynTable += "</tr>";  
dynTable += "</table>";
        }


    }
}

问题是标签不起作用(我应该将数据放在表格中)。你能帮帮我解决这个问题吗?

3 个答案:

答案 0 :(得分:2)

删除额外的<TR>,不要为每行创建新的<table>

    dynTable += "<table  border=\"1\">";  
    for (i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
        strFname = ds.Tables[0].Rows[i]["first_name"].ToString();
        strLname = ds.Tables[0].Rows[i]["last_name"].ToString();
        strEmail = ds.Tables[0].Rows[i]["user_email"].ToString();
        strPass = ds.Tables[0].Rows[i]["user_password"].ToString();
        strAdmin = ds.Tables[0].Rows[i]["is_admin"].ToString();
        dynTable += String.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td></tr>", strFname, strLname, strEmail, strPass, strAdmin);

    }
    dynTable += "</table>";

此外,不是将文字添加到str变量,而是将其添加到dynTable

答案 1 :(得分:1)

您正在增加<tr>

 dynTable += "<table  border=\"1\">";
 for (i = 0; i < ds.Tables[0].Rows.Count; i++)
            {  

                //dynTable += "<tr>";  


                strFname = ds.Tables[0].Rows[i]["first_name"].ToString();
                strLname = ds.Tables[0].Rows[i]["last_name"].ToString();
                strEmail = ds.Tables[0].Rows[i]["user_email"].ToString();
                strPass = ds.Tables[0].Rows[i]["user_password"].ToString();
                strAdmin = ds.Tables[0].Rows[i]["is_admin"].ToString();
                str += String.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td></tr>", strFname, strLname, strEmail, strPass, strAdmin);
                //dynTable += "</tr>"; 

                //your str variable already has each row with a <tr></tr> tags.
                dynTable += str;

            }

 dynTable += "</table>";

答案 2 :(得分:1)

使用ListView控件可以使这更容易,因为您可以将其定义为:

<asp:ListVIew .. ItemPlaceholderID="Items">
   <LayoutTemplate>
       <table border="1">
           <tr runat="server" id="Items" />
       </table>
   </LayoutTemplate>
   <ItemTemplate>
     <tr>
<td>
   <# Eval("FirstName") #>
</td>
<td>
   <# Eval("LastName") #>
</td>
.
.
</tr>
   </ItemTemplate>
</asp:ListView>

然后你可以这样做:

ListView1.DataSource = //some data source
ListView1.DataBind()

进一步的参考文献:

http://weblogs.asp.net/scottgu/archive/2007/08/10/the-asp-listview-control-part-1-building-a-product-listing-page-with-clean-css-ui.aspx http://msdn.microsoft.com/en-us/library/bb398790(V = VS.100)的.aspx