C#中泛型的下拉列表框中的数据绑定问题

时间:2010-03-23 11:24:54

标签: c# .net-3.5

我想通过Genericx在我的程序中实现堆栈。我有一个文本框和按钮,用于在堆栈中添加元素,一个下拉列表框和一个用于在下拉列表框中绑定总堆栈的按钮。

我有通用类,代码如下:

[Serializable]
public class myGenClass<T>
{
    private T[] _elements;
    private int _pointer;

    public myGenClass(int size)
    {
         _elements = new T[size];
        _pointer = 0;


    }

    public void Push(T item)
    {
        if (_pointer > _elements.Length - 1)
        {
            throw new Exception("Stack is full");

        }
        _elements[_pointer] = item;
        _pointer++;
    }

    public T Pop()
    {
        _pointer--;
        if (_pointer < 0)
        {

            throw new Exception("Stack is empty");
        }

        return _elements[_pointer];
    }

    public T[] myBind()
    {


        T[] showall = new T[_pointer];
        Array.Copy(_elements,showall, _pointer);
        T[] newarray = showall;
        Array.Reverse(showall);

        return showall;


    }

}

我的.cs页面如下:

public partial class _Default : System.Web.UI.Page
{
    myGenClass<int> mystack = new myGenClass<int>(25);


         protected void Button1_Click(object sender, EventArgs e)
    {
        mystack.Push(Int32.Parse(TextBox1.Text));
        //DropDownList1.Items.Add(mystack.Pop().ToString());
        TextBox1.Text = string.Empty;
        TextBox1.Focus();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        //string[] db;
        //db = Array.ConvertAll<int, string>(mystack.myBind(), Convert.ToString);
        DropDownList1.DataSource = mystack.myBind();
        DropDownList1.DataBind();
    }


}

但是当我将dropdownlist框的datasource属性绑定到泛型类型返回数组(即myBind())时,它显示为空...请帮助..

1 个答案:

答案 0 :(得分:0)

我没有看过myGenClass类型,但是你是否在请求之间保留了myGenClass的实例?每次单击按钮都会发布到_default类的新实例,该实例使用空堆栈进行初始化。如果请求之间没有持久化堆栈,则它将包含其默认值。