取消选中checkboxList中的项目,该项目通过查询字符串onLoad进行选择

时间:2017-06-07 15:27:28

标签: c# asp.net checkbox

我有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);
}

1 个答案:

答案 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条件中。如上。当您的页面获得回发时,您的选择将保持原样。