2009-07-26 120 views
1

我正在使用哈希表來存儲鍵值對,並且我在方法CreateDropDownLists()中初始化此哈希表(ddl_ht)。但是,當我在我的SelectedIndexChanged方法中檢查「currentItem」的值時,此值爲null。雖然我在監視窗口中選中的C#哈希表不保留值

(串)ddl_ht [鍵[1]

值,並將其顯示值(NOT 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; 
     } 

    } 
} 

回答

1

你不應該將哈希表保存您的網頁類的靜態字段,因爲它會在會話之間共享,因爲在ASP.NET中的靜態變量的生命週期是在AppDomain的生活中,考慮將其存儲在ViewState中:

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

如果創建了該代碼,可能應該將其更改爲存儲哈希表。 – sisve 2009-07-26 05:30:34

3

您將在此設置中遇到各種線程問題。你的散列表是靜態的,並且你的網站上的每一個命中將會在一個新的線程上創建你的類的一個新實例,這個新線程將嘗試訪問相同的散列表 - 並且由於每個頁面的新命中最初將調用CreateDropDownLists,你的散列表將會對每個新用戶重新初始化到頁面。