日期:2014-05-18 浏览次数:20468 次
public partial class WebForm1 : System.Web.UI.Page { string test; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DropDownList.Items.Add("test1"); DropDownList.Items.Add("test2"); test = "hello"; } } protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e) { Response.Write(test);//此时test为空值。。请问如何才能使得只初始化一次,而且又能让test持久化保存,如果我所需要持久化的变量不仅仅只是一个字符串变量,也可能是一个List<string>或者Dictionary类型,那么有哪些方法可以在服务器端只初始化一次,然后能够持久化保存呢? } }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DropDownList1.Items.Add("test1"); DropDownList1.Items.Add("test2"); test = "hello"; ViewState["value"] = test; } } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { if (ViewState["value"] != null) { Response.Write(ViewState["value"].ToString()); } else { Response.Write("null"); } }