为什么我的自定义模板数据库控件中的Dataitem在itemCommand事件中为null



这是我的自定义控制代码:

namespace MyControlNameSpace  
{  
  public class MyControlItemCommandEventArgs : CommandEventArgs
  {
    private MyControlItem _item;
    private object _commandSource;
    public MyControlItemCommandEventArgs(MyControlItem item, object commandSource, CommandEventArgs cea) : base(cea)
     {
      _item = item;
      _commandSource = commandSource;   
     }
    public MyControlItem Item { get{ return _item; } }
    public object CommandSource { get{ return _commandSource; } }
  }
  public class MyControlItem : Control, IDataItemContainer
  {
    public MyControlItem(object dataItem, int index)
    {
        _dataItem = dataItem;
        _dataItemIndex = _displayIndex = index;
    }  
    private readonly object _dataItem;
    private readonly int _dataItemIndex;
    private readonly int _displayIndex;
    public object DataItem { get { return _dataItem; } }
    public int DataItemIndex { get { return _dataItemIndex; } }
    public int DisplayIndex { get { return _displayIndex; } }
    protected override bool OnBubbleEvent(object source, EventArgs args)
    {
      if (args is CommandEventArgs)
      {
       var e = new MyControlItemCommandEventArgs(this, source, (CommandEventArgs)args);
       base.RaiseBubbleEvent(this, e);
       return true;
      }
      return false;
    }
  }  
  public delegate void ItemEventHandler(object sender, MyControlItemCommandEventArgs e);
  [DefaultProperty("Text")]
  [ToolboxData("<{0}:MyControl runat=server></{0}:MyControl>")]
  public class MyControl : CompositeDataBoundControl, IPrivilage
  {
    public event ItemEventHandler ItemCommand;
    protected virtual void OnItemCommand(MyControlItemCommandEventArgs e)
    {
     if (ItemCommand != null)
        ItemCommand(this, e);
    }
    protected override bool OnBubbleEvent(object source, EventArgs args)
    {
        // only bother bubbling appropriate events
        if (args is MyControlItemCommandEventArgs)
        {
            OnItemCommand((MyControlItemCommandEventArgs)args);
            return true;
        }
        return false;
    }
    protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)
    {
       // here I'm adding some controls like buttons and other
       // Iterate for ItemTemplate
       foreach (object dataItem in dataSource)
       {
         if (ItemTemplate != null)
         {
           // create instance of MyControlItem control
           var item = new MyControlItem(dataItem, count++);
           // instantiate in new item object
           ItemTemplate.InstantiateIn(item);
           // add item to Controls collection
           Controls.Add(item);
           // need to support <%# %> expressions
           item.DataBind();
         }
       }
      // some code here
    }
  }

在.aspx页面:

protected void Page_Load(object sender, EventArgs e)
 {
   // if it's not post back then get the data and assign to MyControl DataSource property
   if(!IsPostBack)
     GetData();
 }    
protected void MyControl1_OnItemCommand(object sender, MyControlItemCommandEventArgs e)
 {
   var x = e.DataItem;  
   // x here is always null;
 }

您可以看到,我在onItemCommand事件时无法获得e.dataitem,因为它始终是null。
我试图在Page_Load中删除if(!IsPostBack),以便每次分配页面加载数据源,但这是不对的,如果像ASP.NET Repeater CONTROR中的PostBack一样,我应该能够分配数据。

任何想法如何做?

OnItemCommand事件在后背包上提出。到那时,如果CompositeDataBoundControl启用了ViewState,则它将在不查询数据库的情况下重新创建子女控件。您的CreateChildControls方法将使用dataBinding设置为false,并且dataSource将是null对象的枚举序列,可作为实际数据的占位符。

您将在任何数据结合的控件中获得相同的行为。如果您使用Repeater尝试同一件事,您会发现e.Item.DataItemItemCommand事件中的null

相关内容

  • 没有找到相关文章

最新更新