I'm trying to have a button that's clicked in the Site.Master page to fire a click event in the content page.
I understand that the Master-page actually turns into a child class on compilation.
I've tried several different solutions after a few hours of googling with no result in site.
Here is the current solution I have applied:
PageLoad()
{
Button btn = this.Master.FindControl("lbtnMaintenance") as Button;
btn.Click += new System.EventHandler(MasterlbtnMaintenance);
}
private void MasterlbtnMaintenance(object sender, EventArgs e)
{
MaintenanceHomeScreen(true);
}
//Created an Interface on .aspx page:
public interface ICommandable
{
void LblMaintenanceClick(object argument, EventArgs e);
}
public partial class Default : Page, ICommandable
{
public void LblMaintenanceClick(object sender, EventArgs e)
{
}
}
// On master page:
public void lbtnMaintenance_OnClick(object sender, EventArgs e)
{
if (Page is ICommandable)
{
(Page as ICommandable).LblMaintenanceClick(sender, e);
}
else
throw new NotImplementedException("u NO WURK");
}
Related
I have a project that uses various click events and looks like this
namespace Example
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_obj_Click(object sender, EventArgs e)
{
MyMethods.Method_1("text1");
}
private void btn_catg_Click(object sender, EventArgs e)
{
MyMethods.Method_1("text2");
}
private void btn_up_Click(object sender, EventArgs e)
{
MyMethods.Method_2("text1");
}
private void btn_top_up_Click(object sender, EventArgs e)
{
MyMethods.Method_2("text2");
}
private void btn_down_Click(object sender, EventArgs e)
{
MyMethods.Method_2("text3");
}
private void btn_top_down_Click(object sender, EventArgs e)
{
MyMethods.Method_2("text4");
}
public static class MyMethods
{
public static void Method_1(string text) {...}
public static void Method_2(string text) {...}
}
}
}
As you can see I have a quite a number of click events so i'm curious if I can group them all in another c# file or a class or something
In your code-behind, declare a common method you want to call when any of the above buttons fire the Click event.
private void CommonClick(object sender, EventArgs e)
{
}
Now in your Properties window for each button, you can assign this event handler for all buttons:
Now when any of the buttons are clicked this same event handler is called.
If you want to know which button is clicked, you can either use button Name or even the Tag property.
Let's say we assign a separate unique Tag for each button. Tag is a property you can see in the property window for each button (and most controls).
Then you can use a switch-case statement in your code to identify which button was clicked.
private void CommonClick(object sender, EventArgs e)
{
switch (((Button)sender).Tag)
{
case "B1":
break;
case "B2":
break;
}
}
Above, B1, B2 etc are the tags I've assigned to each button.
usually in the form designer you dblclick on the empty "click" event property to generate new method as btn_..._Click(object sender, EventArgs e).
instead you can select existed method, so multiple buttons can call the same method:
Then in the called Method you can check which control trigger this event:
private void button1_Click(object sender, EventArgs e)
{
if (sender == button2)
{
// ....
}
if (sender == button1)
{
// ....
}
}
I am using Virtual Studio Community in C# (.Net 4.5).
I have a simple form, which contains one button and one webBrowser control.
When I click the button, I make the webBrowser navigate to google.com.
Then, when the page is loaded, I try to override the linkClick events as I saw in a solution I read on this site (stackoverflow) earlier.
But then, when I click on a link on the loaded page, it says the navigation was cancelled, but it navigates anyways.
What am I doing wrong?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("http://www.google.com/");
}
private bool bCancel = false;
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
for (int i = 0; i < webBrowser1.Document.Links.Count; i++)
{
webBrowser1.Document.Links[i].Click += new HtmlElementEventHandler(this.LinkClick);
}
}
private void LinkClick(object sender, System.EventArgs e)
{
bCancel = true;
MessageBox.Show("Link Was Clicked Navigation was Cancelled");
}
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
if (bCancel == true)
{
e.Cancel = true;
bCancel = false;
}
}
}
You need to bind the WebBrowserControl.Navigating event to the handler you've written; having the handler's name matching the control underscore event name isn't enough.
So you can do this in the Form1's constructor:
public Form1()
{
InitializeComponent();
webBrowser1.Navigating += new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
}
Better yet, add a Load event and do the same there. Check out the official documentation on the subject.
I'm currently having two master pages M1 and M2 and several content pages.
M2 has a calendar control and I want to call a content page function each time the selection on the calendar changes.
Here's the code I have in my master page:
public partial class Master2 : BaseMasterPage
{
public event EventHandler CalendarSelectionChanged;
public void Calendar_OnSelectionChanged(object sender, EventArgs e)
{
if (CalendarSelectionChanged != null)
CalendarSelectionChanged(this, EventArgs.Empty);
}
}
And here's the code in the content Page C1:
protected void Page_PreInit(object sender, EventArgs e)
{
Master.CalendarSelectionChanged += new EventHandler(OnMainCalendarSelectionChanged_SubContent);
}
private void OnMainCalendarSelectionChanged_SubContent(object sender, EventArgs e)
{
DoSomething();
}
but the CalendarSelectionChanged is always null and hence the function isn't called.
After some serious researching and dabbling around, I've finally made the calendar in the Master page to trigger an event in the content page.
The Code in the Content Page is the same as mentioned in the question.
The Code in the Second Master page(Nested) is Changed to :
public partial class Master2 : BaseMasterPage
{
public event EventHandler CalendarSelectionChanged;
public void Calendar_OnSelectionChanged(object sender, EventArgs e)
{
OnCalendar_SelectionChanged_CustomEvent(e);
///.....
}
public virtual void OnCalendar_SelectionChanged_CustomEvent(EventArgs e)
{
if (Calendar_SelectionChanged_CustomEvent != null)
Calendar_SelectionChanged_CustomEvent(this, EventArgs.Empty);
}
}
Add the virtual reference path in the content page:
<%# MasterType VirtualPath="~/MasterPage.master" %>
I have a Form with Button ButtonGo.
and I have a class which takes a button through its constructor, then handles its events:
public class HandlingClass
{//.......
Button go ;
public HandlingClass(Button btn)
{
this.go = btn;
this.go.Click += new EventHandler(this.go_Click);
}
//.....
public void go_Click(object sender, EventArgs e)
{
//logic here
}
What am I doing wrong, and why isn't the event being raised when I press the Button in the caller form?
This code works for me
public class HandlingClass
{
Button go;
public HandlingClass(Button btn)
{
go = btn;
go.Click += go_Click;
}
void go_Click(object sender, RoutedEventArgs e)
{
throw new NotImplementedException();
}
}
and in your loaded event of the class having the button you just add the below code
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
HandlingClass hc=new HandlingClass(**MyButton**);
}
MyButton should be the reference to your button.
I have searched extensively on this, but cannot find the solution to my problem. I am trying to call a function in the code behind of a page from a user control on that page.
I have a web application that uses a master page. I am adding a user control that I wrote to one of the content pages. I added the user control to the aspx page by dragging and dropping it from the toolbox. I am able to see the user control from the code behind, but I cannot access the public functions. To fix that problem, I created an object of the user control in the code behind and used the LoadControl function. All of that seems to work fine.
The problem I am having is when I am trying to hook the into the EventHandler from the aspx page to the user control. Everything compiles and runs just fine, but I am not seeing anything happen on the page. I think the issue is that the EventHandler is always null.
User Control Code
public partial class ucBuyerList : System.Web.UI.UserControl
{
public delegate void BuyerSelectedEventHandler(object sender, EventArgs e);
public event BuyerSelectedEventHandler BuyerSelected;
private string name = "";
public string Name
{
get { return name; }
set { name = value; }
}
private string auid = "";
public string AUID
{
get { return auid; }
set { auid = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
private void OnBuyerSelected(EventArgs e)
{
if (BuyerSelected != null)
{
BuyerSelected(this, new EventArgs());
}
}
protected void lbBuyerList_SelectedIndexChanged(object sender, EventArgs e)
{
SetNameAndAUID();
OnBuyerSelected(e);
}
private void SetNameAndAUID()
{
name = lbBuyerList.SelectedItem.Text;
auid = lbBuyerList.SelectedItem.Value;
}
}
Parent Page Code
public partial class frmBuyerInformation : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Master.changePageTitle("Buyer Information");
buyerList.BuyerSelected += new ucBuyerList.BuyerSelectedEventHandler(buyerListControl_BuyerSelected);
}
void buyerListControl_BuyerSelected(object sender, EventArgs e)
{
DisplayBuyerInformation();
}
public void DisplayBuyerInformation()
{
tbName.Text = buyerList.Name;
tbAUID.Text = buyerList.AUID;
}
}
Can anyone see what I am doing wrong?
EDIT: This issue has been resolved. The code snippits above are now functional. If anyone runs into the issue I had, you can model the code above. Make sure that AutoEventWireup="true" in both the aspx and ascx pages. Thank you June Paik for your solution. Thank you Diego De Vita for your input as well.
I've been struggling with events for quite a while as well. Nowadays I always create them this way 'cause it's the only way I know it works. Haven't tested it with your code but here it goes anyway:
public partial class ucBuyerList : System.Web.UI.UserControl
{
public delegate void BuyerSelectedEventHandler(object sender, EventArgs e);
public event BuyerSelectedEventHandler BuyerSelected;
public string Name;
public string AUID;
protected void Page_Load(object sender, EventArgs e)
{
//Select the first buyer in the list when the user control loads
if (!IsPostBack)
{
lbBuyerList.SelectedIndex = 0;
}
}
private void OnBuyerSelected(EventArgs e)
{
BuyerSelectedEventHandler handler = BuyerSelected;
if (handler != null)
{
handler(this, new EventArgs());
}
}
protected void lbBuyerList_SelectedIndexChanged(object sender, EventArgs e)
{
Name = lbBuyerList.SelectedItem.Text;
AUID = lbBuyerList.SelectedItem.Value;
OnBuyerSelected(e);
}
}
In the parent page you can just call your function the same way you're doing it already.
It could be that Page_Load is too late in the page lifecycle to be using LoadControl and subscribing to the event. What happens if you move that code to the Page_Init method?