1

CompositeDataBoundControl から継承するカスタム サーバー コントロールがあります。ヘッダー テンプレート、フッター テンプレート、アイテム テンプレートの 3 つのテンプレートがあります。項目テンプレートには、項目を削除するかどうかを決定するために使用するチェックボックスを含めることができます。

フッターおよび/またはヘッダー テンプレートには、CommandName が「DeleteItem」のボタンがあります。そのボタンがクリックされると、OnBubbleEvent でイベントを処理します。

if (cea.CommandName == "DeleteItem") {
    //loop through the item list and get the selected rows
    List<int> itemsToDelete = new List<int>();
    foreach(Control c in this.Controls){
        if (c is ItemData) {
            ItemData oid = (ItemData)c;
            CheckBox chkSel = (CheckBox)oid.FindControl("chkSelected");
            if (chkSel.Checked) {
                itemsToDelete.Add(oid.Item.Id);
            }
        }                        
    }
    foreach (int id in itemsToDelete) {
        DeleteItem(id);
    }
  }
}

問題は、イベントが発生する前にasp.netがコントロール階層を再作成する必要があるため、CreateChildControlsメソッドがすでに実行されているため、Itemがnullであることです。DummyDataSource と null オブジェクトのリストを使用して、コントロール階層を再作成します。

IEnumerator e = dataSource.GetEnumerator();
if (e != null) {
while (e.MoveNext()) {
    ItemData container = new ItemData (e.Current as OrderItem);
    ITemplate itemTemplate = this.ItemTemplate;
    if (itemTemplate == null) {
        itemTemplate = new DefaultItemTemplate();
    }
    itemTemplate.InstantiateIn(container);
    Controls.Add(container);
    if (dataBinding) {
        container.DataBind();
    }
    counter++;
}

}

問題は次の行です: ItemData container = new ItemData (e.Current as OrderItem); イベントが発生する前にコントロール階層が再構築されると、e.Current が null になるため、削除対象としてマークされた項目を見つけようとすると、元の値が上書きされているため 0 が返されます。

これを修正する方法について何か提案はありますか?

4

1 に答える 1

0

I've finally found a solution that works. The problem is that the bound data is only connected to the control when being bound and directly after(normally accessed in a ItemDataBound event).

So to solve it I had to add a hidden literal containing the data item id to the container control. In the OnBubbleEvent I find the hidden literal and get the id:

ItemData oid = (ItemData)c;
CheckBox chkSel = (CheckBox)oid.FindControl("chkSelected");
if(chkSel != null) {
      if(chkSel.Checked) {
         Literal litId = (Literal)oid.FindControl("litId");
         itemsToDelete.Add(Utils.GetIntegerOnly(litId.Text));
      }
}
于 2010-02-07T18:22:52.163 に答える