【问题标题】:Using delegate with events将委托与事件一起使用
【发布时间】:2017-04-16 03:32:58
【问题描述】:

上下文:我必须创建 4 个页面,我必须为每个页面创建 2 个表。每个表都有相同的结构,唯一的变化是加载的数据。

我为另一个页面做了类似的事情,我必须在一个页面上做 6 个类似的表格。我在提供不同数据的函数上使用了委托,并将所需的函数传递给我的 TableInit 函数。它运作良好! :)

但现在我正在考虑做一个静态类,我将在其中放置所有表格生成器函数,以便能够将表格放在多个页面中,而无需复制粘贴所有函数。

问题是我在表格的某处有一个按钮,并且我修复了该按钮上 page.aspx 中的一个事件。当我将我的函数放在另一个静态类中时,我就是找不到传递事件的方法。

我在此处粘贴我的初始代码以及页面中的所有内容,并提出以下问题:如何将表格生成器函数与页面隔离?

namespace Pointage
{
public delegate List<M_FICHE> delOnGetRecent(M_USER_POINTAGE p_user);
public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        tableInit(this.recentFicheDataTableHead, this.recentFicheDataTableBody, M_FICHE.getFiveRecentUserFiche);
        tableInit(this.recentFicheNeededCorrectDataTableHead, this.recentFicheNeededCorrectDataTableBody, M_FICHE.getFiveRecentUserFicheWithNeededCorrections);
        tableInit(this.recentFicheNeedApprovalDataTableHead, this.recentFicheNeedApprovalDataTableBody, M_FICHE.getFiveRecentUserFicheWithNeededApproval);
        tableInit(this.recentFicheNeededValidDataTableHead, this.recentFicheNeededValidDataTableBody, M_FICHE.getFiveRecentUserFicheWithNeededValidation);
        tableInit(this.recentErrorFicheDataTableHead, this.recentErrorFicheDataTableBody, M_FICHE.getFiveRecentErrorFiche);
        langInit();
    }

    //procédure d'initialisation des différents label prenant compte de la langue
    private void langInit()
    {
        // Useless for this problem
    }

    /// <summary>   Table init. </summary>
    ///
    /// <remarks>   G 0669144, 26/10/2016. </remarks>
    ///
    /// <param name="p_head">           The head. </param>
    /// <param name="p_body">           The body. </param>
    /// <param name="p_delOnGetRecent"> The delete on get recent method from M_FICHE. </param>
    /// <param name="p_link">           The link. </param>

    private void tableInit(HtmlGenericControl p_head, HtmlGenericControl p_body, delOnGetRecent p_delOnGetRecent)
    {
        List<M_FICHE> _listFiche = p_delOnGetRecent(new M_USER_POINTAGE(Convert.ToInt32(Session["id"])));
        if (_listFiche.Count == 0)
        {
            p_head.Controls.Clear();
            HtmlTableRow _row = new HtmlTableRow();
            HtmlTableCell _cell = new HtmlTableCell();
            _cell.InnerText = Resource.NO_RECENT_FICHE;
            _row.Controls.Add(_cell);
            p_head.Controls.Add(_row);
        }
        else
        {
            // HIDED CODE : creating thead code
            p_body.Controls.Clear();
            foreach (M_FICHE fiche in _listFiche)
            {
                //Création de la ligne du tableau
                HtmlTableRow _row = new HtmlTableRow();
                //Création de chaque cellules
                HtmlTableCell cell = new HtmlTableCell();
                M_USER_POINTAGE _user = M_USER_POINTAGE.getUserFromSGID(fiche.USER_SGID);
                cell.InnerText = String.Format("{0}. {1}", _user.NAME[0], _user.FIRSTNAME);
                _row.Controls.Add(cell);
                //Cellule data
                HtmlTableCell cell2 = new HtmlTableCell();
                cell2.InnerText = fiche.DATE_CREATE.ToShortDateString();
                _row.Controls.Add(cell2);
                //Cellule status
                HtmlTableCell cell3 = new HtmlTableCell();
                cell3.InnerText = StatusManager.getRessource((STATUS)fiche.STATUT);
                _row.Controls.Add(cell3);
                //cellule action
                HtmlTableCell cell4 = new HtmlTableCell();
                //Ajout du bouton
                HtmlButton _button = new HtmlButton();
                _button.Attributes.Add("class", "btn btn-default");
                _button.InnerText = Resource.BTN_ACCESS_CARD;
                    // HERE IS THE PROBLEM
                _button.ServerClick += _buttonAccess_ServerClick;
                //bind on button
                cell4.Controls.Add(_button);
                _row.Controls.Add(cell4);
                p_body.Controls.Add(_row);
            }
        }
    }

    private void _buttonAccess_ServerClick(object sender, EventArgs e)
    {

    }

【问题讨论】:

    标签: c# asp.net events delegates


    【解决方案1】:

    您可以将基本继承与抽象函数一起使用。只需为这些类型的页面创建基本页面。

    public abstract class TablePage : Page {
         protected void tableInit(HtmlGenericControl p_head, HtmlGenericControl p_body, delOnGetRecent p_delOnGetRecent) {
          ...
          _button.ServerClick += actionEventHandler;
          ...
          }
          ...
    
       protected abstract void actionEventHandler(object sender, EventArgs e);
    }
    
    public class _Default : TablePage { ... 
    
    protected override void actionEventHander(object sender, EventArgs e) {}
    ...
    }
    

    现在您可以将所有常见的东西放在 TablePage 基类中,并将 TablePage.cs 放在 AppCode 或单独的项目中。

    【讨论】:

    • 这根本不是我想的,但这也很有趣,谢谢 :) 我会看看 :)
    猜你喜欢
    • 1970-01-01
    • 2010-10-09
    • 2011-03-23
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-20
    相关资源
    最近更新 更多