How to Execute Page_Load() in Page's Base Class? - c#

I have the following PerformanceFactsheet.aspx.cs page class
public partial class PerformanceFactsheet : FactsheetBase
{
protected void Page_Load(object sender, EventArgs e)
{
// do stuff with the data extracted in FactsheetBase
divPerformance.Controls.Add(this.Data);
}
}
where FactsheetBase is defined as
public class FactsheetBase : System.Web.UI.Page
{
public MyPageData Data { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
// get data that's common to all implementors of FactsheetBase
// and store the values in FactsheetBase's properties
this.Data = ExtractPageData(Request.QueryString["data"]);
}
}
The problem is that FactsheetBase's Page_Load is not executing.
Can anyone tell me what I'm doing wrong? Is there a better way to get the result I'm after?
Thanks

We faced the similar problem, All you need to do is just register the handler in the constructor. :)
public class FactsheetBase : System.Web.UI.Page
{
public FactsheetBase()
{
this.Load += new EventHandler(this.Page_Load);
}
public MyPageData Data { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
// get data that's common to all implementors of FactsheetBase
// and store the values in FactsheetBase's properties
this.Data = ExtractPageData(Request.QueryString["data"]);
}
}
Another approach would be to override OnLoad() which is less preferred.
public class FactsheetBase : System.Web.UI.Page
{
public FactsheetBase()
{
}
public MyPageData Data { get; set; }
protected override void OnLoad(EventArgs e)
{
//your code
// get data that's common to all implementors of FactsheetBase
// and store the values in FactsheetBase's properties
this.Data = ExtractPageData(Request.QueryString["data"]);
base.OnLoad(e);
}
}

Instead of a Page_Load() method, override OnLoad() and call base.OnLoad() in PerformanceFactsheet

Uhm, I maybe wrong, but I believe this is due to inheritance: you are overwriting the FactsheetBase Page_Load method in the derived class.
In order to have it executed you should do something like
public partial class PerformanceFactsheet : FactsheetBase
{
protected void Page_Load(object sender, EventArgs e)
{
base.Page_Load( sender, e );
// do stuff with the data extracted in FactsheetBase
divPerformance.Controls.Add(this.Data);
}
}
EDIT: n8wrl definitely gave you a cleaner solution (I am not a ASPX programmer).

try this one
public partial class PerformanceFactsheet : FactsheetBase
{
public PerformanceFactsheet()
{
this.Load += new EventHandler(this.Page_Load);
}
protected void Page_Load(object sender, EventArgs e)
{
divPerformance.Controls.Add(this.Data);
}
}
public abstract class FactsheetBase : System.Web.UI.Page
{
public MyPageData Data { get; set; }
public FactsheetBase()
{
this.Load += new EventHandler(this.Page_Load);
}
new protected void Page_Load(object sender, EventArgs e)
{
this.Data = ExtractPageData(Request.QueryString["data"]);
}
}

try this one:
public partial class PerformanceFactsheet : FactsheetBase
{
protected override void Page_Load(object sender, EventArgs e)
{
base.Page_Load(sender, e);
// do stuff with the data extracted in FactsheetBase
divPerformance.Controls.Add(this.Data);
}
}
public class FactsheetBase : System.Web.UI.Page
{
public MyPageData Data { get; set; }
protected virtual void Page_Load(object sender, EventArgs e)
{
// get data that's common to all implementors of FactsheetBase
// and store the values in FactsheetBase's properties
this.Data = ExtractPageData(Request.QueryString["data"]);
}
}

Make the page load public, and call it in a manner like this from the other page:
this.myPageOrUserControl.Page_Load(null, EventArgs.Empty);

Related

Struggling to add to ListBox

