Calling a content page method from nested master pages - c#

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" %>

Related

Trying to call Method from control page

I have been trying to call a method from my .ascx.cs to my .aspx.cs codebehind. The routine will allow a check box on the .aspx page to hide/show a Textbox and Label on my user control page. Here is what I have so far. I have the visibility of Label and the Textbox set to false in properties.
control Page code behind:
public partial class Controls_udc : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
private bool MakeVisible(object sender, EventArgs e)
{
return (labelComments.Visible == true) && (textComments.Visible == true);
}
code behind for .aspx page:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
Controls_udc.MakeVisible();
}
}
The compiler error I receive is Compiler Error Message: CS0117: 'Controls_udc' does not contain a definition for 'MakeVisible'.
I believe I am on the right track but just need a couple pointers.
MakeVisible method must be public but in your code it is private. It should be changed.

Button clicked in Site.Master page fires click event in content page.

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");
}

Same method for all new Page's with override global method / event?

I have question from ASP.NET
on every aspx page I need to have similar action / method and I don't want replicate code.
My method is checking if user has Session, if he doesn't have - browser will go to Login Page.
Maybe I should use some global method?
public partial class FirstPage : Page
{
protected void Page_Init(object sender, EventArgs e)
{
...
}
protected void Page_Load(object sender, EventArgs e)
{
sessionHelper = new SessionHelper();
if (sessionHelper.isUserLogged())
{
Response.Redirect("Login.aspx");
}
}
}

Event Handler is Always null

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?

Updating parent page from dynamically loaded user control

Hosting page:
protected void Page_Load(object sender, EventArgs e)
{
LoadMyControl(Parameters); //Do it every page load to preserve it's state
}
protected void LoadMyControl(string parameters)
{
plchld.Controls.Clear();
Control userControl = LoadControl("TheUserControl.ascx");
userControl.ID = "userControl1";
plchld.Controls.Add(userControl);
}
Now inside this control, when a button is clicked I want to update ,let's say a Label on the hosting page.
What is the best way to do it? Custom event?
Solution maybe:
In the parent page add:
public Label Actions
{
get { return _Actions; }
set { _Actions = value; }
}
on User Control add the following to the top
<%# Reference Page="../parentpage.aspx" %>
and to change the value just do the following in the user control
ASP.parentpage cc = (ASP.parentpage)this.Parent.Page;
cc.Actions.Text = "hello world";
A UserControl has a Page property which is a reference to the page in which the control is added. You could either cast directly to the known page type that contains the label,
e.g.
protected void Button1_Click(object sender, EventArgs e)
{
var page = Page as _Default;
if (page != null)
{
page.Label1.Text = "Hello World";
}
}
but what would probably be better, is that you decorate you pages where this control can be used with an interface:
public interface ILabelHost
{
void SetLabel(string text);
}
public class _Default : System.Web.UI.Page, ILabelHost
{
...
That way you can use that same logic from before, but on anypage the control is loaded and implements the correct interface:
protected void Button1_Click(object sender, EventArgs e)
{
var host = Page as ILabelHost;
if (host != null)
{
host.SetLabel("Hello World");
}
}

Categories