C# C:在页面中包含页面加载数据行

C# C:在页面中包含页面加载数据行,c#,html,asp.net,C#,Html,Asp.net,我已经做过从数据库中提取数据的研究,它工作得很好,但是,我目前正在努力将数据传递到页面上的元素中 我的页面加载函数如下所示: // Create Objects MySqlDataAdapter query; MySqlConnection db; int i = 0; protected void Page_Load(object sender, EventArgs e) { // Create DataSet to store Dat

我已经做过从数据库中提取数据的研究,它工作得很好,但是,我目前正在努力将数据传递到页面上的元素中

我的页面加载函数如下所示:

// Create Objects
    MySqlDataAdapter query;
    MySqlConnection db;
    int i = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        // Create DataSet to store Database Rows
        DataSet ds = new DataSet();

        // Attempt to connect the Database
        try {

            // Create a new connection to the Database
            db = new MySqlConnection(ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString);
            // Open the connection
            db.Open();
            // Create a new Query
            query = new MySqlDataAdapter("SELECT * FROM `test_table`", db);
            // Add Query results to the DataSet
            query.Fill(ds, "AllData");
            // Create a new DataTable to work from
            DataTable dt = new DataTable();
            // Append the rows to the DataTable
            dt = ds.Tables["AllData"];
            // Select and store needed information inside an DataRow array
            DataRow[] r = dt.Select("usergroup='1'");
            // Return all values
            Response.Write("<ul>");
            while(i != r.Length)
            {
                Response.Write("<li>" + r[i]["Name"] + "</li>");
                i++;
            }
            Response.Write("</ul>");

        }
        catch (Exception ex) {
            // Display connection error
            Response.Write("ERROR: " + ex);
        }
        finally {

            // Close the connection
            db.Close();

        }

    }
    <%@ Page Title="About Us" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="About.aspx.cs" Inherits="MyLearningSite.About" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        About
    </h2>
    <p>
        Take a look at all the people that chose us:
    </p>
    <ul>
    </ul>
</asp:Content>
我现在想得到回复。写下我在aspx页面上编码的ul,如下所示:

// Create Objects
    MySqlDataAdapter query;
    MySqlConnection db;
    int i = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        // Create DataSet to store Database Rows
        DataSet ds = new DataSet();

        // Attempt to connect the Database
        try {

            // Create a new connection to the Database
            db = new MySqlConnection(ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString);
            // Open the connection
            db.Open();
            // Create a new Query
            query = new MySqlDataAdapter("SELECT * FROM `test_table`", db);
            // Add Query results to the DataSet
            query.Fill(ds, "AllData");
            // Create a new DataTable to work from
            DataTable dt = new DataTable();
            // Append the rows to the DataTable
            dt = ds.Tables["AllData"];
            // Select and store needed information inside an DataRow array
            DataRow[] r = dt.Select("usergroup='1'");
            // Return all values
            Response.Write("<ul>");
            while(i != r.Length)
            {
                Response.Write("<li>" + r[i]["Name"] + "</li>");
                i++;
            }
            Response.Write("</ul>");

        }
        catch (Exception ex) {
            // Display connection error
            Response.Write("ERROR: " + ex);
        }
        finally {

            // Close the connection
            db.Close();

        }

    }
    <%@ Page Title="About Us" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="About.aspx.cs" Inherits="MyLearningSite.About" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        About
    </h2>
    <p>
        Take a look at all the people that chose us:
    </p>
    <ul>
    </ul>
</asp:Content>
有谁知道一种简单的方法可以编辑页面加载函数,使其自动进入ul,而不是在页面加载之前将其放入ul?

几乎没有理由使用Response.Write。除非你真的知道你在做什么,否则不要使用它

与其尝试写入页面,不如在页面上放置某种控件并修改该控件的属性

有无数种方法可以做到这一点,与数据绑定之类的方法相比,这无疑是一种草率的方法。但是,在当前将原始HTML输出到页面的思维模式中,这可能是与您所拥有的最接近的类比

如果在页面上放置类似文字控件的内容:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        About
    </h2>
    <p>
        Take a look at all the people that chose us:
    </p>
    <asp:Literal runat="server" ID="myLiteral" />
</asp:Content>
然后,您可以从代码隐藏设置该控件的内容:

var output = new StringBuilder();
output.Append("<ul>");
while(i != r.Length)
{
    output.Append("<li>" + r[i]["Name"] + "</li>");
    i++;
}
output.Append("</ul>");
myLiteral.Text = output.ToString();

注意:我已经有一段时间没有使用WebForms了,所以这是未经测试的代码。这真的是为了证明这个概念。使用控件,其中有许多控件可以很好地完成各种任务,它们可以作为页面中要显示内容的占位符。

。。。。你到底为什么要使用回应呢?你真的不想那样做。在页面上放置一个控件并修改该控件的属性。即使只是文字控件的文本。对于这一点,will现在将研究如何添加控件:谢谢。我更欣赏解释而不是代码。不管怎样,你用它击中了要害。非常感谢,我会考虑的。