WPF-like properties with custom controls in ASP.net - asp.net

In WPF it's possible to set a property of a custom control by either an attribute or by an inner property. Like this:
<custom:UserControl1 Text="My text here..."></custom:UserControl1>
Equals:
<custom:UserControl1>
<custom:UserControl1.Text>
My text here...
</custom:UserControl1.Text>
</custom:UserControl1>
In ASP.net the type of custom control property can be defined by the PersistenceMode attribute. At the moment I can only find a way to define a property either as an attribute or as an inner property.
Is there a possible way to set the custom control property like WPF on either way?
Thanks!

For Text, setting:
[
PersistenceMode(PersistenceMode.InnerProperty),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content)
]
public string Text
For the property you want to appear that way will allow you do to the second option; however, alternatively, you probably also could specify it inline. If that's the only property that you use as a child element, you can also specify PersistenceMode.InnerDefaultProperty or EncodedInnerDefaultProperty (as it would be the default), that latter of which would encode it.
Realistically, you can't do everything like you can in WPF in ASP.NET; it's just not that fully supported in the designer as that wasn't it's intent. But primitive types you can define as an inner property with a content design serialization, and it should allow you to do both options.
HTH.

Related

Binding using data-win-bind to backgroundImageUrl

I'm trying to bind the background-img: url('') property in a WinJS application.
I've got a view model property which is set to something dynamic like:
'images/' + myObject.name + '.jpg'
But I'm unsure how to use data-win-bind to set said property to the css property background-img: url(''); correctly.
My template is currently set like this:
<div class="item" data-win-bind="style.backgroundImage: backgroundImageUrl">
Where backgroundImageUrl is my view model property, but this does not seem to setting things correctly.
Any ideas as to how to bind to these properties?
Your data-win-bind syntax looks correct. So there could be two possibilities.
First, make sure you've called WinJS.Binding.processAll. That's necessary to set the binding context and set up the bindings described by data-win-bind attributes. Nothing happens without it.
Second, the value of the source's backgroundImageUrl must be a string in the form that CSS expects, that is, "url('')". It can't just be the relative path itself, like you would use with an img.src target.
To do this, either make the source's property in that form, or use a binding initializer/converter to add the url('') part automatically. For more on that, I suggest looking at Chapter 6 of my free ebook, Programming Windows Store Apps with HTML, CSS, and JavaScript, 2nd Edition, with the general data-binding discussion starting on page 299 and the initializer stuff starting on page 315.

ASP.NET auto width control

I have an ASP.NET control, for example a DropDownList. I want to set the width of the control to auto. How do I achieve this? The Width property in ASP.NET control does not seem to accept auto, but it can only accept like percentage, pixels, and points.
I know I can make some workaround by specifying style="width:auto" as the control's attribute, and as this attribute is not a valid attribute for the control, it will not be parsed by asp.net and hence passed directly to browser. Is there a more proper way to do this?
A few alternatives:
Adding the style=width:auto; into the ASP.NET webcontrol, as mentioned in the question
Adding element.Attributes.Add('style', 'width:auto'); in the code behind. Generally it is the same as option number 1, just that it's added from code behind. But option 1 seems to be better because it does not mix presentation with code behind, and changing the presentation does not require recompiling of the program.
Remove the Width property for the ASP.NET control. If the Width property is not specified, it will be set as auto. But here we need to be careful if there is any CSS rule that may affect the behavior, for example we specify input in the CSS file.
These links might help you:
http://www.telerik.com/help/aspnet-ajax/combobox-auto-width.html
http://demos.telerik.com/aspnet-ajax/combobox/examples/functionality/dropdownautowidth/defaultcs.aspx
http://www.codeproject.com/Articles/5801/Adjust-combo-box-drop-down-list-width-to-longest-s
UPDATE
http://www.kendoui.com/forums/kendo-ui-complete-for-asp-net-mvc/dropdownlist/how-to-make-the-dropdownlist-width-auto-resize.aspx
http://www.webdeveloper.com/forum/showthread.php?48901-Setting-Drop-Down-List-Width-To-Auto
Please do not forget to comment that which of these links helped you. This will be beneficial for others.

mvvm-light: Normal Property or DependencyProperty

