access WebControls markable properties in constructor asp net - asp.net

Here is my custom control.It inherits [Height] property from WebControl class.I want to access it in constructor for calculating other properties.But its value is always 0.Any idea?
public class MyControl : WebControl, IScriptControl
{
public MyControl()
{
AnotherProperty = Calculate(Height);
.......
}
my aspx
<hp:MyControl Height = "31px" .... />

Markup values are not available in your control's constructor but they are available from within your control's OnInit event.
protected override void OnInit(EventArgs e)
{
// has value even before the base OnInit() method in called
var height = base.Height;
base.OnInit(e);
}

As #andleer said markup has not been read yet in control's constructor, therefore any property values that are specified in markup are not available in constructor. Calculate another property on demand when it is about to be used and make sure that you not use before OnInit:
private int fAnotherPropertyCalculated = false;
private int fAnotherProperty;
public int AnotherProperty
{
get
{
if (!fAnotherPropertyCalculated)
{
fAnotherProperty = Calculate(Height);
fAnotherPropertyCalculated = true;
}
return fAnotherProperty;
}
}

Related

C#, Xamarin Forms: No Custom TextChangedEvent Raised on initialization

I'm creating an Xamarin.Forms MVVM App (only using Android) which needs certain buttons to be outlined red, whenever their text property holds a specific value. (Purpose: alert the user to press the button and select a value, which will change the Button Text Property and therefore remove the red outline)
To achieve this I've create the following documents:
A custom button CButton that extents the default Button:
public class CButton : Button
{
// this Hides the Default .Text-Property
public string Text
{
get => base.Text;
set
{
base.Text = value;
TextChangedEvent(this, new EventArgs());
}
}
// The Raised Event
protected virtual void TextChangedEvent(object sender, EventArgs e)
{
EventHandler<EventArgs> handler = TextChanged;
handler(sender, e);
}
public event EventHandler<EventArgs> TextChanged;
}
A custom behavior makes use of the raised TextChangedEvent
public class ButtonValBehavior : Behavior<CButton>
{
protected override void OnAttachedTo(CButton bindable)
{
bindable.TextChanged += HandleTextChanged;
base.OnAttachedTo(bindable);
}
void HandleTextChanged(object sender, EventArgs e)
{
string forbidden = "hh:mm|dd.mm.yyyy";
if (forbidden.Contains((sender as CButton).Text.ToLower()))
{
//Do when Button Text = "hh:mm" || "dd.mm.yyyy"
(sender as CButton).BorderColor = Color.Gray;
}
else
{
//Do whenever Button.Text is any other value
(sender as CButton).BorderColor = Color.FromHex("#d10f32");
}
}
protected override void OnDetachingFrom(CButton bindable)
{
bindable.TextChanged -= HandleTextChanged;
base.OnDetachingFrom(bindable);
}
}
The relevant parts of the ViewModel look the following:
public class VM_DIVI : VM_Base
{
public VM_DIVI(O_BasisProtokoll base)
{
Base = base;
}
private O_BasisProtokoll _base = null;
public O_BasisProtokoll Base
{
get => _base;
set
{
_base = value;
OnPropertyChanged();
}
}
Command _datePopCommand;
public Command DatePopCommand
{
get
{
return _datePopCommand ?? (_datePopCommand = new Command(param => ExecuteDatePopCommand(param)));
}
}
void ExecuteDatePopCommand(object param)
{
//launch popup
var p = new PP_DatePicker(param);
PopupNavigation.Instance.PushAsync(p);
}
}
The .xmal looks the following (b is the xmlns of the Namespace):
<b:CButton x:Name="BTN_ED_Datum"
Text="{Binding Base.ED_datum, Mode=TwoWay}"
Grid.Column="1"
Command="{Binding DatePopCommand}"
CommandParameter="{x:Reference BTN_ED_Datum}">
<b:CButton.Behaviors>
<b:ButtonValBehavior/>
</b:CButton.Behaviors>
</b:CButton>
This solution works fine whenever the input is caused by user interaction. However, when a Value is assigned during the initialization of the Page no red outline is created, in fact the TextChangedEvent isn't raised. By using breakpoints I noticed that during initialization the Text Property of CButton is never set, eventhough it actually will be in the view.
Despite fiddling around with my solution I cannot make this work on initialization. I tried to work around this issue by outlining every button by default in their constructor, however this will outline every button red, even when their text value doesn't require them to be.
How can I achieve my initial goal?
Many thanks in advance!
It's been a while but if I recall correctly what I ended up doing was:
Changing the new Text-Property of my custom Button to CText and
Making sure that I have Mode=TwoWay activated for any Element, that doesn't have it enabled by default. (Look up Binding modes on msdn for more)
making CText a bindable property of CButton
My custom button now looks the following:
using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;
namespace EORG_Anton.Model
{
public class CButton : Button
{
public static readonly BindableProperty CTextProperty =
BindableProperty.Create(nameof(CText),
typeof(string),
typeof(CButton),
default(string),
BindingMode.TwoWay,
propertyChanged: OnTextChanged);
private static void OnTextChanged(BindableObject bindable, object oldValue, object newValue)
{
var control = (CButton)bindable;
var value = (string)newValue;
control.CText = value;
}
public string CText
{
get => base.Text;
set
{
base.Text = value;
TextChangedEvent(this, new EventArgs());
}
}
protected virtual void TextChangedEvent(object sender, EventArgs e)
{
EventHandler<EventArgs> handler = TextChanged;
handler(sender, e);
}
public event EventHandler<EventArgs> TextChanged;
}
}

Why does setting BindableProperty.DefaultValue not call OnPropertyChanged?

I'm using a bindable property like this in a class the inherits from Xamarin.Forms.ContentView:
public static readonly BindableProperty OverlayColorProperty = BindableProperty.Create(nameof(OverlayColor), typeof(Color), typeof(MyControl), Color.FromHex("#55000000"));
public Color OverlayColor
{
get => (Color)GetValue(OverlayColorProperty);
set => SetValue(OverlayColorProperty, value);
}
Furthermore I'm listening for changes to update an inner elements background color:
protected override void OnPropertyChanged(string propertyName)
{
base.OnPropertyChanged(propertyName);
switch (propertyName)
{
case nameof(OverlayColor):
GridBackground.BackgroundColor = OverlayColor;
break;
}
}
I just noticed that OnPropertyChanged does not get called with the default value. Just when I update it from another place, XAML or through code.
Is this exspected behavior?
If yes, why? What should I do instead? Define it also in XAML code?

Simple databinding IN CODE to a DependencyProperty

My apologies as this is simplistic enough I know the question's been answered but in 30 or so pages, I've yet to find the boiled down problem I'm trying to solve.
I'm not yet well practiced in SL and trying a simple version of attempting to write a TextBox that binds to a property within the screen and updates it when Text is altered and vice versa (property change propagates to the Text). Due to a few reasons, I need to do this with DependencyProperties and in the codebehind rather than INotifyPropertyChanged and in XAML.
My latest attempts look something like this:
public partial class MainPage : UserControl
{
static MainPage()
{
TargetTextProperty = DependencyProperty.Register("TargetText", typeof(string), typeof(MainPage), new PropertyMetadata(new PropertyChangedCallback(TextChanged)));
}
public readonly static DependencyProperty TargetTextProperty;
public string TargetText
{
get { return (string)GetValue(TargetTextProperty); }
set { SetValue(TargetTextProperty, value); }
}
public MainPage()
{
InitializeComponent();
TargetText = "testing";
textBox1.DataContext = TargetText;
Binding ResetBinding = new Binding("TargetText");
ResetBinding.Mode = BindingMode.TwoWay;
ResetBinding.Source = TargetText;
textBox1.SetBinding(TextBox.TextProperty, ResetBinding);
}
private static void TextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
MainPage pg = (MainPage)sender;
pg.textBox1.Text = e.NewValue as string;
}
}
Anyone see what (painfully obvious thing?) I'm missing?
Thanks,
John
The following should be enough to set the binding you want:
textBox1.SetBinding(TextBox.TextProperty, new Binding() { Path = "TargetText", Source = this });
The problem with your code is that you set both Source and binding Path to the TargetText property and as a result you get the framework trying to bind to TargetText.TargetText, which is obviously wrong.

