。net新手问题
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
public partial class test : System.Web.UI.Page{
Hashtable ht;
protected void Page_Load(object sender, EventArgs e){
if (!Page.IsPostBack){
ht = new Hashtable();
ViewState["ht"] = ht;
}
}
protected void Button_Click(object sender, EventArgs e){
ht= (Hashtable)ViewState["ht"];
foreach (ListItem i in ListBox1.Items) {
if (i.Selected == true){
ListBox2.Items.Add(i.Text);
ht.Add(i.Text.ToString(), i.Value.ToString());
}
}
int[] indexToDel = ListBox1.GetSelectedIndices(); //取得所有选取值的index集合
int cnt = indexToDel.Length;
for (int i = cnt - 1; i >= 0; i--) {
ListBox1.Items.RemoveAt(indexToDel[i]);
}
}
protected void Button2_Click(object sender, EventArgs e){
ht = (Hashtable)ViewState["ht"];
int[] indexToDel = ListBox2.GetSelectedIndices(); //取得所有选取值的index集合
int cnt = indexToDel.Length;
foreach(ListItem i in ListBox2.Items){
if (i.Selected == true) {
ht.Remove(i.Text.ToString());}
}
for (int i = cnt - 1; i >= 0; i--){
ListBox2.Items.RemoveAt(indexToDel[i]);
}
}
protected void Button3_Click(object sender, EventArgs e){
ht = (Hashtable)ViewState["ht"];
Application["ht"] = ht;
Response.Redirect("right_top.aspx");
Response.Write("<script language=javascript>") ;
Response.Write("window.returnValue='false';window.close()") ;
Response.Write("</script>");
// Response.Write(" <script language=javascript>window.open('right_top.aspx');window.returnValue='True';window.close();</script>");
}
}
当我不把ht放在ViewState["ht"] 中,当我点击每个按钮时为什么ht又实例化一次?如果不用ViewState怎么解决?请高手帮我解决一下,为什么会出现这个问题?我是新手有点迷茫
------解决方案--------------------
定义一个全局变量接收啊