对UserControlled GridView进行子类化

我正在尝试子类化位于UserControl中的GridView。 我希望能够在单独的页面中处理事件。

基本上我的代码如下:

我的UserControl与GridView:

 

使用UserControl的页面如下所示:

    

这是代码背后:

 uc_list.GridView.DataSource = this.TCW.Copy.bypassReasons; uc_list.GridView.DataBind(); 

为了使这个页面工作,我包括这个文件,它设置哪些列是数据绑定,等等:

 public class BypassReasonsByToolTable : UCS_Web.uP.UserControls.StdList.ICustomTable { public DataControlField[] Columns { get { BoundField col1 = new BoundField(); col1.DataField = "Code"; col1.HeaderText = "Code"; col1.SortExpression = "Code"; col1.ItemStyle.Width = new Unit(50, UnitType.Percentage); BoundField col2 = new BoundField(); col2.DataField = "Text"; col2.HeaderText = "Text"; col2.SortExpression = "Text"; col2.ItemStyle.Width = new Unit(50, UnitType.Percentage); TemplateField editReason = new TemplateField(); editReason.ItemTemplate = new addTemplate(); return new DataControlField[] { col1, col2, editReason }; } } 

我希望能够将OnRowCommand,OnRowDelete和所有事件处理程序放在一个单独的文件中,而不是在UserControl的CodeBehind中。 我怎样才能做到这一点?

我尝试将它们作为虚拟类,并在我使用它们的页面上覆盖它们,但这不起作用。 任何其他可以使这项工作的方法?

编辑:UserControl CodeBehind

 namespace UCS_Web.uP.UserControls { public partial class StdList : UserControl { private ICustomTable m_custom = null; protected void _gridView_DataBound(object sender, EventArgs e) { if (_gridView.Rows.Count > 0) { for (int i = _gridView.Rows.Count + 1; i  0) ? datacontrolrowstate.normal : datacontrolrowstate.alternate); DataControlRowState.Alternate); foreach (DataControlField field in _gridView.Columns) { TableCell cell = new TableCell(); cell.Text = " "; row.Cells.Add(cell); } //row.Attributes.Add("OnClick", "javascript:alert();"); row.BackColor = System.Drawing.ColorTranslator.FromHtml("#ffffff"); _gridView.Controls[0].Controls.AddAt(i, row); } } } protected void _gridView_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Delete") { //DO MY DELETE STUFF FOR THIS SPECIFIC PAGE } } } 

编辑:我的新覆盖function添加到.cs文件(尝试了很多变化,但这是当前的)

 namespace UCS_Web.uP.UserControls { public class MyStdList : StdList { protected override void _gridView_RowCommand(object sender, GridViewCommandEventArgs e){ Response.Redirect("HERPA DERP!"); } } 

}

你的用户控件是partial class吗?

请参阅C#的部分关键字:

可以在两个或多个源文件上拆分类或结构,接口或方法的定义。 每个源文件都包含类型或方法定义的一部分,并且在编译应用程序时将所有部分组合在一起。

它可能看起来像这样

 // MyOtherFile.cs: namespace MyWebSite.UserControls { public partial class MyUserControl : System.Web.UI.UserControl { protected override void OnInit(System.EventArgs e) { base.OnInit(e); _gridView.OnRowCommand += _gridBiew_RowCommand; _gridView.OnDataBound += _gridView_DataBound; } // events here... } } 

要覆盖子类中的方法,基类StdList需要具有virtual方法和/或属性。

请参阅C#的虚拟关键字:

virtual关键字用于修改方法,属性,索引器或事件声明,并允许在派生类中重写它。 例如,任何inheritance它的类都可以覆盖此方法:

  namespace UCS_Web.uP.UserControls { public partial class StdList : UserControl { private ICustomTable m_custom = null; } protected virtual void _gridView_DataBound(object sender, EventArgs e) { if (_gridView.Rows.Count > 0) { for (int i = _gridView.Rows.Count + 1; i <= _gridView.PageSize - 1; i++) { GridViewRow row = new GridViewRow( 0, 0, DataControlRowType.DataRow, //(i % 2 > 0) ? datacontrolrowstate.normal : datacontrolrowstate.alternate); DataControlRowState.Alternate); foreach (DataControlField field in _gridView.Columns) { TableCell cell = new TableCell(); cell.Text = " "; row.Cells.Add(cell); } //row.Attributes.Add("OnClick", "javascript:alert();"); row.BackColor = System.Drawing.ColorTranslator.FromHtml("#ffffff"); _gridView.Controls[0].Controls.AddAt(i, row); } } } protected virtual void _gridView_RowCommand(object sender, GridViewCommandEventArgs e) { // I do nothing for now... A subclass could override me and do very nice stuff } } 

和…

  namespace UCS_Web.uP.UserControls { public partial class SpecialStdList : StdList { } protected override void _gridView_RowCommand(object sender, GridViewCommandEventArgs e) { // I do very nice stuff } }