简体   繁体   English

如何使用 C# 将“li”添加到“ul”标签

[英]How to add “li” to “ul” tag with C#

i am trying to get some names from Sql database and append them as "li" in to pre-created "ul" with the code below.我正在尝试从 Sql 数据库和 append 中获取一些名称,将它们作为“li”输入到预先创建的“ul”中,代码如下。 i am getting "System.Data.Common.DataRecordInternal" instead my datas in Sql what could be the problem?我得到“System.Data.Common.DataRecordInternal”而不是我在 Sql 中的数据可能是什么问题?

also with this code i cannot take first item in SQL.同样使用此代码,我无法在 SQL 中获取第一项。 i mean totaly i have 9 item in Sql however i get only 8 "System.Data.Common.DataRecordInternal".我的意思是我总共有 9 个项目在 Sql 但是我只有 8 个“System.Data.Common.DataRecordInternal”。

the code:编码:

HTML HTML

<div class="modal-body" style="width: 1000px;">
    <ul class="list-group" runat="server" id="A3List">
    </ul>
</div>

C# C#

protected void loadEval_ServerClick(object sender, EventArgs e)
{
    SqlCommand comd = new SqlCommand("select name from A3_Coaching", con.connection());
    SqlDataReader dr = comd.ExecuteReader();

    while (dr.Read())
    {
        foreach (var r in dr)
        {
            HtmlGenericControl li = new HtmlGenericControl("li");
            A3List.Controls.Add(li);
            li.InnerHtml = r.ToString();
        }
    }
}

You need to read the actual column name from the data reader not just do a.ToString which will print out the underlying type's name.您需要从数据读取器中读取实际的列名,而不仅仅是执行 a.ToString 来打印出基础类型的名称。 You don't need the extra foreach also, the while on the DataReader is your loop.您也不需要额外的 foreach,DataReader 上的 while 是您的循环。 Try the below.试试下面的。

protected void loadEval_ServerClick(object sender, EventArgs e)
{
    SqlCommand comd = new SqlCommand("select name from A3_Coaching", con.connection());
    SqlDataReader dr = comd.ExecuteReader();

    while (dr.Read())
    {
        HtmlGenericControl li = new HtmlGenericControl("li");
        A3List.Controls.Add(li);
        li.InnerHtml = dr["name"];        
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM