Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6238087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:08:52+00:00 2026-05-24T11:08:52+00:00

I’m trying to subclass a GridView that is located in a UserControl. I want

  • 0

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

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:08:53+00:00Added an answer on May 24, 2026 at 11:08 am

    Your user control is partial class right?

    See C#’s partial keyword:

    It is possible to split the definition of a class or a struct, an interface or a method over two or
    more source files. Each source file contains a section of the type or
    method definition, and all parts are combined when the application is
    compiled.

    It could 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 needs to have virtual methods and/or properties.

    See C#’s virtual keyword:

    The virtual keyword is used to modify a method, property, indexer, or event declaration and allow
    for it to be 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
          } 
      }
    

    And…

      namespace UCS_Web.uP.UserControls
      {
          public partial class SpecialStdList : StdList { }
    
          protected override void _gridView_RowCommand(object sender, GridViewCommandEventArgs e)
          {
              // I do very nice stuff
          } 
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.