OnBubbleEvent冒泡事件奇怪的问题
今天没事试着写写控件,没想到碰到一个奇怪问题
public class myListView : CompositeDataBoundControl,INamingContainer { private int editIndex = -1; [Category("Default"), DefaultValue(-1)] public int EditIndex { get { return editIndex; } set { editIndex = value; } } //public int EditIndex //{ // get // { // if (ViewState["EditIndex"] == null) // { // return -1; // } // return (int)ViewState["EditIndex"]; // } // set { ViewState["EditIndex"] = value; } //} #region 模板 private ITemplate itemTemplate; [TemplateContainer(typeof(myListViewItem)), PersistenceMode(PersistenceMode.InnerProperty)] public ITemplate ItemTemplate { get { return itemTemplate; } set { itemTemplate = value; } } private ITemplate editTemplate; [TemplateContainer(typeof(myListViewItem), BindingDirection.TwoWay), PersistenceMode(PersistenceMode.InnerProperty)] public ITemplate EditTemplate { get { return editTemplate; } set { editTemplate = value; } } #endregion protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding) { int index = 0; if (dataBinding) { } if (ItemTemplate != null) { Controls.Clear(); foreach (object dataItem in dataSource) { myListViewItem myItem = new myListViewItem(dataItem, index, index); if (EditIndex == index) { if (EditTemplate != null) { EditTemplate.InstantiateIn(myItem); } } else { ItemTemplate.InstantiateIn(myItem); } index++; Controls.Add(myItem); } } DataBind(false); ChildControlsCreated = true; return index; } protected override HtmlTextWriterTag TagKey { get { return HtmlTextWriterTag.Ul; } } protected override bool OnBubbleEvent(object source, EventArgs args) { bool handle = false; if (args is CommandEventArgs) { CommandEventArgs myargs = args as CommandEventArgs; switch (myargs.CommandName.ToUpper()) { case "EDIT": EditIndex = int.Parse( myargs.CommandArgument.ToString()); RequiresDataBinding = true; handle=true; break; case "UPDATE": break; case "CANCEL": EditIndex = -1; RequiresDataBinding = true; handle = true; break; default: break; } } return handle; } } [ToolboxItem(false)] public class myListViewItem:WebControl,INamingContainer,IDataItemContainer { private object dataItem; private int dataIndex; private int index; public myListViewItem() { } public myListViewItem(object dataItem, int dataIndex, int index) { this.dataItem = dataItem; this.dataIndex = dataIndex; this.index = index; } #region IDataItemContainer 成员 public object DataItem { get { return dataItem; } } public int DataItemIndex { get { return dataIndex; } } public int DisplayIndex { get { return index; } } #endregion protected override HtmlTextWriterTag TagKey { get { return HtmlTextWriterTag.Li; } } protected override bool OnBubbleEvent(object source, EventArgs args) { if (source is IButtonControl) { CommandEventArgs e = args as CommandEventArgs; if (e != null) { CommandEventArgs myargs = new CommandEventArgs(e.CommandName, this.dataIndex.ToString()); base.RaiseBubbleEvent(this, myargs); return true; } } return false; } } 源代码: <lv:myListView ID="myList" runat="server" DataSourceID="SqlDataSource1"> <ItemTemplate> uid: <asp:Label ID="uidLabel" runat="server" Text='<%# Eval("uid") %>'></asp:Label><br /> username: <asp:Label ID="usernameLabel" runat="server" Text='<%# Bind("username") %>'></asp:Label><br /> nickname: <asp:Label ID="nicknameLabel" runat="server" Text='<%# Bind("nickname") %>'></asp:Label><br /> <asp:LinkButton ID="EditButton" runat="server" CommandName="Edit" Text="编辑"> </asp:LinkButton> </ItemTemplate> <EditTemplate> uid: <asp:Label ID="uidLabel1" runat="server" Text='<%# Eval("uid") %>'></asp:Label><br /> username: <asp:TextBox ID="usernameTextBox" runat="server" Text='<%# Bind("username") %>'></asp:TextBox><br /> nickname: <asp:TextBox ID="nicknameTextBox" runat="server" Text='<%# Bind("nickname") %>'></asp:TextBox><br /> <asp:Button ID="aa" runat="server" Text="update" CommandName="update"/> <asp:Button ID="bb" runat="server" Text="cancel" CommandName="cancel"/> </EditTemplate> </lv:myListView>
当没有EditIndex属性没有加viewstate时,cancel和update都不会调用OnBubbleEvent事件,
EditIndex属性加了viewstate时,cancel和update会调用OnBubbleEvent事件
EditIndex属性加了viewstate时,cancel和update会调用OnBubbleEvent事件