子类化用户控制的 GridView

2024-01-03

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

基本上我的代码如下:

我的带有 GridView 的 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>

使用 UserControl 的页面将如下所示:

<%@ 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>

它的背后代码:

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

为了使此页面正常工作,我包含此文件,该文件设置哪些列是数据绑定的等:

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

编辑:我的新覆盖函数添加到 .cs 文件中(尝试了很多变体,但这是当前的)

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

}


您的用户控件是partial class right?

请参阅 C# 的partial http://msdn.microsoft.com/en-us/library/wa80x488.aspx关键词:

可以将类或结构、接口或方法的定义拆分为两个或多个 更多源文件。每个源文件包含一个类型或的部分 方法定义,并且在应用程序时将所有部分组合起来 编译。

它可能看起来像这样

// 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 http://msdn.microsoft.com/en-us/library/9fkccyh4.aspx关键词:

virtual 关键字用于修改方法、属性、索引器或事件声明并允许 以便在派生类中重写它。例如,这个方法 可以被继承它的任何类覆盖:

  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
      } 
  }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

子类化用户控制的 GridView 的相关文章

随机推荐