make the Hashtable a member of your Form class
class YourForm : Form
{
   Hashtable hstable = new Hashtable();
...void buttonclick(...) 
{
 Random r=new Random();
 double d=r.NextDouble();
 int i=(int)(d*1220);
 hstable.Add(i.ToString(),r.NextDouble().ToString());
}

解决方案 »

  1.   

    还是不行的!还是一条一条的!!我的代码是:
    public class p21 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.DataList MyDataList;
    protected Hashtable hstable = new Hashtable();
    …………
    private void Button1_Click(object sender, System.EventArgs e)
    {
    Random r=new Random();
    double d=r.NextDouble();
    int i=(int)(d*2000);
    hstable.Add(i.ToString(),r.NextDouble().ToString());
    MyDataList.DataSource = hstable;
    MyDataList.DataBind();
    }
    …………
    }
    是这样吗?这该怎么做?
      

  2.   

    用ViewState来保存Hashtable。
    在Page_Load中加入:
    if (ViewState["Hashtable"] != ull)
    {
      hstable = (Hashtable)ViewState["Hashtable"];
    }
    在Button1_Click的最后加上:
    ViewState["Hashtable"] = hstable;
      

  3.   

    以上方法是可以在同一个页面保存数据项的,如果要在第一次单击后就跳转到另一个页面,当再回来到这个页面再单击这个按钮的时候,上一项已不存在了,要使第一个值在页面Redirect()后还存在,这该如何操作?
      

  4.   

    怪怪!!!第一次单击是一项,从第二次开始第次单击增加两项  :@(
    看:
    …………
    protected Hashtable myhstable =new Hashtable();
    void Page_Load(object sender, System.EventArgs e){
    if (Session["Hashtable"] != null)
    {
    myhstable = (Hashtable)Session["Hashtable"];
    }}
    ……
    void Button1_Click(……){
    string reqid=Request.QueryString["cr_id"].ToString();
    Random r=new Random();
    double d=r.NextDouble();
    int i=(int)(d*2000);
    myhstable.Add(i.ToString(),reqid);
    Session["Hashtable"] = myhstable;
    DataList1.DataBind();
    Response.Redirect("WebForm1.aspx");}
    ??
      

  5.   

    你的处理流程是什么?
    在Button1_Click中转向WebForm1.aspx,那在WebForm1.aspx中的处理呢?
      

  6.   

    是这样的:单击WebForm1.aspx中的一个链接(如链接为:second.aspx?cr_id=7)(cr_id是数据库中的一个字段),然后转向second.aspx,此页中的一个DataList绑定在此Hashtable上。在Second.aspx中有一个按钮,点击一次,向hstable中插入一条记录,然后再次转向WebForm1.aspx
    …………