Add ability to provide list items to composite control with DropDownLIst

I'm creating a composite control for a DropDownList (that also includes a Label).
The idea being that I can use my control like a dropdown list, but also have it toss a Label onto the page in front of the DDL.
I have this working perfectly for TextBoxes, but am struggling with the DDL because of the Collection (or Datasource) component to populate the DDL.
Basically I want to be able to do something like this:
<ecc:MyDropDownList ID="AnimalType" runat="server" LabelText="this is what will be in the label">
<asp:ListItem Text="dog" Value="dog" />
<asp:ListItem Text="cat" Value="cat" />
</ecc:MyDropDownList>
The problem is, I'm not extending the DropDownList class for my control, so I can't simply work it with that magic. I need some pointers to figure out how I can turn my control (MyDropDownList), which is currently just a System.Web.UI.UserControl, into something that will accept List items within the tag and ideally, I'd like to be able to plug it into a datasource (the same functions that the regular DDL offers).
I tried with no luck just extending the regular DDL, but couldn't get the Label component to fly with it.
After doing some digging and searching I found a solution that works. Hopefully this will help someone else out in the future:
[ParseChildren(true, "Items")]
public class EDropDownList : CompositeControl, IValidatedFields
{
public string PromptingText { get; set; }
public string Value { get; set; }
public Label __Label { get; set; }
private ListItemCollection _items;
public DropDownList __DropDownList;
public ListItemCollection Items
{
get { return _items; }
set
{
if (_items != value)
{
_items = value;
}
}
}
public string Type { get { return "DropDownList"; } }
public EDropDownList()
{
__Label = new Label();
}
protected override void CreateChildControls()
{
__DropDownList = new DropDownList();
foreach (ListItem myItem in _items)
{
__DropDownList.Items.Add(myItem);
}
Controls.AddAt(0, __Label);
Controls.AddAt(1, __DropDownList);
}
protected override void OnLoad(EventArgs e)
{
// label section
__Label.Text = PromptingText+"<br />";
__Label.ForeColor = Color.Red;
__Label.Visible = false;
// ddl section
if (Page.IsPostBack)
Value = __DropDownList.SelectedValue;
}
}
The easiest thing would be to go back to your original option of extending the DropDownList control. What problems did you have getting the label to work with it? Those problems are probably easier to solve?

