C#Hashtable不保留值

时间:2009-07-26 05:03:56

标签: c# asp.net hashtable

我使用哈希表存储键值对,并在方法CreateDropDownLists()中初始化此哈希表(ddl_ht)。但是,当我在SelectedIndexChanged方法中检查“currentItem”的值时,此值为null。即使我检查了

的值
  

(字符串)ddl_ht [键[1]]

在我的Watch窗口中,它显示一个值(不为null)。你明白为什么这个currentItem是空的吗?

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ADONET_namespace;

namespace AddFileToSQL
{
    public partial class DataMatch : _Default
    {
        protected System.Web.UI.WebControls.PlaceHolder phTextBoxes;
        protected System.Web.UI.WebControls.PlaceHolder phDropDownLists;
        protected System.Web.UI.WebControls.Button btnAnotherRequest;
        protected System.Web.UI.WebControls.Panel pnlCreateData;
        protected System.Web.UI.WebControls.Literal lTextData;
        protected System.Web.UI.WebControls.Panel pnlDisplayData;

        protected static string inputfile2;
        static string[] headers = null;
        static string[] data = null;
        static string[] data2 = null;
        static DataTable myInputFile = new DataTable("MyInputFile");
        static string[] myUserSelections;
        static Hashtable ddl_ht = new Hashtable();

        // Page Load 
        private void Page_Load(object sender, System.EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.NumberOfControls = 0;
            }
        }

        // Add DropDownList Control to Placeholder
        private void CreateDropDownLists()
        {
            for (int counter = 0; counter < NumberOfControls; counter++)
            {
                DropDownList ddl = new DropDownList();
                SqlDataReader dr = ADONET_methods.DisplayTableColumns(targettable);
                ddl.ID = "DropDownListID " + (counter + 1).ToString();
                ddl.DataTextField = "COLUMN_NAME";
                ddl.DataValueField = "COLUMN_NAME";
                ddl.DataSource = dr;
                ddl.DataBind();

                //myUserSelections[counter] = "";

                ddl.AutoPostBack = true;
                ddl.EnableViewState = true; //Preserves View State info on Postbacks
                ddl.Style["position"] = "absolute";
                ddl.Style["top"] = 100 * counter + 80 + "px";
                ddl.Style["left"] = 250 + "px";
                ddl.SelectedIndexChanged += new EventHandler(SelectedIndexChanged);
                ddl_ht.Add(counter, ddl.SelectedValue);

                pnlDisplayData.Controls.Add(ddl);
                pnlDisplayData.Controls.Add(new LiteralControl("<br><br><br>"));
                pnlDisplayData.Visible = true;
                pnlDisplayData.FindControl(ddl.ID);
               // pnlDropDownList.FindControl(ddl.ID);
                dr.Close();
            }
        }

        protected void SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender; 
            string[] value=(ddl.SelectedValue).Split(' ');
            string[] key = ddl.ID.Split(' ');
            string currentItem=(string)ddl_ht[key[1]];
            //if (String.IsNullOrEmpty(currentItem))
            //{
            //    ddl_ht.Add(key[1], value[0]);
            //}
            if (currentItem != ddl.SelectedValue)
            {
                ddl_ht.Remove(key[1]);
                ddl_ht.Add(key[1], ddl.SelectedValue);
            }
        }

        // Add TextBoxes Control to Placeholder
        private void RecreateDropDownLists()
        {
            for (int counter = 0; counter < NumberOfControls; counter++)
            {
                DropDownList ddl = new DropDownList();
                SqlDataReader dr = ADONET_methods.DisplayTableColumns(targettable);

                ddl.ID = "DropDownListID " + (counter + 1).ToString();
                ddl.DataTextField = "COLUMN_NAME";
                ddl.DataValueField = "COLUMN_NAME";
                ddl.DataSource = dr;
                ddl.DataBind();
                myUserSelections[counter] = "";
                dr.Close();

                ddl.AutoPostBack = true;
                ddl.EnableViewState = true; //Preserves View State info on Postbacks
                ddl.Style["position"] = "absolute";
                ddl.Style["top"] = 100 * counter + 80 + "px";
                ddl.Style["left"] = 250 + "px";
                ddl.SelectedIndexChanged += new EventHandler(SelectedIndexChanged);
                pnlDisplayData.Controls.Add(ddl);
                pnlDisplayData.Controls.Add(new LiteralControl("<br><br><br>"));
            }
        }

        // Create TextBoxes and DropDownList data here on postback.
        protected override void CreateChildControls()
        {
            // create the child controls if the server control does not contains child controls
            this.EnsureChildControls();

            // Creates a new ControlCollection. 
            this.CreateControlCollection();

            // Here we are recreating controls to persist the ViewState on every post back
            if (Page.IsPostBack)
            {
                RecreateDropDownLists();
                RecreateLabels();
            }
            // Create these conrols when asp.net page is created
            else
            {
                PopulateFileInputTable();
                CreateDropDownLists();
                CreateLabels();
            }

            // Prevent child controls from being created again.
            this.ChildControlsCreated = true;
        }

    }
}

2 个答案:

答案 0 :(得分:3)

您将在此设置中遇到各种线程问题。您的哈希表是静态的,并且您网站上的每个匹配都将在一个新线程上创建一个新的类实例,该新线程将尝试访问相同的哈希表 - 并且由于每次点击该页面最初都会调用CreateDropDownLists ,您的哈希表将针对页面的每个新用户重新初始化。

答案 1 :(得分:1)

您不应将HashTable存储为页面类的静态字段,因为它将在会话之间共享,因为ASP.NET中静态变量的生命周期在appdomain的生命周期内,请考虑将其存储在ViewState:

private Hashtable ddl_ht
{
    get 
    {
       return ViewState["ddl_ht"] as HashTable;
    }
    set
    {
       ViewState["ddl_ht"] = value;
    }
}