I have contentpage overriding the OnInit event of a masterpage. The override works fine, until I put a custom user control on the page: in this case the OnInit event does not fire for the contentpage (no overrides are used in the user control)
What are the possible causes/solutions for this? (I use the OnInit event to create dynamic controls)
Edit:
now i tried this in the content page:
(The OnPreInit part runs, but Masters_Init does not get called...)
protected override void OnPreInit(EventArgs e)
{
base.Master.Init += new EventHandler(Masters_Init);
}
void Masters_Init(object sender, EventArgs e)
{
//code
}
Are you calling the base.OnInit?
public override void OnInit(EventArgs e)
{
// code before base oninit
base.OnInit(e);
// code after base oninit
}
Update
public class Page1 : Page
{
public Page1 : base() {
PreInit += Page_PreInit;
}
void Page_PreInit(object sender, EventArgs e)
{
Master.Init += Master_Init;
}
void Master_Init(object sender, EventArgs e)
{
//code
}
}
Also as mentioned in the comments I would recommend not overriding the events if you don't have to, but if you must be sure to call the base. so in your edit above it should be
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
base.Master.Init += new EventHandler(Masters_Init);
}
Related
I have some basic override code in the same class to catch mouse events. I can get OnMouseWheel to fire, but other click events do to not fire with the same code.
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
Debug.WriteLine("mouse down"); //does not work
}
protected override void OnMouseWheel(System.Windows.Forms.MouseEventArgs e)
{
Debug.WriteLine("mouse wheel"); //works
}
Does OnMouseWheel need focus to fire? That has been what I have been trying to troubleshoot so far.
protected override void OnMouseClick ( MouseEventArgs e )
{
Debug.WriteLine("Mouse click"); //works
}
private void panel1_MouseClick ( object sender, MouseEventArgs e )
{
OnMouseClick(e);
}
In your actual design window make sure you attached the event to the Control. If you need to do it programatically, then
this.panel1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseClick);
Note:
Yes I know the control names aren't the exact same and you aren't using MetroControls, but it's the same idea.
You need to call the base method implementation of OnMouse events so the base class can react to the events:
protected override void OnMouseDown(MouseEventArgs e)
{
Debug.WriteLine("OnMouseDown");
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
Debug.WriteLine("OnMouseUp");
base.OnMouseUp(e);
}
protected override void OnMouseWheel(MouseEventArgs e)
{
Debug.WriteLine("OnMouseWheel");
base.OnMouseWheel(e);
}
I have this page say test.aspx.Its codebehind is like the code below.
protected void Page_Load(object sender, EventArgs e)
{
}
public void display()
{
// some block statements
}
Since the function display() is outside the Page_Load it is never called. So how do
I make this call to this function after Page_Load.
NOTE: I need this function outside the Page_Load.
Use Page.LoadComplete:
protected void Page_LoadComplete(object sender, EventArgs e)
{
display();
}
protected void Page_Load(object sender, EventArgs e)
{
//call your function
display();
}
protected void Page_PreRender(object sender, EventArgs e)
{
//call your function even later in the page life cycle
display();
}
public void display()
{
// some block statements
}
Here is the documentation that discusses the various Page Life Cycle methods: http://msdn.microsoft.com/en-us/library/ms178472(v=vs.90).aspx
After Page_Load you have:
PreRender
Render
Unload
If you want to load that function on the time of load only then do like this
protected void Page_Load(object sender, EventArgs e)
{
if(!isPostBack)
{
display();
}
}
public void display()
{
// some block statements
}
as this will load only once. But if u want to load it on each post back then do like this
protected void Page_Load(object sender, EventArgs e)
{
if(!isPostBack)
{}
dispplay();
}
public void display()
{
// some block statements
}
There's a default event called Page_LoadComplete which will execute when page is loaded fully
But,If you write your code in a Page_Load Event that code will execute and your controls will be accessible there
So best to call like in a page_load for once with first postback ;)
But, still if you want to go after page load then go for the Page_LoadComplete
protected void Page_LoadComplete(object sender, EventArgs e)
{
display();
}
public void display()
{
// some block statements
}
I've created a custom user control for a windows form that will operate similar to a button (and please don't suggest that I just use a button, because I will be storing data in this user control), but I can't figure out how to get the OnClick() event to fire. I've sifted through a handful of tutorials and looked at a few similar questions on the site, but I can't seem to get the event to fire off - so I'm either doing something wrong or everyone posted incorrect code (I hope it isn't the latter)
In my custom control.cs,
namespace MobCreator {
public partial class MOBSample : UserControl {
public MOBSample() {
InitializeComponent();
}
protected override void OnMouseUp(MouseEventArgs e) {
this.BorderStyle = BorderStyle.FixedSingle;
base.OnMouseUp(e);
}
protected override void OnMouseDown(MouseEventArgs e) {
this.BorderStyle = BorderStyle.Fixed3D;
base.OnMouseDown(e);
}
public event EventHandler ButtonClick;
private void OnButtonClick(object sender, EventArgs e) {
// invoke UserControl event here
if (this.ButtonClick != null) this.OnButtonClick(sender, e);
}
}
}
And in my form.cs,
private void MobCreatorForm_Load(object sender, EventArgs e) {
UserControl1.ButtonClick += new EventHandler(this.CustomEvent_Handler);
}
private void CustomEvent_Handler(object sender, EventArgs e) {
Console.WriteLine("Click");
}
However, when I run the program my console never outputs "Click".
Check this link on MSDN: it is a simple Event tutorial, you should be able to adapt it to your scenario.
At a first look, what you are probably missing is a Delegate for your event.
Try this
private void MobCreatorForm_Load(object sender, EventArgs e)
{
CustomEvent_Handler(null,null);
}
private void CustomEvent_Handler(object sender, EventArgs e)
{
Console.WriteLine("Click");
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
content page class method calling from master page class
I need to access Content Page Method from Master page Event. How can I do this?
Content Page:
public partial class Call_Center_Main : System.Web.UI.Page
{
Page_Load(object sender, EventArgs e)
{
}
public void MenuClick(string ClkMenu)
{
// Some Code
}
}
MasterPage:
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
{
//How Can I call MenuClick method from Content Page from Here ???
}
}
This answer is taken from Interacting with the Content Page from the Master Page
You can do this using Delegates.
For Example, you have a button in MasterPage and you want to call a Method in Content Page from Master Page. Here is the Code in Master Page.
Master Page:
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (contentCallEvent != null)
contentCallEvent(this, EventArgs.Empty);
}
public event EventHandler contentCallEvent;
}
Content Page:
public partial class Content_1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private void Master_ButtonClick(object sender, EventArgs e)
{
// This Method will be Called.
}
protected void Page_PreInit(object sender, EventArgs e)
{
// Create an event handler for the master page's contentCallEvent event
Master.contentCallEvent += new EventHandler(Master_ButtonClick);
}
}
And Also add the Below Line Specifying you MasterPage Path in VirtualPath
<%# MasterType VirtualPath="~/MasterPage.master" %>
// This is Strongly Typed Reference
I have a ascx page suppose page1.ascx in that I have a button click event handler
btnSave.Click +=
delegate
{
if (Save != null) Save(this, EventArgs.Empty);
};
and I have another ascx page suppose page2.ascx in that I have a button click
protected void btnEdit_Click(object sender, EventArgs e)
{
// want to call
btnSave.Click +=delegate
{
if (Save != null) Save(this, EventArgs.Empty);
};
}
I want to call btnSave click delegate(page1.ascx) on btnEdit(page2.ascx). Is it possible if yes then how?
If I understood what you mean, one way would be as below:
public class Control1 : UserControl {
public delegate void ButtonClickHandler(object sender, EventArgs e);
public ButtonClickHandler ButtonClickEvent {get;set;}
public void Save(object sender, EventArgs e) {
//do something
if (ButtonClickEvent != null) {ButtonClickEvent(sender, e);}
//do something
}
}
public class Control2 : UserControl {
protected override void OnLoad(EventArgs e) {
control1.ButtonClickEvent += YourMethod;
}
protected void YourMethod(object sender, EventArgs e) { // do something here ... }
}
Another way is to declare your button's event in the first control as property and assign your method in the 2nd control.
I'd recommend moving the code out of the code-behind and into a separate class, so that it is accessible to both .ascx files.