Overriding Enabled property in a custom CheckBox - asp.net

I have a custom CheckBox class. I have overrided getters for Enabled and Visible property (I need some complicated behavior).
When I the run application and debug it I found that my Enabled's getter is not called at all. At the same time the Visible property is called correctly, and I get the desired result.
public class CustomCheckBox : CheckBox
{
public override bool Visible
{
get
{
bool result;
//Do something
return result;
}
set
{
base.Visible = value;
}
}
public override bool Enabled
{
get
{
bool result;
//Do something
return result;
}
set
{
base.Enabled = value;
}
}
}
I have some suspicions about it. Maybe during the render stage CheckBox does not call the Enabled property, but looks for the needed attribute within InputAttribute collection. I'm not sure and do not know how to determine it.

Related

Why does setting IsFocused in ViewModel not focus the Entry in the View

It seems like the ViewModel gets an update of the Focused state of an Entry (based on user interaction) but setting a value on the bound property in the ViewModel has no effect.
I have this in my View:
<Entry IsFocused="{Binding Focused, Mode=TwoWay}"/>
And this in my ViewModel:
public bool Focused
{
get
{
return _focused;
}
set
{
_focused = value; // This gets invoked on user interaction with UI, but setting it programatically has no effect on the Entry's focus.
OnPropertyChanged(nameof(Focused));
}
}
Why is the above not working for me?
Is it because IsFocussed is read-only? If so, why am I even allowed to specify Mode=TwoWay?
public bool IsFocused
{
get { return (bool)GetValue(IsFocusedProperty); }
}
You guess right: the IsFocused property is indeed readonly.
They indeed could throw an exception in a setter, but design decisions are tough and must be coherent between all ui elements.

System.Dynamic ExpandoControl is it possible?

I'm trying to figure out how to create a web server control which is basically an ExpandoObject.
The desire is to automatically create a property on the control when it is created in the aspx markup.
For example:
<x:ExpandoControl someProperty="a value"></x:ExpandoControl>
Where the someProperty attribute does not yet exist as a property on the control.
I should also mention that I don't strictly need any functionality of Control or WebControl. I just need to be able to declare it in markup with runat="server" (which in and of itself may require it to be a control, at least that's what I'm thinking).
Is it possible? If so how can I get started?
Many thanks.
I think your first bet would be to implement IAttributeAccessor:
public interface IAttributeAccessor
{
string GetAttribute(string key);
void SetAttribute(string key, string value);
}
The ASP.NET page parser calls IAttributeAccessor.SetAttribute for each attribute it cannot map to a public property.
So perhaps you can start with
public class ExpandoControl : Control, IAttributeAccessor
{
IDictionary<string, object> _expando = new ExpandoObject();
public dynamic Expando
{
{
return _expando;
}
}
void IAttributeAccessor.SetValue(string key, string value)
{
_expando[key] = value;
}
string IAttributeAccessor.GetValue(string key)
{
object value;
if (_expando.TryGetValue(key, out value) && value != null)
return value.ToString();
else
return null;
}
}

Get and Set not working with List<>

