我有以下全局列表 -
public partial class About : System.Web.UI.Page
{
List<string> changes = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<string> changes = new List<string>();
}
我正在尝试将字符串值添加到changes
这样 -
protected void CheckBox1_CheckedChanged(Object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
DataListItem item = (DataListItem)chk.NamingContainer;
TextBox txt = (TextBox)DataList1.Items[item.ItemIndex].FindControl("aliasTextBox");
string text = txt.Text;
changes.Add(text);
ViewState["array"] = changes;
}
所以我试图将 dataList 中所做的所有更改存储到changes
,以便当用户单击时 -
protected void Button5_Click(object sender, EventArgs e)
{
List<string> changes = (List<string>)ViewState["array"];
foreach (string text in changes)
{
WebService1 ws = new WebService1();
ws.WebMethod(text);
}
}
但是,当单击该按钮时,我在 -changes.add(text) 部分得到一个空引用异常。如何存储更改中的所有值以在单击按钮时可用?