我有一個<asp:CheckBoxList>
在我的頁面中使用RepeatLayout="Flow"
我動態地分配值。如果第一次加載頁面(!IsPostBack
),則渲染後的版本與此類似;防止CheckBoxList的新呈現
<span children="Cat">
<input id="ctl00_Menu_Category_0" type="checkbox" name="ctl00$Menu$Category$0"/>
<label for="ctl00_Menu_Category_0">Cat</label>
</span>
<br/>
<span class="Cat">
<input id="ctl00_Menu_Category_1" type="checkbox" name="ctl00$Menu$Category$1"/>
<label for="ctl00_Menu_Category_1"> - SubCat1</label>
</span>
children
是我使用的一個jQuery代碼的屬性,所以,當還檢查用戶檢查Cat
,所有SubCat
秒。
jQuery代碼搜索所有<span>
,它們的類的屬性等於children
-attribute,所以我需要維護jQuery工作的這種結構。
但是,我重新加載頁面或點擊一個鏈接,什麼的,名單突然看起來像這樣經過:
<input id="ctl00_Menu_Category_0" type="checkbox" name="ctl00$Menu$Category$0"/>
<label for="ctl00_Menu_Category_0">Cat</label>
<br/>
<input id="ctl00_Menu_Category_1" type="checkbox" name="ctl00$Menu$Category$1"/>
<label for="ctl00_Menu_Category_1"> - SubCat1</label>
這怎麼可能呢?我只將值賦給列表一次,那麼爲什麼在PostBack之後重新渲染,我怎麼能阻止它這樣做呢?
編輯
下面是創建列表中的代碼;
// Get all available categories that are not a child of another category
DataTable categoryParents = functions.SelectSql(Resources.Data.GetCategoryParents);
// Get the child categories
foreach (DataRow parent in categoryParents.Rows)
{
// Add the category
ListItem parentItem = new ListItem(parent["Name"].ToString(), parent["Name"].ToString());
parentItem.Attributes.Add("children", parent["Name"].ToString().Replace(' ', '_'));
Category.Items.Add(parentItem);
// For every parent category, get all its child categories
DataTable categoryChildren = functions.SelectSql(Resources.Data.GetCategoryChildrenByParent.Replace("##PARENTID##", parent["ID"].ToString()));
// Add the child categories after their parents
foreach (DataRow child in categoryChildren.Rows)
{
ListItem item = new ListItem(" - " + child["Name"].ToString(), parent["Name"].ToString() + "\\" + child["Name"].ToString());
item.Attributes.Add("class", parent["Name"].ToString().Replace(' ', '_'));
Category.Items.Add(item);
}
}
Category.DataBind();
jQuery本身不會對HTML做任何事情,它只是在檢查父代時檢查子類別的功能;
$("#Category :checkbox").click(function(){
var checked = $(this).attr("checked");
var children = $(this).parent("span").attr("children");
$("#Category ." + children + " :checkbox").attr("checked", checked);
});
jQuery根本沒有操作,請參閱我的編輯。 – 2009-12-10 10:57:13
好吧,我害怕我沒有一個簡單而真棒的解決方案,但看到我的編輯 – 2009-12-10 13:06:23
好吧,解決方法似乎工作;我設置了'EnableViewState =「false」'現在它每次都正確呈現......但是在重新加載之後刪除選中的框: -/ – 2009-12-10 14:29:10