I have several controls that inherit BaseUserControl. BaseUserControl inherits System.Web.UI.UserControl.
I want to override OnLoad event like this:
public partial class MyControl1 : BaseUserControl
{
protected override void OnLoad(EventArgs e)
{
this.Value = myCustomService.GetBoolValue();
///More code here...
base.OnLoad(e);
}
}
This works great, the only problem is that I have to copy this piece of code across 3 controls, which I do not like. (I do not have access to Base class, since it is inherited by 100s of controls.)
So, my result currently looks like this:
public partial class MyControl2 : BaseUserControl
{
protected override void OnLoad(EventArgs e)
{
this.Value = myCustomService.GetBoolValue();
///More code here...
base.OnLoad(e);
}
}
public partial class MyControl3 : BaseUserControl
{
protected override void OnLoad(EventArgs e)
{
this.Value = myCustomService.GetBoolValue();
///More code here...
base.OnLoad(e);
}
}
What are the good ways to refactor this? One way is to extract
this.Value = myCustomService.GetBoolValue();
///More code here...
to a separate method, but I was wondering if there's a way that will allow us specify override event only once?
You could create an extra base class for those controls sharing functionality and make this class inherits from BaseUserControl
// Change YourBaseControl by a meaningful name
public partial class YourBaseControl : BaseUserControl
{
protected override void OnLoad(EventArgs e)
{
this.Value = myCustomService.GetBoolValue();
///More code here...
base.OnLoad(e);
}
}
public partial class MyControl2 : YourBaseControl
{
...
}
public partial class MyControl3 : YourBaseControl
{
...
}
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);
}
}
For some reason i am created my own version of System.Web.UI.Page class and inherited the deafult Page class. Now i want to set Title of every page that using my custom Page class on Page_Init event. Can you please suggest me the best way without creating a new version of Page_Init method on drrived classes
public class MyPage : Page{
protected void Page_Init(object sender, EventArgs e){
//do some basic opeation
}
}
public partial class Login : MyPage{
//want to set title from Here
}
Thanks
Ravi Mittal
What's wrong with doing this.Title?
I'm not too sure if you're setting the AutoEventWireup property but I'd suggest you to override the OnInit method rather than using Page_Init
You can set it just via Title property
protected void Page_Init(object sender, EventArgs e){
//do some basic opeation
this.Title = "Some title";
}
If you want to make it different for each sub page, you can make abstract property
public abstract class MyPage : Page
{
public abstract string PageTitle {get;}
protected override void OnInit(EventArgs e){
//do some basic opeation
this.Title = PageTitle;
base.OnInit(e);
}
}
and sample page
public class SubPage : MyPage
{
public override string PageTitle
{
get { return "Some sub title";}
}
}
Here's a portion of the standard Label control in WinForms:
public class Label : Control
{
protected override void OnTextChanged(EventArgs e)
{
...
}
}
I'd like to override the OnTextChanged event but I'm not sure of the best way.
Should I derive a subclass from Label class and then override the function like this?
public class Class1 : Label
{
protected override void OnTextChanged(EventArgs e)
{
MessageBox.Show("S");
}
}
If so, how and where should I add this class?
If not, how can I override functions which are defined inside a control?
This is the way you can override the method for control. As you have done is absolutely right but detailed implementation is here.
This is the form part
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class TestForm : Form
{
MyLabel newLable;
public TestForm()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
newLable = new MyLabel();
newLable.Height = 30;
newLable.Width = 40;
newLable.Text = "hello";
this.Controls.Add(newLable);
}
}
}
You can use MyLabel from toolbox also.
And MyLabel class is
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public class MyLabel:Label
{
public MyLabel()
{
}
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
MessageBox.Show("Label Clicked");
}
}
}
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()
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);
}
}