How To Compare My Actual MasterPage - c#

I have 2 MasterPage in my project.
The MasterPage to common pages and the MasterPage to PopUp pages.
And I have a class BasePage that is inherited by all pages, and in BasePage I need to verify which is the actual MaterPage that is used.
Ex:
if(Master.GetType() == typeof(Master) ....
How do I test it?

The is operator is handy for checking types.
If the two masters (I will call them MasterPage and MasterPagePopup) are inherited form a common ancestor (Page?) and not one another, you could do something like this:
if(Master is MasterPage)
{ do some stuff; }
if(Master is MasterPagePopup)
{ do other stuff; }
The only gotcha is if one master is inherited from the other; if MasterPagePopup is inherited form MasterPage, then both cases above would be true for MasterPagePopup as he IS both a MasterPage and MasterPagePopup . However, if...else if would solve this:
if(Master is MasterPagePopup)
{ do other stuff; }
else if(Master is MasterPage) // popup is already handled and will not hit this
{do some stuff; }

The easiest way to check the type of your MasterPage is with the is keyword:
if (this.Master is MasterPageCommon) {
} else if (this.Master is MasterPagePopup) {
}

You should just be able to do
if(page.Master is PopUpMaster)
{
//Do Something
}
else if (page.Master is NormalMaster)
{
//Do Something
}

Related

Method with generic and multiple parameters

I want to enable/disable controls in a Windows Forms application according to the user privileges.
Initially I thought of writing a method in each form class that would check the user credentials and then enable/disable its controls. But then I realized I could (maybe) create a static class method which would take the form as a parameter and do the job.
So I started writing it, presuming that sometimes I would like to enable the controls of just one or two panels, instead of the whole form. So, I need the parameters to be:
a varying number of panels and/or
a form class.
My difficulties with this task is that I'm getting an error trying to make the panels argument varying, and I have no idea how to set a parameter that could take any form class. All my form classes obviously inherits from Form generic class, but I don't know how to apply this.
Here's what I got:
public static void Enable(TableLayoutPanel[] containers = null)
{
if (MyOF.isEnabled)
{
return;
}
else
{
try
{
foreach (TableLayoutPanel table in containers)
{
foreach (Control control in table.Controls)
{
control.Enabled = false;
}
}
}
catch (NullReferenceException)
{
}
}
}
If we remember that the Form class derives from Control (indirectly, by deriving from ContainerControl which derives from ScrollableControl, which derives from Control), and the Enabled property belongs to the Control class, we can write a method that will enable any control's children (including the Form or TableLayoutPanel controls), since the Controls collection also belongs to the Control class:
public static void EnableChildren(Control control, bool enabled = true)
{
foreach (Control child in control.Controls)
{
child.Enabled = enabled;
}
}
And then if we also want to be able to use this with a collection of controls (as in your example), we can write an overload that takes a collection:
public static void EnableChildren(IEnumerable<Control> controls = null,
bool enabled = true)
{
if (controls == null) return;
foreach (var control in controls)
{
EnableChildren(control, enabled);
}
}
Now we can use this with a Form or a collection of TableLayoutPanel controls (or any control that has controls in it's Controls collection).
Examples of usage:
var myForm = new Form1();
EnableChildren(this); // 'this' is the current form
EnableChildren(myForm); // a separate instance of a form control
EnableChildren(tableLayoutPanel1, false); // A single TableLayoutPanel control
var tableLayoutPanels = new [] {tableLayoutPanel1, tableLayoutPanel2, tableLayoutPanel3};
EnableChildren(tableLayoutPanels); // An array of tableLayoutPanel controls
One of the simple ways I can think about what you are trying to do, is this. Let me get away for a sec here. I worked on projects where all form controls were built from Metadata. And meta came with licensing info. So, when control was placed where it should, it also was disabled or set read-only based on Metadata but the whole feature would be hidden if licensing info was restricting access to it. Coming back to your approach, this is not a bad approach and I see that this is can be done. And it can be done in 2 ways, (quickly from my head).
Use user controls as surface for the components you want to enable/disable. Create an interface
public interface IDisableableControl // make your fine name, no methods needed - marker interface
. . . . .
public class MyFineUserControl : UserControl, IDisableableControl
And in your static method that you're going to write pass the form, and find all controls that implement this interface and work them the way you want.
2.
Similarly, you can use property Tag, which is available on each control. With that, you can actually set your complex security object that can come from DB-stored metadata and then you evaluate this object stored in Tag to apply your configuration
Your method needs to be recursive
internal static void SetAllControls(Control parent)
{
// Do something with control, for example parent.Enabled = false
if (parent is IDisableableControl)
{
// here you use your logic, evaluate your parent you're dialing with and
// enable/disable correspondingly
parent.Enabled = false;
return;
}
foreach(var c in parent.Controls)
SetAllControls(c);
}
In real life, your TOP parent will be a form and will not need to be disabled, but it's certain children will. In fact, most of the time, once you found a UserControl which implements IDisableableControl that should be end of line, means, you don't need to go into children controls as they all sit on this parent and all will be disabled
I manage to accomplish what I was trying to do with the code below, which is pretty much a blend of all the helpful answers I got:
public static void EnableContainer(params Control[] containers)
{
if(containers.Count() == 0) { return; }
if (MyOF.isEnabled)
{
return;
}
else
{
try
{
foreach (var container in containers)
{
foreach (Control control in container.Controls)
{
control.Enabled = false;
}
}
}
catch (NullReferenceException)
{
}
}
}
public static void EnableForm<form>(form f) where form : Form
{
if (MyOF.isEnabled)
{
return;
}
else
{
foreach(Control control in f.Controls)
{
control.Enabled = false;
}
}
}
The community is welcome to suggest improvements as I am far from being a professional programmer. Thanks everyone once again.

Xamarin Forms: MasterPage Helper class

My current app has multiple master detail pages. I want to create a helper class which has a function that accepts list of PageModels-Pages( ViewModels-views ) which I can iterate through and create master detail pages.
MyCurrent Code:
public static Page SetupMasterDetailNav<T,U>( Dictionary<T,string> Menu)
where T : class
//In Dictionary T is ViewModel(PageModel) ,
String is name displayed on Master page
{
var masterDetail = new FreshMasterDetailNavigationContainer();
foreach (KeyValuePair<T,string> item in Menu)
{
masterDetail.AddPage<item.Key>(item.Value);
}
masterDetail.Init("");
return masterDetail;
}
This code doesn't work.It tells me item.key is a variable and cannot be used as a type Can any one suggest me a better approach or how else I can achieve my goal ?
The AddPage<T> method is a generic method, which expects a type. In this case it is FreshBasePageModel. The normal usage would be something like:
masterDetail.AddPage<MyViewModel>("MyPage", model);
or:
masterDetail.AddPage<MyViewModel>("MyPage");
Since your method is already generic, and seems you want it to be the type of the ViewModel you could simply do:
masterDetail.AddPage<T>(item.Value);
To do this you must change the your method signature to something like:
public static Page SetupMasterDetailNav<T,U>(Dictionary<T,string> Menu)
where T : FreshBasePageModel
Not sure what the U is used for in your case, you haven't shown usage of it.
Why are you even doing this is puzzling me.

How to determine the calling page in user control

I have a user control which is in common in two asp.net pages how to find out the parent page i dont want to use the page's title is there any concrete way to determine may be a page's class or any way else
I would like to do this
if (this.page == "pagename")
{
//do this
}
else
{
//do this
}
You can use this.Page in order to find the current page and use this.Page.GetType() if you are expecting class name
You can use:
Page myParentPage = this.Page;
In your method.
Try this:
if (this.Page.GetType().Name.ToLower() == "pagename".Replace(".", "_").ToLower())
{
//do this
}
else
{
//do this
}

Deciding on User Control to Use at Runtime

I have 2 user controls defined on a page:
<%# Register Src="Foo.ascx" TagName="FooControl" TagPrefix="acme" %>
<%# Register Src="Bar.ascx" TagName="BarControl" TagPrefix="acme" %>
.
.
.
<acme:FooControl ID="myFoo" runat="server" Visible="false" />
<acme:BarControl ID="myBar" runat="server" Visible="false" />
At runtime, I'd like to set one of the user control's properties in various locations in the page's code. For example:
protected void SomeMethod()
{
if (isSomeCondition)
{
myFoo.Visible = true;
}
else
{
myBar.Visible = true;
}
// ...
if (somethingElse)
{
if (isSomeCondition)
{
myFoo.Prop1 = 123;
}
else
{
myBar.Prop1 = 123;
}
}
// ...
}
I know that I can have the 2 user controls inherit from a common Interface, but is there another (possibly better) way?
EDIT: I just realized most of my answer was already covered by the comments to the same question. Apologies to the people who commented, I wasn't "stealing" your content intentionally... :)
no, I can think of different ways to achieve the same result (calling properties via reflection or working out something with FindControl) but I can't think of any better way than having both your controls implement the same interface.
You could then access the active control via another property, for instance:
public IMyControl ActiveControl
{
get
{
return (isSomeCondition)? myFoo : myBar;
}
}

ASP.NET Web Page Globalization & Localization in Master Page C# 3.0

I cant use the following code in Master page for Globalization & Localization. It gives the error as commented in the code part "does not contain a defination for InitializeCulture"
protected override void InitializeCulture()
{
if (Request["Language"] != null)
{
//String selectedLanguage = Request["Language"];
// code wil go here
}
base.InitializeCulture();
//base.InitializeCulture gives error as mentioned in the next line
//does not contain a defination for InitializeCulture
}
When i add this code to other pages other than Master Page it works fine. is there any restriction on using this code in Master Page.
If i am able to define this code in Master Page then i dont need to write this code in every file.
Am i doing something wrong, I have include File for threading and Globalization, Still it doesn't work in Master Page
You have to do this (= override InitializeCulture) in your Page class. It doesn't work in the master page (MasterPage is derived from Control, not from page). I would suggest that you implement a base class which is derived from Page and derive every web form from this class, then you also have to write the code only once. It is always handy to have your own base class.
In Visual Studio you add a new class PageBase.cs:
public class FormBase : Page
{
protected override InitializeCulture()
{
if (Request.Form["lbCulture"] != null)
{
String selectedLanguage = Request.Form["lbCulture"];
UICulture = selectedLanguage;
Culture = selectedLanguage;
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);
}
base.InitializeCulture();
}
}
The current culture is either stored in some dropdown listbox, in the session or passed by query string. I used a listbox in the sample.
And then you derive your WebForm from this page like this:
public class Default : FormBase // instead of deriving from Page

Categories