How can I hide some not virtual properties of parent class in PropertyGridControl without using [Browsable(false)]? I can not use this, because I can not override not virtual properties.
If you cannot override the property then you can use new modifier.
Here is example:
public class SomeChildClass : SomeParentClass
{
[Browsable(false)]
new public TypeOfProperty SomeProperty
{
get { return base.SomeProperty; }
set { base.SomeProperty = value; }
}
}
Related
I have following logic in Admin screen. I need similar logic in Logs screen also. Hence I am planning to move this logic into base page. In base page, how do I recognize the current page? (How do I distinguish between Admin screen and Logs screen?).
Based on the page the value retrieved from the config is different.
What are the different ways to achieve this? What is the best way out of these approaches?
//Admin Screen
List<string> authorizedRoles = new List<string>((ConfigurationManager.AppSettings["AdminScreenRoles"]).Split(','))
if (!authorizedRoles.Contains(userRole))
{
Response.Redirect("UnauthorizedPage.aspx");
}
//Logs Screen
List<string> authorizedRoles = new List<string>((ConfigurationManager.AppSettings["LogsScreenRoles"]).Split(','))
if (!authorizedRoles.Contains(userRole))
{
Response.Redirect("UnauthorizedPage.aspx");
}
Don't put code in base that recognize the class that inherit it. Add an abstract property that the child will have to override.
In base:
public abstract string AppSettingsRolesName { get; }
List<string> authorizedRoles = new List<string>((ConfigurationManager.AppSettings[AppSettingsRolesName]).Split(','))
if (!authorizedRoles.Contains(userRole))
{
Response.Redirect("UnauthorizedPage.aspx");
}
In Logs:
public override string AppSettingsRolesName
{
get { return "LogsScreenRoles"; }
}
In Admin:
public override string AppSettingsRolesName
{
get { return "AdminScreenRoles"; }
}
The easiest way would be to look into forms authentication, as it will handle all of this for you through a configuration file. There are a number of good articles on this dotted around the web - here's one:
http://ondotnet.com/pub/a/dotnet/2003/01/06/formsauthp1.html
However, if you're looking for a quick fix, the easiest way is to move your code into the base page as you said, and use an interface property to make inherited pages indicate what role type to use - e.g. something along the lines of:
public abstract class BasePage : Page
{
protected abstract string AuthorisedRoles { get; }
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
List<string> authorizedRoles = new List<string>((ConfigurationManager.AppSettings[this.AuthorisedRoles]).Split(','));
if (!authorizedRoles.Contains(userRole))
{
Response.Redirect("UnauthorizedPage.aspx");
}
}
}
public class LogsPage : BasePage
{
protected override string AuthorisedRoles
{
get { return "LogsScreenRoles"; }
}
}
public class AdminPagePage : BasePage
{
protected override string AuthorisedRoles
{
get { return "AdminScreenRoles"; }
}
}
But seriously, look into forms authentication if you want to do it properly - it's not as complicated as it first looks.
Assume i have a costum actionscript class.
public class myClass
{
private var myVariable:ArrayCollection;
...
}
Suppose also that i have a different class, that changes a second variable, which has the metadatatag [Bindable]. What methods and events do i have to implement in either of these classes, to make myVariable change, whenever the other is changend?
If you make the myVariable public, then you can just use [BindingUtils.bindProperty()][1]:
public class MyClass
{
public var myVariable:ArrayCollection;
public function MyClass(other:OtherClass) {
BindingUtils.bindProperty(this, "myVariable", other, "propertyName");
}
}
If you prefer to keep myVariable private, then you can use [BindingUtils.bindSetter()][2]:
public class MyClass
{
private var myVariable:ArrayCollection;
public function MyClass(other:OtherClass) {
BindingUtils.bindSetter(
function(newVal:*):void {
this.myVariable = newVal;
}, other, "propertyName");
}
}
class MyListControl : DropDownList {
public override object DataSource {
get { return Helper.GetDictionary(); }
set { }
}
public override string DataTextField {
get { return "Key"; }
set { }
}
public override string DataValueField {
get { return "Value"; }
set { }
}
public override void DataBind() {
if (Helper.ConditionSatisfied) {
base.DataBind();
}
}
}
Does ASP.NET ever need to use the set_DataSource()? And are there any references to when get_DataSource() might ever be called more than once (iterating over a collection when the iterator may not be resettable)?
Thank you.
I'm not sure what you mean by does it ever use set_DataSource?
Of course it does for example:
myDropDownBox.DataSource = MyBLLayer.GetSomeDataSet(SomeParams);
myDropDownBox.DataBind();
In the above example MyBLLayer is simply a static class that calls a method which returns some sort of dataset. That dataset could be a List for example.
RequiresDataBinding is protected in the base class hence no overriding of this property. You would not want to override this property in any case as its values are simply True or False.
I can't seem to find any examples on how to implement ITemplates with multiple instances. All I need to do is build out a navigation that has a template for the content of each navigation item.
If your control is not a databound control, you can solve the problem by something as follows. But, I haven't tested it.
public class FooItem : WebControl, INamingContainer
{
protected override CreateChildControls()
{
Control placeHolder = this.FindControl(((Foo)this.Parent).ItemPlaceHolderId);
if (placeHolder != null)
{
// Add some controls to the placeHolder.
}
}
}
public class Foo : WebControl, INamingContainer
{
public ITemplate ItemTemplate { get; set; }
public string ItemPlaceHolderId
{
get { ... }
set { ... }
}
public FooItemCollection Items
{
get { ... }
}
protected override CreateChildControls()
{
foreach (FooItem item in this.Items)
{
this.ItemTemplate.InstantiateIn(item);
}
}
}
Look at documentation of creating a data-bound templated control.
Unfortunately, the best documentation I've found is from .NET 1.1:
Developing a Templated Data-Bound Control.
Note from MSDN:
This TemplateInstanceAttribute class
is optional. If a template property is
not extended with a
TemplateInstanceAttribute class, the
default value, the Multiple field, is
used.
So any ITemplate example that does not use the TemplateInstanceAttribute is using TemplateInstance.Multiple.
I have a class like this. Property "isPag" is based on filed "ecboardid", I found that when ecboardid is changed, UI controls seem not be able to detect that "isPag" is also changed. So, how to make a property like this bindable?
[Bindable]
public class Encoder extends EventDispatcher
{
public var ecboardid : String;
/*-.........................................Methods..........................................*/
public function copyFrom(newEncoder:Encoder):void
{
ecboardid = newEncoder.ecboardid;
this.dispatchEvent(new Event('isPagChanged'));
}
[Bindable (event="isPagChanged")]
public function get isPag():Boolean
{
if(this.ecboardid != null)
{
if(this.ecboardid.search('xxx') != -1)
{
return false;
}
return true;
}
else
{
return false;
}
}
}
Edit:
If I change the property to a static function,
[Bindable]
public class Encoder extends EventDispatcher
{
public var ecboardid : String;
/*-.........................................Methods..........................................*/
public function copyFrom(newEncoder:Encoder):void
{
ecboardid = newEncoder.ecboardid;
this.dispatchEvent(new Event('isPagChanged'));
}
public static function isPag(String ecboardid ):Boolean
{
if(ecboardid != null)
{
if(ecboardid.search('xxx') != -1)
{
return false;
}
return true;
}
else
{
return false;
}
}
}
Will bind like this :
visible = {Encoder.isPag(encoder.ecboardid)}
work? Will visible change when encoder.ecboardid change?
I don't believe you can make read only properties Bindable, since Flex uses calls to the setter method for a property to issue the change events. Take a look here at the section on binding to properties of objects.
You need to have a setter as well as a getter.
Also, you have the class marked as bindable, if you weren't specifying the event name, then the [Bindable] on the method would be redundant.
Usually the compiler complains if you have just a getter and no setter and try to mark it [Bindable]
Code like this:
visible = {Encoder.isPag(encoder.ecboardid)}
Isn't how binding works. The {var} notation is for use in MXML. And isPag isn't static, so you can't refer to it as a class property. You need an instance of the Encoder class for this to be legal.
This is a simple change - just create getters/setters and dispatch your change event in the setter (after changing the value), like this:
private var _ecboardid : String;
public function get ecboardid():String
{
return _ecboardid;
}
public function set ecboardid(value:String):void
{
_ecboardid = value;
this.dispatchEvent(new Event('isPagChanged'));
}