使用委托与事件

时间:2016-12-01 13:14:10

标签: c# asp.net events delegates

上下文:我必须创建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)
    {

    }

1 个答案:

答案 0 :(得分: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中或单独的项目中。