Assign Master Page to Nested Master Page from Code Behind - nested

I have a parent MasterPage (Main.master) and a child NestedMasterPage
In the NestedMasterPage i have assigned the MasterPageFile property in the html code like below
<%# Master Language="C#" AutoEventWireup="true" MasterPageFile="~/Themes/ABC/Main.Master" CodeBehind="User_Main.master.cs" Inherits="ProjectABC.UI.Pages.MasterPages.User_Main" %>
Now in the code behind of the NestedMasterPage i am trying to change the MasterPageFile property to some other MasterPage but its not working.
protected void Page_PreInit(object sender, EventArgs e)
{
MasterPageFile = "~/Themes/XYZ/Main.Master";
}
No code error, just the HTML assigned path is working and not the code behind. What am i missing???

The reason behind this issue is that the PreInit() event of a master page file is never called in the asp.net page life cycle. You can see the complete set of master and contebnt page events here.
You can set the master page of a child master page in the content page's PreInit() event in the following way:
public partial class TestMaster : System.Web.UI.Page
{
protected void Page_PreInit(object sender, EventArgs e)
{
//Set the master page file of the current pages master.
this.Master.MasterPageFile = "~/NewMasterPage.master";
}
}

Related

Handling Page KeyUp event

I'm writing C#/XAML UWP App.
I want to handle KeyDown event for a whole page in my app. That is, no matter what specific control on the page has focus (e.g. a TextBox, a ListView, etc. ), whenever user presses a key while on that page, I want global for the whole Page KeyDown event to be fired. In theory this should be simple - subscribing to KeyDown event when the page is navigated to or loaded, for example:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
KeyDown += SettingsPage_KeyDown;
}
In practice this does't work for all the pages, even for quite simple ones and I can't understand why. I hooked up Window.Current.CoreWindow.KeyDown event which works always and properly,
but I'd like to know what's wrong with Page's KeyDown event. Obviously there may be hundredts of reasons why this doesn't work, but are there any common? I tried setting focus to page (Programmatic, Keyboard) and still
there seems to be no rule when this event works and when it doesn't.
I am using the next code in an application for Windows IoT, and it works:
public sealed partial class YourPage: Page
{
public YourPage() //Constructor
{
this.InitializeComponent();
//Add this line
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
}
void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
//todo
}
}
You could also add the KeyDown attribute in the XAML:
<Page
x:Class="WESS1.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WESS1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" KeyDown="SettingsPage_KeyDown">
And in the code behind file:
public sealed partial class SettingsPage: Page
{
public SettingsPage() //Constructor
{
this.InitializeComponent();
}
private void SettingsPage_KeyDown(object sender, KeyRoutedEventArgs e)
{
// e.g. check the value of e.Key
}
}

Page load of user control and parent page

I have a user control for showing message of actions. It is hidden on page load of user control, so that it disappears once a warning is acted upon. But in certain cases when a page loads, i want the user control to be visible, but doesn't due to hiding in page load of user control. How can I manage this?
usercontrol
protected void Page_Load(object sender, EventArgs e)
{
this.Visible = false;
}
public void SetMessage(string title, string desc)
{
this.Visible = true;
Title = title;
Description = desc;
}
parent page
protected void Page_Load(object sender, System.EventArgs e)
{
msgDialogue.SetMessage(a);
}
From what I understood, you need to show or hide a usercontrol depending on a property value in your page (or in a session variable). If this is the case, you would better add a placeholder in your page, then load the control (or not load it) depending on your condition.
if(condition)
{
_usercontrol ctrl = LoadControl("~/usercontrols/_usercontrol.ascx") as _usercontrol;
Placeholder1.Controls.Add(ctrl);
}
//otherwise, do not load the control
Hope it helps.

mdicontainer overlap the childcontrol

In my application I have a main page with a menustrip to navigate to other forms Since i want it to be opened always I set it as a MDICONTAINER .now I need to open a form on menustripclick event as child i set the form like this
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
this.IsMdiContainer = true;
loginmain();
intialsetupform();
}
private void companyToolStripMenuItem_Click(object sender, EventArgs e)
{
Master.CompanyMasterForm cmpnymasterform = new Master.CompanyMasterForm();
cmpnymasterform.MdiParent= this ;
cmpnymasterform.Show();
}}
All was Ok and I got the childwindows work correctly
but Now i need to add two panels in the mainform << one for showing status meddages and other for showing some other controls
I had added the panel and anchored them and the issue is that now when the child window opens all the panels and controls of the parentform(mainform) comes above the childwindow controls which make it unreadable Please provide any ideas to overcome this
I had set the location of all the pannels using Anchor properties of the control now the controls know there location
and set the message box to bottom using dock property

