【问题标题】:Display contents of arraylist in aspx page在aspx页面显示arraylist的内容
【发布时间】:2015-10-08 14:14:34
【问题描述】:

我想在.aspx 页面中显示arraylist 的内容。

我试过了……

public string jl;
public ArrayList jline = new ArrayList();
public string jli { get { return jline.ToString(); } }

protected void Page_Load(object sender, EventArgs e)
{
    ProductClick();
}

public void ProductClick()
{
    string list_name;
    string list_clicks;
    try
    {
        cn.con.Open();
        cn.cmd.Connection = cn.con;
        cn.cmd.CommandText = "Select * From user_product Inner Join latest_upload On latest_upload.ProductGUID = user_product.ProductGUID where user_product.UserGUID='" + Session["LogedUserGUID"] + "' AND Approve='" + 1 + "' ";
        dr = cn.cmd.ExecuteReader();
        while (dr.Read())
        {
            list_name = dr["Name"].ToString();
            list_clicks = dr["Count"].ToString();

            jl = "{ y: '" + list_name + "', a: '" + list_clicks + "' },";
            jline.Add(jl.ToString());

        }
        dr.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        cn.con.Close();
    }
}

现在显示.aspx页面中的内容...

<%= jli %>

我只想显示arraylist的内容,但是除了内容之外,它显示的是System.Collections.Arraylist。

我该怎么办?

【问题讨论】:

  • 出于兴趣,既然可以使用List&lt;string&gt;,为什么还要使用ArrayList?您使用的是 .NET 1 吗?
  • 因为你只是在做return jline.ToString();。这只是打印 ArrayList 的 ToString 值,这只是目前的类型名称。要正确返回 ArrayList 中的字符串值,您必须使用存储的所有值生成一个字符串。任何类型的循环和StringBuilder 都将有助于将 ArrayList 的内容生成为可用的字符串。

标签: c# asp.net arraylist


【解决方案1】:

因为您将整个数组转换为字符串,所以您必须一个一个地转换您的数组。 在你的 .aspx 页面中试试这个:

<%
foreach(var item in jline)
{
    Response.Write(item);
}
%>

【讨论】:

  • 为什么不改用&lt;%= item %&gt;?
  • 在我提到的方式中,您将一些 c# 代码添加到您的 .cshtml 页面中,而您想将 c# 代码添加到您的 .cshtml 中,您必须在 之间添加您的 c# 代码,在某些情况下如果您只想在 .cshtml 文件中编写一个 c# var,您可以编写诸如 之类的东西,但是当您想像您的问题一样处理您的 var 或列表时,您必须像我回答的那样编写您的列表子项。
  • 我不是提出这个问题的人。我试图让您解释为什么选择Response.Write 而不是&lt;%= item %&gt; 语法。顺便说一句,没有 CSHTML。这是 Web 表单,而不是 Razor。
  • 打扰一下@mason,我认为 OP 在第一条评论中提出问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-14
  • 1970-01-01
  • 1970-01-01
  • 2018-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多