我最近看了
this article on smart use of ViewState
我特别感兴趣的是在viewstate中没有不必要的静态数据。但是,我也很好奇是否可以为父子下拉列表做同样的事情,比如经典的Country/CountrySubdivision示例。
所以我有了这个标记:
<asp:DropDownList runat="server" ID="ddlCountry" DataTextField="Name" DataValueField="ID" EnableViewState="false" />
<asp:DropDownList runat="server" ID="ddlCountrySubdivision" DataTextField="Name" DataValueField="ID" EnableViewState="false" />
<asp:Button runat="server" ID="btnTest" />
这背后的代码是:
public class Country
{
public string Name { get; set;}
public int Id { get; set; }
}
public class CountrySubdivision
{
public string Name { get; set; }
public int Id { get; set; }
public int CountryId { get; set; }
}
protected override void OnInit(EventArgs e)
{
var l = new List<Country>();
l.Add(new Country { Name = "A", Id = 1 });
l.Add(new Country { Name = "B", Id = 2 });
l.Add(new Country { Name = "C", Id = 3 });
ddlCountry.DataSource = l;
ddlCountry.DataBind();
var l2 = new List<CountrySubdivision>();
l2.Add(new CountrySubdivision { Name = "A1", Id = 1, CountryId = 1 });
l2.Add(new CountrySubdivision { Name = "A2", Id = 2, CountryId = 1 });
l2.Add(new CountrySubdivision { Name = "B1", Id = 4, CountryId = 2 });
l2.Add(new CountrySubdivision { Name = "B2", Id = 5, CountryId = 2 });
l2.Add(new CountrySubdivision { Name = "C1", Id = 7, CountryId = 3 });
l2.Add(new CountrySubdivision { Name = "C2", Id = 8, CountryId = 3 });
// this does not work: always comes out 1 regardless of what's actually selected
var selectedCountryId = string.IsNullOrEmpty(ddlCountry.SelectedValue) ? 1 : Int32.Parse(ddlCountry.SelectedValue);
// this does work:
var selectedCountryIdFromFormValues = Request.Form["ddlCountry"];
ddlCountrySubdivision.DataSource = l2.Where(x => x.CountryId == selectedCountryId).ToList();
ddlCountrySubdivision.DataBind();
base.OnInit(e);
}
所以我注意到的第一件事就是
EnableViewstate
是
false
,我的国家/地区控制的价值在所有请求中都是持久的,不需要额外的努力。太好了。这是很多系列化的东西,我不需要在提交表单时跨线发送。
然后我用一对父子下拉列表来举上面的例子,我看到了
ddlCountry.SelectedValue
违约,而
Request.Form["ddlCountry"]
正在反映控件的值。
有办法保持
EnableViewState = "false"
不诉诸
Request.Form
要获取依赖控件的值?