Setting property default values for a Web User Control - c#

I am trying to build a web user control and set some default values for its properties in the code-behind like this:
[DefaultValue(typeof(int), "50")]
public int Height { get; set; }
[DefaultValue(typeof(string), "string.Empty")]
public string FamilyName { get; set; }
[DefaultValue(typeof(Color), "Orange")]
public System.Drawing.Color ForeColor { get; set; }
When I add the user control to the page and call it without any properties:
<uc1:Usercontrol ID="uc" runat="server" />
the default values are not set and every property is 0 or null.
What am I doing wrong?
Thanks.

The DefaultValueAttribute will not set any value for your property, it only serves as a hint for designers and whatnot.
If you want a default value, you'll have to set it in the constructor (or even better, in a property initializer, which were added in C# 6). If you're storing your stuff in the ViewState, you'll need to expand those property definitions and make them access the ViewState. Then set the default values for the properties in the OnInit method to avoid persisting them on the client side.

From http://msdn.microsoft.com/en-us/library/system.componentmodel.defaultvalueattribute.aspx
Note
A DefaultValueAttribute will not cause a member to be automatically initialized with the attribute's value. You must set the initial value in your code.
In other words, the DefaultValueAttribute just gives you a place to declare what you want the value to be. You still have to write code to populate the value from the attribute.

Related

Why the NumericUpDown's Text property is not suggested by Intellisense?

I have noticed that all Controls have a Text property. However, the Intellisense doesn't suggest it for NumericUpDown objects. When manually writing it down, it does work and returns the value of the NumericUpDown as a string. Why is that?
The docs show the property defined as:
[BrowsableAttribute(false)]
[BindableAttribute(false)]
public override string Text { get; set; }
The BrowsableAttribute(false) bit (or more likely EditorBrowsableAttribute) is what 'hides' it from Intellisense.
Why does it hide it?
This API supports the product infrastructure and is not intended to be
used directly from your code.

Class model - setting default value

When defining the default value, what is the difference between
[DefaultValue("member")]
public string Role { get; set; }
and
public string Role { get; set; } = "member";
The first is an attribute which can be useful for meta-programming. For example, you might want to remember what the default value is if someone clears an input. It has nothing to do with the C# language itself. It does not modify the value of Role.
The second actually sets the property's value to 'member' in memory.
From the documentation:
A DefaultValueAttribute will not cause a member to be automatically initialized with the attribute's value. You must set the initial value in your code.
In other words, your first example helps tools (like the Windows Forms Designer) to know what the intended default value for a property is. But it does nothing at run-time.
If you want a property to be assigned a default value at run-time, you have to do it yourself, as in the second example you show.

Change Info Shown in VS 2010 DataTip

I am attempting to find a way to alter the information that is shown in a DataTip in the VS 2010 Debugger. The purpose being that I would like to choose what property value is shown on the initial window of a DataTip.
For example, when hovering over a collection in debug mode I am presented with the Name of the collection followed by its Count property's value.
This is useful information, but when I am hovering over one of my custom objects I am only presented with a path providing the type of object it is (in my case something like BOS.SuggestedOrdersDataEntity.SuggestedOrdersEntity).
I would like to have this initial DataTip window contain the property that I would determine to be the most useful depending on what custom object it is. For instance it could be the case that for an object that is of type SuggestedOrdersEntity it would be more helpful for the DataTip to show the value of its VendorName property in place of BOS.SuggestedOrdersDataEntity.SuggestedOrdersEntity (similar to the way collections show their Count property). The goal is to not have to use the '+' expander to find the current value of the VendorName (or whatever property is most useful).
I would like to be able to alter the DataTips so that I can customize them to immediately show a specific property's value (just like Count shows for collections) without needing to expand and view all the properties.
Does anyone know if this is possible? I've done some researching on DataTips, but nothing I have found discusses customizing them in this way...
You can use the DebuggerDisplay attribute, examples here.
[DebuggerDisplay("{Name} - {StockSymbol}")]
public class Company
{
public string Name { get; set; }
public string StockSymbol { get; set; }
public IEnumerable<Employee> Employees { get; set; }
public Company(string name) { Name = name; }
}

How to programmatically convert column type in DataGridView C#?

I have dynamically created DataGridView control on form, with DataSource specified as:
((DataGridView)createdControl).DataSource = (IList)(p.GetValue(editedClient, null));
where IList is defined as generic collection for following class:
public class CDocumentOperation
{
[DisplayName(#"Time")]
public DateTime TimePosted { get; set; }
[DisplayName(#"User")]
public CUser User { get; set; }
[DisplayName(#"Action")]
public string Action { get; set; }
}
grid is populated successfully with data, but the only problem that all columns
are created as Text fields.What I need is to manually convert column
which binds to User field, to have Buttons or Links (convert column type to DataGridViewButtonColumn).
Can I do this, without modifying grid auto-fill on grid post creation, without manual
column creation of appropriate type and data copying ?
The short answer is that this cannot be done without manually creating the columns (and setting the DataPropertyName property) before binding. There is no attribute you can use to decorate your data source, the DataGridView will simply generate a DataGridViewTextBoxColumn for every data type (except Boolean which it will resolve to a checkbox column). This behaviour is internal and unchangeable.
Your best bet is to disable AutoGenerateColumns on the grid and write your own method that dynamically generates appropriate column types, perhaps based on your own custom attribute, such as (from your example above):
[DisplayName(#"Time"), ColumnType(typeof(DataGridViewButtonColumn))]
public DateTime TimePosted { get; set; }
The attribute class is easy to write (just extend Attribute, add a Type field and an appropriate constructor). In the method that will generate the columns (immediately before binding), you can use reflection to crawl for properties and check for the presence of the custom attribute. (BindingSource.GetItemProperties() is very useful for obtaining information about the properties on objects in a collection.)
This is not the most elegant solution (and it delves into some intermediate-level concepts), but it's the only way to get around this limitation with auto-generated columns in the DataGridView control.

How do I specify a required attribute in a custom .NET Web control?

private string _itemId;
[Browsable(true),
Description("Required identifier for the Item.")]
public string ItemId
{
get { return _itemId; }
set
{
if (string.IsNullOrEmpty(_itemId))
{
_itemId = value;
}
}
}
How would I actually make that required when someone uses the control? I'm trying to find an attribute that says something like Required(true).
I don't know that there's an attribute for this. I believe on the Page_Load event (or perhaps some rendering event) just check if the value has been set. If not then throw an exception.
I don't think this is possible. Consider that the designer needs to be able to create an instance of the control when it's dragged from the toolbox. At that time, it's going to have default values for properties, and these values need to be valid.

Categories