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,
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!"); } }
}