整了个晚上,搜遍大江南北居然没答案.
偶然看到ViewState这个单词,想起我是在Web.Config中禁用了ViewState的,在之前是能获得ListBox的值的.于是该页面page指令上加上EnableViewState="true" 就中了.难道ListBox的值和字是放在ViewState里的.
然后试了下只设置该控件的ViewState=true结果是不行的,必须在页面上加.
测试代码如下
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
for(int i=1; i<10;i++)
{
ListBox1.Items.Add(new ListItem("aa"+i, "a"+i));
}
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
string result = "";
foreach(System.Web.UI.WebControls.ListItem li in ListBox1.Items)
{
if(li.Selected == true)
{
result += li.Value + ",";
}
}
Response.Write("<br>result="+result);
}
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
for(int i=1; i<10;i++)
{
ListBox1.Items.Add(new ListItem("aa"+i, "a"+i));
}
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
string result = "";
foreach(System.Web.UI.WebControls.ListItem li in ListBox1.Items)
{
if(li.Selected == true)
{
result += li.Value + ",";
}
}
Response.Write("<br>result="+result);
}