So I'm making this small program for my assignment at university and I'm finding it hard to add to my list in my form. Here is my code:
public partial class WorkOutBeam : Form
{
Check checkLib;
public BindingList<ListBox> list;
public WorkOutBeam()
{
InitializeComponent();
}
public void StartForm(object sender, EventArgs e)
{
list = new BindingList<ListBox>();
listBox1.DataSource = list;
}
private void NewForce_Click(object sender, EventArgs e)
{
NewForceName forceItem = new NewForceName();
forceItem.Show();
}
public void AddToForceList(string name)
{
list.Items.Add(name);
}
}
NewForceName class below:
public partial class NewForceName : Form
{
public WorkOutBeam workOutBeam;
public NewForceName()
{
InitializeComponent();
}
private void OkButton_Click(object sender, EventArgs e)
{
if (NewForceNames.Text != "")
{
ReferToLibs();
workOutBeam.AddToForceList(NewForceNames.Text);
Close();
}
}
private void ReferToLibs()
{
workOutBeam = new WorkOutBeam();
}
private void NewForceName_Load(object sender, EventArgs e)
{
}
}
So I say to my program, "give me a new force." When it does, it initializes a new form of "NewForceName." I type into a text box and click 'Ok', this starts a public method shown below:
The list is a binding list which refers to the listBox as a data source. However the program tells me that the Items part is inaccessible due to its protection but I don't know how to add it as public. I tried looking in the properties of my listBox but to no avail.
Give this a shot:
public partial class WorkOutBeam : Form
{
Check checkLib;
// public BindingList<ListBox> list; // get rid of this for now
public WorkOutBeam()
{
InitializeComponent();
}
/*public void StartForm(object sender, EventArgs e)
{
list = new BindingList<ListBox>();
listBox1.DataSource = list;
}*/
private void NewForce_Click(object sender, EventArgs e)
{
NewForceName forceItem = new NewForceName(this); // pass a reference to this
// instance of WorkoutBeam
forceItem.Show();
}
public void AddToForceList(string name)
{
// we should do some more things here, but let's keep it simple for now
listBox1.Items.Add(name);
}
}
And
public partial class NewForceName : Form
{
public WorkOutBeam workOutBeam;
public NewForceName( WorkoutBeam beam ) // we take a WorkoutBeam instance as CTOR param!
{
InitializeComponent();
workoutBeam = beam;
}
private void OkButton_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(NewForceNames.Text))
{
workOutBeam.AddToForceList(NewForceNames.Text);
Close();
}
}
// DO NOT create new WorkoutBeams every time. Use the original.
/*private void ReferToLibs()
{
workOutBeam = new WorkOutBeam();
}*/
}
Disclaimer: I did not address each and every problem in this code. This is just enough so that it should "work" as intended.

how to access object from inheritance

Hello I want to access an object from inheritance in my project,but I cant find a way
My page is ;
public partial class siteler_page : siteDynamic
{
public static string pageType = "contentpage";
protected void Page_Load(object sender, EventArgs e)
{
}
}
And the main class is ; (i want to access that pageType paramter in onpreinit)
public class siteDynamic : System.Web.UI.Page
{
public siteDynamic()
{
//
// TODO: Add constructor logic here
//
}
protected override void OnPreInit(EventArgs e)
{
// I want to access the pageType in here
base.OnPreInit(e);
}
}
Any help is appreciated,thans
One way to do it is to define an abstract property and let the child classes override it (siteDynamic should be an abstract class):
public abstract class siteDynamic : System.Web.UI.Page
{
public siteDynamic()
{
// ...
}
public abstract string PageType { get; }
protected override void OnPreInit(EventArgs e)
{
string type = this.PageType;
// ...
base.OnPreInit(e);
}
}
public partial class siteler_page : siteDynamic
{
public override string PageType
{
get
{
return "contentpage";
}
}
protected void Page_Load(object sender, EventArgs e)
{
// ...
}
}
this will not work because your parent class siteDynamic is called an initialized first before your child class siteler_page. At least this is how you set it up. In order for this to work set your parent class should have a property in the parent class then override the base class method and set the value there.
public abstract class siteDynamic : System.Web.UI.Page
{
public string PageType { get; set; }
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
}
}
public partial class siteler_page : siteDynamic
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void OnPreInit(EventArgs e)
{
base.PageType = "contentpage";
base.OnPreInit(e);
}
}

Modified UserControl