Possible to have a inner control on a custom server control?

I would like to be able to do something like:
<ui:Tab Title="A nice title">
<TabTemplate>
<asp:Literal runat="server" ID="SetMe">With Text or Something</asp:Literal>
</TabTemplate>
</ui:Tab>
but also be able to do:
<ui:Tab Title="A nice title">
<TabTemplate>
<asp:DataList runat="server" ID="BindMe"></asp:DataList>
</TabTemplate>
</ui:Tab>
Answer code I eventually came up with:
[ParseChildren(true)]
public class Node : SiteMapNodeBaseControl, INamingContainer
{
private ITemplate tabTemplate;
[Browsable(false),
DefaultValue(null),
Description("The tab template."),
PersistenceMode(PersistenceMode.InnerProperty),
TemplateContainer(typeof(TabTemplate))]
public ITemplate TabTemplate
{
get { return tabTemplate; }
set { tabTemplate = value; }
}
protected override void CreateChildControls()
{
if (TabTemplate != null)
{
Controls.Clear();
TabTemplate i = new TabTemplate();
TabTemplate.InstantiateIn(i);
Controls.Add(i);
}
}
protected override void Render(HtmlTextWriter writer)
{
EnsureChildControls();
base.Render(writer);
}
}
public class TabTemplate : Control, INamingContainer
{
}
The ParseChildren attribute tells .NET whether to treat your control's children as properties or as controls. For your first example, you want to treat children as controls, so add
[ ParseChildren(ChildrenAsProperties = false) ]
For the second, you want ChildrenAsProperties=true, and a TabTemplate property of type ITemplate. There's some plumbing involved after that, which this MSDN sample describes. It doesn't add a lot of value if you only need one template, though.

Resources