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.
Related
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);
}
}
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 have a user control class named EmployeeInformation.aspx.cs with a Textfield txtRegistrationNo
public partial class EmployeeInformation : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public string TextRegistrationNo
{
get
{
return txtRegistrationNo.Text;
}
}
}
Now in another class named SeparationInfo.aspx.cs how can i access txtRegistrationNo field?
You have to access it through object of your custom control, suppose the instance of EmployeeInformation is EmployeeInformation1
string TextRegistrationNo = EmployeeInformation1.TextRegistrationNo;
EmployeeInformation objEmployeeInfo = new EmployeeInformation();
string temp = objEmployeeInfo.TextRegistrationNo;
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 created an OnPaint event of my button,which I lately tried to override,but I failed
My code:
protected override void button1_Paint(PaintEventArgs e)
{
}
I get this error: "no suitable method found to override".
What should I do to make the error dissapear,but keep the method as override?
If the method is not virtual, you can't override it. If you can't override it, there is no point in trying to keep the override keyword.
If you want to shadow the method, you use the new keyword instead of override.
The method that you want to override is probably called OnPaint, not button1_Paint. Change the method declaration so it looks like this instead:
protected override void OnPaint(PaintEventArgs e) { }
Note though that this code should be in a subclass of the class from which you want to override the method. If you place this method in a form, it will handle that form's painting.
In your base-class, you need to declare the method as virtual
example:
public class Person
{
public virtual void DoSomething()
{
// do something here
}
}
public class Employee : Person
{
public override void DoSomething()
{
base.DoSomething();
}
}
Edit:
Perhaps this can help you out?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
}
private void button1_Paint(object sender, PaintEventArgs e)
{
}
}
You should declare the method in the base class as virtual in order to be able to override it.
you override methods coming from other classes. To keep the method you'll have to put it in a child class and call it from there.
do you have the override in the same class as the original method? if so, just merge the functions and remove the override.
I guess you have something like this now:
public class MyButton : Button {
public MyButton() {
this.Paint += new PaintEventHandler(MyButton_Paint);
}
void MyButton_Paint(object sender, PaintEventArgs e) {
//your code
}
}
If you inherited from a button you should use this code:
public class MyButton : Button {
protected override void OnPaint(PaintEventArgs pevent) {
base.OnPaint(pevent);
}
}