I have following control class withsome extra methods in it. I have multiple form usercontrols, FormChoosePage has dropdownlist with form list in it, and I get selected forms user control, load its data and after he click save button I call forms save method.
at btnSave_Click ucForm is null, I have 2 question;
1 ) how can I keep dynamicly Modified UserControl (View State or something?)
2 ) am I on right way to do this with generic user controls and everything. If not, what is you sugestion?
FormControler
public class Controler : UserControl
{
public virtual void PageLoad() { }
public virtual void SaveForm() { }
}
Form UC
public partial class ApplicationForm : Controls.Controler
{
public override void PageLoad()
{
ddlFormType.DataSource = [Data];
ddlFormType.DataBind();
}
public override void SaveForm()
{
XForm form = new XForm();
form.something = txtSomething.Text;
form.Save();
}
}
Form Choose Page
public partial class FormChoosePage: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlFormLoad();
}
}
Controler ucForm;
protected void ddlForm_SelectedIndexChanged(object sender, EventArgs e)
{
XForm form = XForm.Get<XForm>(ddlForm.SelectedValue);
ucForm = this.LoadControl(form.URL) as Controler;
ucForm.ID = "ucForm";
ucForm.PageLoad();
}
protected void btnSave_Click(object sender, EventArgs e)
{
ucForm.SaveForm();
}
}

Inherit event handlers

Im trying to write abstract class for different reports.
I have a method
protected Tuple<byte[], string, string> RenderReport()
which has such lines
var localReport = new LocalReport { ReportPath = _reportLocalFullName };
...
localReport.SubreportProcessing += localReport_SubreportProcessing;
Derived class must write own code in the localReport_SubreportProcessing.
I'm not sure how to make inheritance here. Can someone help ?
Rather than having a method:
private void localReport_SubreportProcessing(...) {...}
consider instead:
protected virtual void OnSubreportProcessing(...) {...}
Now your subclasses can simply use:
protected override void OnSubreportProcessing(...) {...}
You can call a common method, which you override in your base class.
So in localReport_SubreportProcessing, call ProcessSubreport
private void localReport_SubreportProcessing(object sender, EventArgs e)
{
this.ProcessSubreport();
}
protected virtual void ProcessSubreport()
{ }
And override it in your deriving class:
protected override void ProcessSubreport()
{ }
Try like below.
public abstract class BaseReport
{
......
protected Tuple<byte[], string, string> RenderReport()
{
var localReport = new LocalReport { ReportPath = _reportLocalFullName };
...
localReport.SubreportProcessing += localReport_SubreportProcessing;
...
}
protected abstract void LocalReport_SubreportProcessing(object sender, EventArgs e);
}
public class DerivedReport1 : BaseReport
{
protected override void LocalReport_SubreportProcessing(object sender, EventArgs e)
{
// Report generation logic for report1.
}
}
public class DerivedReport2 : BaseReport
{
protected override void LocalReport_SubreportProcessing(object sender, EventArgs e)
{
// Report generation logic for report2.
}
}

Calling a base page method from a user control in asp.net

I have been trying to find a good answer to this question, but can't seem to find one. I have an ASP.NET page that derives from a base page, like this:
public partial class MainPage : MyBasePage
{
protected void Page_Load(object sender, EventArgs e)
{
var loginTime = GetLoginTime(); // This works fine
}
}
And the base page:
public partial class MyBasePage: Page
{
}
protected DateTime GetLoginTime()
{
// Do stuff
return loginTime;
}
Now I have a user control on that page that needs to call my method...Like this:
public partial class TimeClock : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
var loginTime = GetLoginTime(); // This does not work!
}
}
As you can see, I cannot call my base method, for obvious reasons. My question is, how can I call this method from my user control? One work around I've found is like this:
var page = Parent as MyBasePage;
page.GetLoginTime(); // This works IF I make GetLoginTime() a public method
This works, if I make my function public instead of protected. Doing this doesn't seem like a very OOP way to tackle this solution, so if someone can offer me a better solution, I'd appreciate it!
TimeClock inherits from UserControl, not from MyBasePage so why should TimeClock see the Method GetLoginTime()?
You should keep your UserControl out of your Page stuff. It should be decoupled in OOP speak. Add properties to set values and delegates to hook into events:
public partial class TimeClock : UserControl
{
public DateTime LoginTime{ get; set; }
public event UserControlActionHandler ActionEvent;
public delegate void UserControlActionHandler (object sender, EventArgs e);
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button_Click(object sender, EventArgs e)
{
if (this.ActionEvent!= null)
{
this.ActionEvent(sender, e);
}
}
}
Page
public partial class MainPage : MyBasePage
{
protected void Page_Load(object sender, EventArgs e)
{
var loginTime = GetLoginTime();
TimeClock1.LoginTime = loginTime;
TimeClock1.ActionEvent += [tab][tab]...
}
}
(this.Page as BasePage).MethodName()

Categories

Resources