Subclassing UserControlled GridView

I am trying to subclass a GridView that is in a UserControl. I want to be able to handle events on a separate page as a result of this.

I basically have the code as follows:

My UserControl with GridView:

<%@ Control Language="C#" AutoEventWireup="false" CodeBehind="StdList.ascx.cs" Inherits="UCS_Web.uP.UserControls.StdList" %> <div> <asp:Panel ID="Panel1" runat="server"> <asp:GridView ID="_gridView" runat="server" PageSize="6" GridLines="None" AutoGenerateColumns="False" OnRowCommand="_gridView_RowCommand" AutoGenerateEditButton="false" OnDataBound="_gridView_DataBound" OnPreRender="_gridView_PreRender"> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" CssClass="gridViewHdr" /> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> </asp:GridView> </asp:Panel> 

A page using UserControl will look like this:

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="BypassReasonsPage.aspx.cs" Inherits="UCS_Web.uP.Tools.BypassReasonsPage" %> <%@ Register Src="~/uP/UserControls/StdList.ascx" TagName="List" TagPrefix="uc" %> <body> <form id="form1" runat="server"> <div> <uc:List ID="uc_list" runat="server" /> </div> </form> 

Code by code:

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

In order for this page to work, I include this file, which sets which columns are bound to the data, etc .:

 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 }; } } 

I want to have OnRowCommand, OnRowDelete and all event handlers in a separate file, and not in the CodeBehind UserControl. How can I do this work?

I tried to make them virtual classes and override them on the pages where I use them, but that didn't work. Any other methods that could make this work?

EDIT: 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 <= _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 = "&nbsp;"; 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 } } } 

EDIT: my new override function has been added to the .cs file (tried many options, but this is current)

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

}

+4
source share
1 answer

The partial class user control is right?

See the C # partial keyword:

You can split the definition of a class or structure, interface, or method into two or more source files. Each source file contains a type section or method definition, and all parts are combined when the application is compiled.

It might look like this:

 // 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... } } 

To override your methods in a subclass, the base class StdList must have methods and / or virtual properties.

See the C # virtual keyword:

A virtual keyword is used to modify a method, property, indexer, or event declaration and resolution so that it is overridden in a derived class. For example, this method can be overridden by any class that inherits it:

  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 = "&nbsp;"; 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 } } 

A...

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

All Articles