Pass MasterPage ImageButton event to content Page - asp.net

I have ImageButton in a MasterPage. I want the OnClick event to fire and be captured by the .ASPX page hosted inside the MasterPage?
MasterPage:
<asp:ImageButton ID="btnClear" OnClick="Clear_Click"
ImageUrl="images/Back_Icon_06.png" runat="server" AlternateText="Clear"
width="38" height="39"/>

The masterpage is actually a child of the page (in fact, it's a UserControl). We don't want the page to have to be aware of the intimate details of its child controls (thats why we delegate those aspects to those controls in the first place), so the correct approach would be to handle the click event on the master page and from there fire another event on the masterpage which the page handles:
Master:
public event EventHandler SomethingHappened;
protected void Button_Click(object sender, EventArgs e)
{
OnSomethingHappened(EventArgs.Empty);
}
protected void OnSomethingHappened(EventArgs e)
{
if(this.SomethingHappened != null)
{
this.SomethingHappened(this, e);
}
}
Page:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
//allows us to change master pages
if(this.Master is MyMaster)
{
((MyMaster)this.Master).SomethingHappened += new EventHandler(HandleSomethingHappened);
}
}
private void HandleSomethingHappened(object sender, EventArgs e)
{
//deal with it
}

I would recommend specifying a strongly typed master page in your content page and exposing the event on the master page side
Here's a good MSDN reference
This is similar to what Rex M specified but just simplifies accessing the Master Page a little bit.
// Master Page Code
public event EventHandler ClearClick
{
add
{
this.btnClear.Click += value;
}
remove
{
this.btnClear.Click -= value;
}
}
// Content Page markup
<%# Page masterPageFile="~/MasterPage.master"%>
<%# MasterType virtualPath="~/MasterPage.master"%>
// Content Page Code
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Master.ClearClick += new EventHandler(OnClearClick);
}
private void OnClearClick(object sender, EventArgs e)
{
// Handle click here
}

Related

How to check which control fired in Master Page

How can we find the control that fired inside of a Master page that also has a panel from code behind?
I have the following layout.
Master Page
+---------Panel-----------+
| TextBox Button1 Button2|
+-------------------------+
|
-------->Child Page
+-------------------------+
| Get From Here! |
+-------------------------+
When I try the following the targetId is empty
if (IsPostBack)
{
var targetID = Request.Form["__EVENTTARGET"];
if (targetID != null && targetID != string.Empty)
{
var targetControl = this.Page.FindControl(targetID);
}
}
I want to find out which button caused post back.
****Update****
I've tried the following link below without sucess.
Find on Page Init
You can use an event that your MasterPage offers to communicate with the Page.
Within your MasterPage create something like these
public event EventHandler Button1ClickEvent;
public event EventHandler Button2ClickEvent;
What is going on here is that you are creating an event that the MasterPage can check for a page that is listening for and fire an event to that page to signal that is has been called. In this case EventHandler means that you'll be passing EventArgs to the event listener.
You send an event by doing this
protected void Button1_Click(object sender, EventArgs e)
{
if (Button1ClickEvent != null) // check if a sub page is implementing it
Button1ClickEvent(this, EventArgs.Empty); // fire the event off
}
protected void Button2_Click(object sender, EventArgs e)
{
if (Button2ClickEvent != null)
Button2ClickEvent(this, EventArgs.Empty);
}
Next, wire up your Page to implement these events
Add in this to your Page definition
<%# MasterType VirtualPath="~/MasterPage.master" %>
Now you can access these events in your page code-behind
private void Master_Button1Click(object sender, EventArgs e)
{
// This is called when the `Button1ClickEvent` is fired from your MasterPage
}
private void Master_Button2Click(object sender, EventArgs e)
{
// This is called when the `Button2ClickEvent` is fired from your MasterPage
}
protected void OnInit(EventArgs e)
{
// Setup an event handler for the buttons
Master.Button1ClickEvent += new EventHandler(Master_Button1Click);
Master.Button2ClickEvent += new EventHandler(Master_Button2Click);
}

how to update master page in all child pages without refreshing

