Reputation: 620
Context: I have to create 4 pages where I will have to create 2 tables for each pages. Each table have the same structure and the only change is on the data that are loaded.
I did something similar for another page where I had to do 6 similar tables on a page. I used delegate on functions that give the differents data and I pass the function needed to my TableInit function. It works well ! :)
But now I'm thinking about doing a static class where I will put all my table generator function to be able tu put tables in severals page without having to copy paste all the function.
The problem is that I have a button somewhere in the table and that I fix an event located in the page.aspx on this button. And when I put my function in another static class I just can't find a way to pass my event.
I'm pasting here my initial code with everything in the Page with that question: How do you isolate the table generator function from the 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)
{
}
Upvotes: 0
Views: 56
Reputation: 116
You can use basic inheritance with abstract functions. Simply create base page for these type of pages.
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) {}
...
}
Now you can put all the common stuff in the TablePage base class and put the TablePage.cs in AppCode or in a separate project.
Upvotes: 1