【问题标题】:retrieving values in array & store in session and calling to other page asp.net c#检索数组中的值并存储在会话中并调用另一个页面asp.net c#
【发布时间】:2011-04-06 06:57:51
【问题描述】:

嗨 我想在会话中将值存储在数组中。我想将它用于另一个页面。我怎么能这样做? 我使用 session 来存储数组的值如下:

    int i, randno;
    int[] a = new int[5];
        for ( i = 0; i < 4; i++)
        {
            int flag = 0;
            Random rnd = new Random();
            randno = rnd.Next(1, 15);
            for (int j = 0; j < i; j++)
            {
                if (a[j] == randno)
                    flag = 1;
            }
            if (flag == 0)
            {
                a[i] = randno;

            }
            else
            {
                i--;
            }

        }
Session["values"] = a;

在其他页面我使用了代码:

int[] a = (int[])Session["values"];
Response.Write(a);

这段代码对吗? 因为它没有给出价值。 但是检索到另一个页面然后它给出数组的最后一个值,在同一页面上它给出所有值。我想要数组的所有值。 ASP.NET, C# 谢谢你。

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    试试这个

    存储数组的所有项。

    string[] a = new string[]{"a","b","c"};
    Session["values"] = a;
    

    在下一页中,您可以像这样检索它。

    string[] a = (string[])Session["values"]
    

    【讨论】:

    • 我以同样的方式使用,但它给出了输出:System.Int32[]。未检索到任何值
    • 我没听懂你。你从哪里得到这个?什么是?
    • 哦,你修改了问题。您在进行 response.Write 时得到 System.Int32[]?这是因为您不能直接打印数组。你需要做一个循环并尝试。尝试这个。 foreach(int i in a){ Response.Write(i.ToString());}
    • 当我在 foreach 循环中使用 response.write 时,它​​仍然给出 System.Int32[]System.Int32[]System.Int32[]System.Int32[]System.Int32[]
    • @Anuraj 告诉我如何在函数中使用这些值。我有 getquestion(int no) 函数。我必须在该函数中使用这些数组值代替 no 。他们有没有办法像索引一样做到这一点?
    【解决方案2】:

    您可以将整个数组存储到会话中:

    Session["values"] = a;
    

    在另一页上:

    SomeType[] a = Session["values"] as SomeType[];
    // Use the array here
    

    【讨论】:

    • 我用过,但它给出了输出:System.Int32[]
    • 我就是这样做的。我发布了我的 code.values 仍然没有给出输出:System.Int32[]
    【解决方案3】:

    你面临的问题在最后一行代码中:

    Response.Write(a);
    

    就像你说的那样

    int[].ToString() 
    

    通常情况下,输出将与您现在一样。

    我的做法是将数据存储为字符串数组,然后您可以像这样表示它:

    Response.Write(string.Join(", ", a));
    

    【讨论】:

      【解决方案4】:

      你可以把它放在一个类中,像这样:

      公开课 MyGlobals

      私有共享 _QuizArray 作为新的 ArrayList

      公共属性 theArray As ArrayList

      获取

      Return _theArray
      

      结束获取

      Set(ByVal value As ArrayList)

      _theArray = value
      

      结束集

      结束属性

      结束类

      然后像这样从各个页面访问它:

      设置值:QuizArray.Add(index)

      获取值:i = QuizArray(index)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-15
        • 1970-01-01
        • 1970-01-01
        • 2023-03-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多