问题
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 this www.abcd.com/product?price=2000|3000|5000
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(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(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);
}
回答1:
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;
}
}
}
Enclose logic of checkbox selection into IsPostBack condition in page load event. As above. When your page get postback your selection remain as it.
来源:https://stackoverflow.com/questions/44416897/uncheck-item-from-checkboxlist-which-gets-selected-with-querystring-onload