C# Webbrowser control - Defining document loaded

DocumentCompleted will work after all dom elements loaded (images, flash and etc.). And I am waiting when all dom elements are downloaded to work this document. How can I define when documnet (page) loaded (when client see the page)? Is there any event to define when document LOADED to client?
Why not let the page raise an event? For instance, something like:
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace TestWebBrowser
{
[ComVisible(true)]
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, System.EventArgs e)
{
webBrowser1.ObjectForScripting = this;
webBrowser1.DocumentText = #"
<html>
<body onLoad='jscript:window.external.DocumentLoaded();'>
<img src=""http://www.nasa.gov/images/content/464377main_image_1695_1600-1200.jpg"" />
</body>
</html>";
}
public void DocumentLoaded()
{
MessageBox.Show("Document Finished Loading.");
}
}
}
In the above sample the page uses the onLoad() event to notify the form when the page has finished loading.
Hope this helps.

How To Make UserControl In MasterPage Public For All Child Pages

I have a UserControl which I have added to my web.config
<add tagPrefix="BCF" src="~/controls/MyMessageBox.ascx" tagName="error"/>
and added to my master page
<BCF:error ID="BCError" runat="server" Visible="false" />
Now I need to be able to reference this control AND its public properties from all child pages that use that masterpage. I did this is my BasePage OnLoad event
public UserControl BCError;
BCError = (UserControl)Master.FindControl("BCError");
Problem is, although I can do this in the .aspx page
BCError.Visible = true;
I cannot reference any of the Controls properties I have put in? Such as ShowError .. If I do
BCError.ShowError = "Error Message";
I just get an error saying
'System.Web.UI.UserControl' does not contain a definition for 'ShowInfo' and no extension method 'ShowInfo'
Can you please point me in the right direction!
This is the code for the user control... I can use the properties in the masterpage code behind (And in a page if I put the control directly into it) but cannot use them in the child page code behind?? It doesn't even show the properties or wrapper methods in the intellisense?
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class MyMessageBox : System.Web.UI.UserControl
{
#region Properties
public bool ShowCloseButton { get; set; }
#endregion
#region Load
protected void Page_Load(object sender, EventArgs e)
{
if (ShowCloseButton)
CloseButton.Attributes.Add("onclick", "document.getElementById('" + MessageBox.ClientID + "').style.display = 'none'");
}
#endregion
#region Wrapper methods
public void ShowError(string message)
{
Show(MessageType.Error, message);
}
public void ShowInfo(string message)
{
Show(MessageType.Info, message);
}
public void ShowSuccess(string message)
{
Show(MessageType.Success, message);
}
public void ShowWarning(string message)
{
Show(MessageType.Warning, message);
}
#endregion
#region Show control
public void Show(MessageType messageType, string message)
{
CloseButton.Visible = ShowCloseButton;
litMessage.Text = message;
MessageBox.CssClass = messageType.ToString().ToLower();
this.Visible = true;
}
#endregion
#region Enum
public enum MessageType
{
Error = 1,
Info = 2,
Success = 3,
Warning = 4
}
#endregion
}
Ok I think I reproduced roughly what your describing and I deleted my original answer cause it was way off.
What I found is that when you want a content page to reference a user control being used in a master page and the control is accesible and what not, you will get an error indicating that you need to reference a specific assembly, and then you get errors indicating that no Method exists of type such and such.
By adding the Register page directive on the child page to the user control resolved this issue. I reproduced this even with the control defined in the web.config or on the page. In both cases I still had to explicitly add a Register on the content page.
This doesn't make sense to me but it allowed my code to compile and work. Give it a shot let me know.
Once you do this you can reference the control like
this.Master.MessageBox.ShowInfo();
This assumes that you have a public property called MessageBox on the Master Page.
Edit
I've also found that this works much better if you register the control on both the master and the content page and not use the web.config.
Edit
If you don't want your child page to reference the user control your other option is expose methods on the master page like ShowInfo() which would delegate to the user control.
You need to declare it as your control type to access it's properties.
public MyMessageBox BCError;
BCError = (MyMessageBox )Master.FindControl("BCError");
Try using this in the pages that inherit from your master page:
<%# MasterType VirtualPath="~/MasterPageName.Master" %>

Resources