我有checkboxList,其autopostback为true。我以这样的方式制作它,在SelectedIndexChanged上,它通过querystrting重定向到同一页面。使用所选项生成查询字符串值。像这样的东西 www.abcd.com/product?price=2000|3000|5000
因此,当页面加载时,Checkboxlist项目会被选中,其值为2000,3000,5000等。但这里我有一个缺点是,当我取消选中任何项目然后agin首先执行pageLoad事件代码&它找到了取消选中的价值&再次被选中。简而言之,未经检查的项目会再次被选中。
PageLoadevent(使用查询字符串值选中复选框项目)
string PageUrl = Request.Url.AbsolutePath;
if (PageUrl.Contains("price")) {
string price = Request.QueryString("price");
string[] priceList = price.Split('|');
foreach (string p in priceList) {
if (priceRange.Items.FindByValue(p + "|") != null) {
priceRange.Items.FindByValue(p + "|").Selected = true;
}
}
}
SelectedIndexChanged(使用查询字符串&重定向创建的URL)
string pageURL = Request.Url.AbsoluteUri;
string strPrice = Request.QueryString("price").ToString;
if (totalcount > 1) {
foreach (ListItem chk in brandsList.Items) {
if (chk.Selected == true) {
selectedBrands += (chk.Value);
}
}
selectedBrands = selectedBrands.Remove(selectedBrands.Length - 1);
Response.Redirect((Request.Url.AbsolutePath + "?") + "&brand=" + selectedBrands + "&price=" + strPrice);
}
答案 0 :(得分:0)
if (!IsPostBack)
{
string price = Request.QueryString["price"];
string[] priceList = price.Split('|');
foreach (string p in priceList)
{
if (chkList.Items.FindByText(p) != null)
{
chkList.Items.FindByText(p).Selected = true;
}
}
}
在页面加载事件中将复选框选择逻辑包含在IsPostBack条件中。如上。当您的页面获得回发时,您的选择将保持原样。