对事件使用委托

格里高利

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

我对另一页做了类似的操作,在另一页中我必须在一页上做6个类似的表。我在提供差异数据的函数上使用了委托,然后将所需的函数传递给TableInit函数。效果很好!:)

但是现在我正在考虑做一个静态类,在其中我将所有表生成器函数放入表页面中,而不必复制粘贴所有函数。

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

我在这里将我的初始代码粘贴到Page中的所有内容,并带有一个问题:如何将表生成器函数与页面隔离?

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)
    {

    }
dave000

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

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或单独的项目中。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章