In mvvm-light, it comes with two property-creation snippets: mvvminpc to create a 'normal' property; and mvvmpropa to create an attached property (aka dependency property). I just want to know if there is any guidance on what to take into consideration when choosing which one to use?
I've been also searching for a while on these property topics for MVVM light. Below is my understanding of the property snippets provided in MVVM light. There aren't really any cases where you would use one over the other because they are for different purposes.
Bindable Property - Bindable properties are created in your ViewModels to expose data which your view will display.
Examples include: Displaying a collection of Employees, or the selected employee information.
Dependency Property - View specific when it comes to the code snippet. These properties can be set to a constant value or use data binding for changes depending on the viewmodel data.
Examples include: A textblock control's "Text" property is a dependency property. The "Height" and "Width" properties are also dependency properties.
Attach Property - Also used more specifically for user controls. This property is added to a parent control. Child controls use these properties to behave a certain way within the parent control.
Example includes: a Dockpanel has a "Dock" (Attached Property Name) that isn't used by the Dockpanel itself. It is used by the children contained inside of it, the children will now be able to set the property: DockPanel.Dock="Top".
<DockPanel>
<TextBlock Text="{Binding EmployeeName}" DockPanel.Dock="Top" />
<Button Content="Click Here" DockPanel.Dock="Bottom" />
</DockPanel>
In the example code above:
The Bindable Property is the EmployeeName, which would be a public property in the viewmodel code.
The Dependency Property is the Text="" property of the textblock and the Content="" property of the button.
The Attach Property is the DockPanel.Dock property used in the Textblock and button, but is actually exposed by the Dockpanel itself. Notice that the Dockpanel doesn't contain any of the .Dock code, only the children do.
Dependency properties are recommended when you create UI components (controls, etc.): they have better time performances for binding.
Normal properties are used if you have business object or if you dont have/create UI controls.

How to specify in QTP's Object Repository, that I want to find Web Button object by title

I have rich web application with a lot of dynamic stuff (used ExtJS 3.0). I want to find Web Button using HP QuickTest Pro 10. This web button have only one property , that can uniquely identify it: title. But if I define that object in Object Repository with Description Property Title= it doesn't find it.
Please, advise me what can I do in that situation? I've tried to use object.title=Some value as well, but it doesn't helped.
Thanks!
Title is a DOM property not a QTP property, you can use DOM properties for identification by appending attribute/ to the property name.
Try adding a new property "attribute/title" to the button class, and make that an identification property in the object identification configuration.
The "attribute/" prefix allows you to use any attribute that you can access via the Object property.

Why the CSS "gets lost" when i set the IncludeStyleBlock property equal to True?

The demo web project that ships with the VS2010 contains a system.web.ui.webcontrols.menu control.
That particular menu includes the IncludeStyleBlock property.
When the IncludeStyleBlock property is set to False the menu is displayed as it supposed to. The menu gets destroyed if i set that property to True. So here is my main question...
Is there any way of preserving the appearence of the menu, with the IncludeStyleBlock property set to false?
P.S. I have to set it to False... since my provider does not support the forth generation of the .NET framework.
If the generate elements got id's and classes, you can style them with a normal CSS file. Does it generated inline CSS with the option turned on? If so you can copy that to an external file to start with
Does this page give you some clue, specifically in the Remarks section?
In short, if you set the property to false, you must provide "your own block of CSS definitions in the page, or include a link to an external CSS file that contains the definitions." In addition, you won't be able to set style properties.
So, conversely, if you set the property to true, it would ignore the style properties you provide.
You misspoke in your question. You begin by saying the menu is perfect when IncludeStyleBlock property is set to False and breaks when True. Then you say it is broken when False and you want a workaround for making in work under False.
Because of this confusion I am basing this answer off the assumption that you want to mimic the default style set by ASP.NET when IncludeStyleBlock is set to true but while keeping IncludeStyleBlock="False"
First: Since the menu displays perfectly when IncludeStyleBlock="True" , what you need to do is set it to true and preview the rendered source code. From the source code you can find the copy of the default CSS block that the Menu control generates by default. This is what you need.
Second: Once you have the CSS block, simply copy and paste it into your markup (inline or externally). Once you do that, you can make IncludeStyleBlock="False" and the now inline/external CSS block will preserve the appearance of the menu. (As a bonus, this is a small performance boost from caching CSS)

Resources