As i am new to asp.net i need some help from you,
i am using a MasterPage and child pages.
In master page I have a Label in which it shows the items added to cart.
when i am adding a product in products page.aspx it is updated in master page as (1 item) and showing in the label but when i am coming to homepage.aspx it is not showing i am using same master page in home.aspx.
If i refresh the home.aspx, it is updated. I need to update it without refreshing.
Sample code look like this
MasterPage markup: Here's the label in your masterpage which you want to be update from childpage
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
MasterPage.cs:
public partial class MasterPage : System.Web.UI.MasterPage
{
public string labeltext
{
get
{
return Label1.Text;
}
set
{
Label1.Text = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
ChildPage.cs:
protected void Page_Load(object sender, EventArgs e)
{
this.Master.labeltext = "Your value"; // set your value
}

Adding User-Control when clicking a button

I created a user-control (customized calendar - but it doesn't matter).
In addition, I have a button control, and my target is to add to the page the calendar (the user-control) when clicking on the button. The number of calendars in the page is unlimited, I do not want to add only one calendar to the page.
protected void addCalendar_Click(object sender, EventArgs e)
{
// clicked on button, should add the user control.
}
The user-control is already registered on the page:
<%# Register src="CalendarUserControl.ascx" tagname="CalendarUserControl" tagprefix="uc" %>
Each calendar should be appended to the following div (this div is defined in the aspx page):
<div id="calendars-holder">
// first calendar appended
// second calendar appended
// and so on..
</div>
protected int countCalendars;
protected void Page_Load(object sender, EventArgs e)
{
if (ViewState["countCalendars"] != null)
countCalendars = (int) ViewState["countCalendars"];
// init
if (!IsPostBack)
countCalendars = 0;
// register existing user-controls; won't occur before onclick button
// so ViewState["countCalendar"] will be exist.
for (int i = 0; i < countCalendars; i++)
addCalendar(i);
}
// onclick button
protected void addCalender_Click(object sender, EventArgs e)
{
addCalendar(countCalendars++);
ViewState["countCalendars"] = countCalendars;
}
// Adding a new User-Control
protected void addCalendar(int idNumber)
{
CalculatorUserControl Calendar = LoadControl("~/CalendarUserControl.ascx") as CalendarUserControl;
Calendar.ID = "CalculatorUserControl" + idNumber.ToString();
PlaceHolder1.Controls.Add(Calendar);
}

Submit is not working

I'm using dynamic controls at my ASP.NET applications. So the issue is - when I use these controls separatelly - they work well. But when I put these two controls on the same page - ONLY LAST submit event is fired. Could anyone help me to identify where is the issue and how to fix it?
Here is my aspx layout:
<%# Control Language="C#" AutoEventWireup="True" CodeBehind="MYPAGE.ascx.cs" Inherits="MYNAMESPACE" %>
<%# Register Assembly="MYANOTHERNAMESPACE" Namespace="MYNAMESPACE" TagPrefix="DAControl" %>
...
<div>
<DAControl:ChooseImageDialog runat="server" id="ChooseImageDialog" />
<DAControl:ChooseVideoDialog runat="server" id="ChooseVideoDialog" />
</div>
Here is my 1st control:
[ToolboxData("<{0}:ChooseImageDialog runat=server></{0}:ChooseImageDialog>")]
public class ChooseImageDialog : WebControl
{
/* This is only part of my code, which should enough for explaining the issue*/
private Button applyPreview = new Button();
private Button cancelPreview = new Button();
protected override void OnLoad(EventArgs e)
{
applyPreview.Click += new EventHandler(applyPreview_Click);
base.OnLoad(e);
}
void applyPreview_Click(object sender, EventArgs e)
{
// I want to reach this block
}
}
}
Here is my 2nd control:
[ToolboxData("<{0}:ChooseVideoDialog runat=server></{0}:ChooseVideoDialog>")]
public class ChooseVideoDialog : WebControl
{
/* This is only part of my code, which should enough for explaining the issue*/
private Button applyVideoPreview = new Button();
private Button cancelPreview = new Button();
protected override void OnLoad(EventArgs e)
{
applyVideoPreview.Click += new EventHandler(ApplyPreviewVideo_Click);
base.OnLoad(e);
}
void ApplyPreviewVideo_Click(object sender, EventArgs e)
{
// I want to reach this block
}
}
Events must be wired up in the Init event. Any later in the life cycle is too late.

Access textbox value in a header in a master page to .aspx.vb page

I created a textbox and a submit button in header(User Control) included in a master page
and I want to use that textbox value after clicking submit in my .aspx.vb page.
How can i access that, as the vb page is loaded first and then master page is loading ?
TextBox Val = (TextBox)this.Master.FindControl("TextBoxID");
The best way to communicate from UserControl to Page/MasterPage is using events
The best way to communicate from MasterPage to Page is using events
On this way you don't hardlink UserControls with their Pages/MasterPages and the Page with it's Master.
Add an event to your UserControl that is raised when the user clicks the button, for example:
In UserControl of type MyControl:
public delegate void SubmittedHandler(MyControl ctrl);
public event SubmittedHandler Submitted;
protected void BtnCLick(object sender, EventArgs e) {
Submitted(this);
}
Then add an handler to this event in your MasterPage, handle it and raise the Master's event again:
In Master's codebehind:
public delegate void MyControlSubmittedHandler(MyControl ctrl);
public event MyControlSubmittedHandler ControlSubmitted;
protected void Page_Init(Object sender, EventArgs e) {
this.MyControl1.Submitted += MyControlSubmitted;
}
protected void MyControlSubmitted(MyControl sender) {
ControlSubmitted(sender);
}
Then add an handler to this event to your page:
In your Page:
protected void Page_Init(object sender, EventArgs e) {
((SiteMaster)Master).ControlSubmitted += MasterControlSubmitted;
}
protected void MasterControlSubmitted(MyControl sender){
// do whatever you need to do
}
If you only need to access the TextBox from the page and you don't need to handle the click-event, you could also use properties to achieve this:
add a public property f.e. MyControlText in your UserControl that get/set the TextBox.Text property
add a public property f.e. MyControlText in your Master that get/set your UserControl's MyControlText property
Now you can get/set this property from your page in this way:
((SiteMaster)Master).MyControlText = "Hello World";

Resources