Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 子类化用户控制的GridView_C#_Asp.net_Gridview_User Controls - Fatal编程技术网

C# 子类化用户控制的GridView

C# 子类化用户控制的GridView,c#,asp.net,gridview,user-controls,C#,Asp.net,Gridview,User Controls,我正在尝试对位于UserControl中的GridView进行子类化。因此,我希望能够在单独的页面中处理事件 基本上我有如下代码: 带有GridView的My UserControl: <%@ Control Language="C#" AutoEventWireup="false" CodeBehind="StdList.ascx.cs" Inherits="UCS_Web.uP.UserControls.StdList" %> <div> <asp:Panel

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

基本上我有如下代码:

带有GridView的My UserControl:

<%@ 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>
为了使此页面正常工作,我添加了此文件,其中设置了哪些列是数据绑定的,等等:

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的代码隐藏中。我怎样才能让这一切顺利进行

我尝试将它们作为虚拟类,并在我使用它们的页面上覆盖它们,但这不起作用。有没有其他方法可以让这一切顺利

编辑:用户控制代码隐藏

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

}您的用户控件是
部分类
对吗

参见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关键字用于修改方法、属性、索引器或事件声明并允许 使其在派生类中被重写。例如,这个方法 可以被继承它的任何类重写:


因此,我将这个部分类添加到驱动.aspx页面的.cs文件中。这段代码不能直接工作,但我尝试了类似于_gridView.RowCommand+=\u gridView\u RowCommand的方法,它说它不明确,但如果我使用唯一的名称,它就永远不会运行。你能举一个与上述类似的事件的例子吗?是的,我误解了你的问题,我猜:p。。更新我的答案!我从来没有那么擅长用措辞提问。但这看起来很棒!这正是我想要的。它还不起作用,但我会处理它一段时间,我希望能成功。谢谢你的帮助!有些东西仍然不起作用。我已经设置了覆盖函数和用于覆盖的分部类,但它们仍然从未运行过!旧的虚拟函数在我看来无法运行的情况下运行:(有什么想法吗?编辑你的答案,并向我们展示你如何在网页上下文中实际使用你的子类。如果有人觉得这个答案很难理解,请检查
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
      }
  }
}
namespace UCS_Web.uP.UserControls
{
public class MyStdList : StdList
{
    protected override void _gridView_RowCommand(object sender, GridViewCommandEventArgs e){
        Response.Redirect("HERPA DERP!");
    }
}
// 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...
    }
}
  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
      } 
  }
  namespace UCS_Web.uP.UserControls
  {
      public partial class SpecialStdList : StdList { }

      protected override void _gridView_RowCommand(object sender, GridViewCommandEventArgs e)
      {
          // I do very nice stuff
      } 
  }