Im trying to make a get and set to make my List<> persistent. I think my intention is clear with the code. I want the List to remain throughout postbacks so i dont loose data in it.
What am i doing wrong? Thanks to my AlertPopUp i can see that the Get is triggered, but never the Set. So the List comes back as containint zero items when it should have several items.
private List<string> accountIDsSelectedForDeletion = new List<string>();
public List<string> AccountIDsSelectedForDeletion
{
get {
if (ViewState["AccountIDsSelectedForDeletion"]!= null)
{
accountIDsSelectedForDeletion = ViewState["AccountIDsSelectedForDeletion"] as List<string>;
AlertPopUp.QuickDebugMessage("getting list from viewstate. Count: "+ accountIDsSelectedForDeletion.Count);
}
AlertPopUp.QuickDebugMessage("returning list");
return accountIDsSelectedForDeletion;
}
set {
AlertPopUp.QuickDebugMessage("setting list to viewstate. Count: " + accountIDsSelectedForDeletion.Count);
accountIDsSelectedForDeletion = value;
ViewState["AccountIDsSelectedForDeletion"] = accountIDsSelectedForDeletion;
}
Could it be because i change the List by its Add() method?
In a nutshell, yes. .Add() is mutating the list, while the Set property acts on the list as a set piece.
To accomplish what your trying to do here, I would create an inherited type from List, so that I could inject code into the Add Remove (ext, there are more) methods. Like:
//not syntatically correct sorry
public class MyList : List<string>
{
public override Add(string NewItem)
{
//do a extra peristance step here
//
me.Add(NewItem);
}
}
Or have a class that wraps a private List<> and manages the Add/Remove methods, maybe like:
public class MyList
{
private List<string> listItems { get; set; }
//don't expose the list in a mutable state, IEnumerable instead
public IEnumerable<string> Items { get { return this.listItems; } }
public void Add(string NewItem)
{
this.ListItems.Add(NewItem);
}
}
Then just replace List with this type on your page level class.
By the way, persisting to the ViewState bloats the amount of information you have to send up and down the wire per page load. Have you considered Session, or the Cache object?

Required Property in Custom ASP .NET Control

I am making a custom web control for my ASP page that inherits from CompositeDataBoundControl. I have a public property in the definition of my control that is required, if the user does not provide this property in the control definition on an ASP page it will break and we will get an exception. I want the compiler to throw a warning similar to the one when a user forgets to provide the 'runat' property of a Control.
"Validation (ASP.Net): Element 'asp:Button' is missing required attribute 'runat'."
Here is basically what my code looks like:
public class MyControl : CompositeDataBoundControl, IPostBackEventHandler
{
private string _someString;
public string SomeString
{
get { return _someString; }
set { _someString = value; }
}
// Other Control Properties, Functions, Events, etc.
}
I want "SomeString" to be a required property and throw a compiler warning when I build my page.
I have tried putting a Required attribute above the property like so:
[Required]
public string SomeString
{
get { return _someString; }
set { _someString = value; }
}
but this doesnt seem to work.
How can I generate such a compiler message?
Thats quite simple, on page load you can check if the property has some value or not. You can check it for Null or Empty case depending on your property type.
like if i have this
private string _someString;
public string SomeString
{
get { return _someString; }
set { _someString = value; }
}
On page_load event i will check if
if(_someString != null && _someString != "")
{
String message = "Missing someString property";
isAllPropertySet = false; //This is boolean variable that will decide whether any property is not left un-initialised
}
All The Best.
and finally

User Control not getting declarative property value

I have a user control on a web form that is declared as follows:
<nnm:DeptDateFilter ID="deptDateFilter" runat="server" AllowAllDepartments="True" />
In the code-behind for this control, AllowAllDepartments is declared as follows:
internal bool AllowAllDepartments { get; set; }
Yet when I view the page, and set a breakpoint in the control's Page_Load event handler, my AllowAllDepartments property is always false. What are possible reasons for this?
BREAKING NEWS: Even setting the property programmatically has no effect on the property value when I hit my breakpoint in Page_Load of the control. Here is the Page_Load of the host page:
deptDateFilter.FilterChanged += deptDateFilter_FilterChanged;
if (!IsPostBack)
{
deptDateFilter.AllowAllDepartments = true;
PresentReport();
}
Try adding the property value to the ViewState:
protected bool AllowAllDepartments
{
get
{
if (ViewState["AllowAllDepartments"] != null)
return bool.Parse(ViewState["AllowAllDepartments"]);
else
return false;
}
set
{
ViewState["AllowAllDepartments"] = value;
}
}
EDIT
Furthermore, you may want to handle the control's PreRender event, to see whether the the control's property has been correctly set there or not.
Make the property bindable like:
[Bindable(true), Category("Appearance"), DefaultValue(false)]
internal bool AllowAllDepartments { get; set; }
Just out of curiosity... does it work OK if you don't use the get;set; shortcut?
private bool _allowAllDepartments;
public bool AllowAllDepartments
{
get { return _allowAllDepartments; }
set { _allowAllDepartments = value;}
}
Have you tried making the property public?

Resources