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);
}
}
Related
I am trying to access a global variable defined in the class.I am unable to access the variable inside my override function.What should I do???
below is the code :
public partial class VR: System.Web.UI.Page
{
public SqlConnection SQLCONN;
public string headervalue = "";
protected void Page_Load(object sender, EventArgs e)
{
}
// code...............
public class ITextEvents : PdfPageEventHelper
{
// code....
public override void OnEndPage(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document)
{
string value = "";
// code.....................................
headervalue //error unable to access it here
}
protected void btnExport_Click(object sender, EventArgs e)
{
headervalue = "abcc";
CreatePDF();
}
I am trying to access headervalue inside public override void OnEndPage method.I am able to access headervalue into btnExport click method
A variable can only be declared inside a class.
So, in order to use it, you must do it :
public class ITextEvents : PdfPageEventHelper
{
public static string headervalue = "";
public override void OnEndPage(PdfWriter writer, Document document)
{
[..your implementation..]
}
}
If you want to share it between more than one class, you need to use container like SimpleIOC.
I don't know if it will help you.
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.
}
}
I am working on an asp .net project and i have a main ascx control which is divided by two RadSpliters. On load of the main.ascx the app is loading two other controls, the control1.ascx and control2.ascx. In the control1 i have a treeview and on selected node of the treeview i want to reload the control2.ascx. Is there a way to do this. Below i am pasting the code that i am using to do that but is not working. Any help or suggestions please?
public partial class Control1: System.Web.UI.UserControl
{
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
BrowseProject b = new BrowseProject();
b.load();
}
public partial class MainControl : System.Web.UI.UserControl
{
public void load()
{
Control codeEditor = Page.LoadControl("Control2.ascx");
PlaceHolder4.Controls.Clear();
PlaceHolder4.ID = "PlaceHolder4";
PlaceHolder4.Controls.Add(codeEditor);
}
public interface IReload{
public void reload();
}
public partial class Control2: System.Web.UI.UserControl, IReload
{
}
public partial class Control1: System.Web.UI.UserControl
{
IReload _r;
public IReload setReload
{
set { _r = value; }
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
BrowseProject b = new BrowseProject();
b.load();
if(_r != null){
_r.reload();
}
}
public partial class MainControl : System.Web.UI.UserControl
{
public void load()
{
Control codeEditor = Page.LoadControl("Control2.ascx");
PlaceHolder4.Controls.Clear();
PlaceHolder4.ID = "PlaceHolder4";
PlaceHolder4.Controls.Add(codeEditor);
c.setReload(codeEditor);
}
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);
I want to have a generic event that I can fire that will take a custom eventArgs> e
Here is my code so far
public event resultsEventHandler<T> returnResults;
public delegate void resultsEventHandler<T>(object sender, resultEventArgs<ObservableEntityCollection<T>> e);
protected virtual void OnreturnResults(resultEventArgs<ObservableEntityCollection<T>> > e)
{
if (returnResults != null)
{
returnResults<T>(this, e);
}
}
public class resultEventArgs<ObservableEntityCollection<T>> : EventArgs
{
private readonly ObservableEntityCollection<T> _results;
public resultEventArgs(ObservableEntityCollection<T> results)
{
this._results = results;
}
public ObservableEntityCollection<T>> queryResult
{
get { return _results; }
}
}
Not sure of the question but
public class resultEventArgs<ObservableEntityCollection<T>> : EventArgs
should be
public class resultEventArgs<T> : EventArgs