Reputation: 31
So, this is my situation. I have multiple ASPX pages (11 total), each of which contains a different set of fields exclusive to those pages.
The way I have implemented is to copy the same code over all those pages, make a few modifications and execute. Ex. on Page 1 remove any handling for Page01 button, on page 2 I remove eventhandler for Page02 button... so on and so forth.
Good News? This works just fine (postbacks, saves, etc). Bad News? It is tedious, especially if I have to need to add new event(s) to the control ASCX (TMControl.ASCX). Then, I have to recode all those 11 ASPX pages, wire them to the ASCX, et. al.
Question: Is there a way to do this in a way that I do not duplicate effort?
protected void Page_Load(object sender, EventArgs e)
{
Page.Title = "...";
wucTMControls = LoadControl("TMControls.ascx") as TMControls;
wucTMControls.btnPage02Click += new EventHandler(wucTMControls_btnPage02Click);
wucTMControls.btnPage03Click += new EventHandler(wucTMControls_btnPage03Click);
//-----------
// similar lines of eventhandling code exists here for pages 4 thru 11,
// which I removed for readability
//-----------
wucTMControls.btnSavePageClick += new EventHandler(wucTMControls_btnSavePageClick);
wucTMControls.btnSavePayeeClick += new EventHandler(wucTMControls_btnSavePayeeClick);
phTMControls.Controls.Add(wucTMControls);
if (!IsPostBack)
{
loadPage01();
}
}
void wucTMControls_btnPage02Click(object sender, EventArgs e) { if (noChangesOnThisPage()) wucTMControls.btnPage02Clicked(); }
void wucTMControls_btnPage03Click(object sender, EventArgs e) { if (noChangesOnThisPage()) wucTMControls.btnPage03Clicked(); }
//-----------
// similar lines of eventhandler code exists here for pages 4 thru 11,
// which I removed for readability
//-----------
void wucTMControls_btnSavePayeeClick(object sender, EventArgs e) { }
Upvotes: 0
Views: 119
Reputation: 20361
A UserControl is used when you have to do the same task on different pages. It seems to me that your UserControl does different things on all the pages it is used on. Moreover it has events named like the pages that it is used on. This is really bad design. The UserControl should not have to know anything about the pages. You can use member variables to alter the behavior of the UserControl. But it seems to me that a UserControl is not the solution for your task.
Upvotes: 1