2009-12-10 65 views
0

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

回答

0

整個HTML代碼將在您每次重新加載頁面時始終呈現。你可以通過檢查IsPostBack來防止的是改變這次HTML是如何呈現的。這意味着當頁面回傳時,服務器不會將複選框列表與新值綁定,但會直接使用存儲在ViewState中的值直接按照上次的方式呈現它們。

如果你的jQuery代碼在渲染後改變了HTML,那麼服務器將會有不知道,並且真的沒有可行的方法來改變它。這裏有趣的問題是:包裝跨度和屬性以及所有代碼首先應用到哪裏?

你的選擇是執行下列操作之一:

  • 做一個AJAX請求,而不是一個完整的回傳,只改變要更改
  • 重新應用jQuery代碼的DOM的一部分該回發後實現這種變化,在domready中

EDIT

響應於第e編輯原始帖子:

Category您的CheckBoxList?如果你已經迭代地添加了所有列表項,那麼爲什麼你要重新綁定它,之後呢?我認爲第一個for循環應該使數據綁定過時。

這裏我最好猜測的是當CheckBoxList被序列化爲ViewState時,它將存儲應用於它的屬性以及列表項的id/value字典(而不是迭代應用的其他屬性)。

如果它沒有保存在ViewState中 - 它看起來好像不是 - 沒有真正乾淨的解決方案來解決您的問題。解決方法是在每個頁面加載上執行代碼,或者繼承子類中的CheckBoxList,該代理覆蓋Render方法,以始終生成所需的輸出。事實上,如果您在整個網站中都使用這個功能,最後一個選項可能會非常整齊。

+0

jQuery根本沒有操作,請參閱我的編輯。 – 2009-12-10 10:57:13

+0

好吧,我害怕我沒有一個簡單而真棒的解決方案,但看到我的編輯 – 2009-12-10 13:06:23

+0

好吧,解決方法似乎工作;我設置了'EnableViewState =「false」'現在它每次都正確呈現......但是在重新加載之後刪除選中的框: -/ – 2009-12-10 14:29:10

相關問題