本文介绍了取消选中checkboxList中的项目,该复选框可通过querystring onLoad选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个checkboxList,它的autopostback为true。我已经做到了,在SelectedIndexChanged上,它通过querystrting重定向到同一页面。查询字符串值将与选定项一起生成。像这样的东西
www.abcd.com/product?price=2000|3000|5000

I have checkboxList which has autopostback true. I have made it in such way in which, on SelectedIndexChanged it gets redirected to same page with querystrting. Querystring value gets generated with selected items. Something like thiswww.abcd.com/product?price=2000|3000|5000

因此,当页面加载时,Checkboxlist项目会在其值所在的位置被选中是2000,3000,5000等。但是这里的缺点是,当我取消选中任何项目然后重新开始时,它将执行pageLoad事件代码&在那里找到未选中&的值再次被选中。简而言之,再次选中未选中的项目。

So when page gets load the Checkboxlist items gets selected where its value is 2000,3000,5000 etc. But here I have drawback is that when I uncheck any item then agin first it executes pageLoad event code & there it finds value which is uncheck & gets selected again. In short unchecked items gets selected again.

PageLoadevent(复选框项目使用查询字符串值进行选择)

PageLoadevent(Checkbox Items gets selected with querystring values)

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(使用querystring创建的URL & redirected)

SelectedIndexChanged(URL created with querystring & redirected)

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


推荐答案

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条件。如上。页面回发后,您的选择将保持不变。

Enclose logic of checkbox selection into IsPostBack condition in page load event. As above. When your page get postback your selection remain as it.

这篇关于取消选中checkboxList中的项目,该复选框可通过querystring onLoad选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 12:43