I’m trying to subclass a GridView that is located in a UserControl. I want to be able to handle the events in a separate page as a result of this.
Basically I have the code as follows:
My UserControl with a 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 that uses the UserControl would be 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>
It’s code behind:
uc_list.GridView.DataSource = this.TCW.Copy.bypassReasons;
uc_list.GridView.DataBind();
To make this page work, I include this file which sets which columns are data bound, 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 be able to have the OnRowCommand, OnRowDelete, and all event handlers in a separate file instead of being in the CodeBehind of the UserControl. How can I go about making this work?
I tried making them as virtual classes and overriding them on the pages I use them on, but that did not 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 = " ";
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 added to the .cs file (tried many variations, but this is the current)
namespace UCS_Web.uP.UserControls
{
public class MyStdList : StdList
{
protected override void _gridView_RowCommand(object sender, GridViewCommandEventArgs e){
Response.Redirect("HERPA DERP!");
}
}
}
Your user control is
partial classright?See C#’s partial keyword:
It could look like this
To override your methods in a subclass, the base class
StdListneeds to havevirtualmethods and/or properties.See C#’s virtual keyword:
And…