ASP.Net server control complex "properties" - asp.net

I am going to aplogize in advance because I am really at the limits of my understanding on this so if I do not explain this well....well sorry...
Anyway I am trying to create an asp.net server control that has complex properties which can be set using intellisense. So as an example I will use cars, so the server control might be called car and when I actually implement the control on a webform I want to set complex, hierarchical properties so for example:
<Control:Car Color="Paint.Metalic.CandyRed"
Wheels="Forged.Aluminun.FiveSpoke.GunMetal" />
or
<Control:Car Color="Paint.Matte.Yellow"
Wheels="Forged.Steel.SevenSpoke.BareMetal" />
I have tried creating public properties in the server control that are just types/classes that point to the base class but intellisense doesn't come up with anything. I can use a straight forward enum and that will show up but I can't do anything hierarchical that way. I've been looking for examples but I can't seem to find anything. Any help would be greatly appreciated!!!!
Thank you!
How about a different example as it seems the relationship between items or their intended value, although completely unimportant, seems to be an issue.
Let's take the relationships between continent/ country / state / city / etc.... By this example, if my custom server control is called "Location" then I would like to be able to ...
<Control:Location CurrentLocation="UnitedStates.Nebraska.Lincoln" />
or
<Control:Location CurrentLocation="Europe.Italy.Napoli" />

Your problem is worse than the previous two answers suggest: you don't know what you're doing.
Sorry to sound so harsh, but where did you ever see something like "Paint.Metalic.CandyRed", and what did it mean there? Or "Forged.Aluminun.FiveSpoke.GunMetal"? What do you even want that to mean?
First, figure out what you want to represent. Then, create a class that can represent it. Then, add a property of that class to the server control. You may have to add a TypeConverter or other designer support in order for ASP.NET to convert your preferred textual representation into an instance of the class. Otherwise, you'll be able to get something like the properties of a Font.
I'm going to make a guess about what some of these values represent, and try to show you how to deal with them in a control. My guess could be far off, though.
I'll work with "Paint.Metalic.CandyRed". I'll assume this applies to the domain of automobile customization, and that the Color property is meant to represent the finish given to the car as a whole. In that case, "Paint" would probably be an enum referring to the type of finish (though I don't know what other sorts of finish apply to a car!). I know from building model cars when I was a kid that paints may be metallic, or gloss, or flat, so those three would be enum values of one enum. "CandyRed" would be one of many colors. This would give something like this:
public enum FinishType
{
Paint,
NotPaint // _I_ don't know!
}
public enum PaintFinish
{
Metallic,
Gloss,
Flat
}
public enum CarColor
{
CandyRed,
SilverMist,
DesertSandMica,
MagneticGray,
// etc.
}
public class CarFinish
{
public FinishType FinishType {get;set;}
public PaintFinish PaintFinish {get;set;}
public CarColor CarColor {get;set;}
}
public class Car : WebControl
{
public CarFinish Color {get;set;}
}
This would allow for something like this:
<Control:Car Color-FinishType="Paint"
Color-PaintFinish="Metallic"
Color-CarColor="CandyRed" .../>
or this:
<Control:Car ...>
<Color FinishType="Paint" PaintFinish="Metallic" CarColor="CandyRed"/>
</Control:Car>

These items would need to be enums as that is the only way that it is supported at least in everything I have seen. now, you can accomplish what you want with a few enums.
namespace Paint
{
public enum Metalic
{
CandyRed
}
public enum Matte
{
Yellow
}
}
Granted, not perfect, but easy to document and understand!

As Mitchel Sellers posted, they will need to be either Enum or Constants/Statics.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace Paint
{
public class Metallic
{
public static Color CandyRed
{
get { return Color.Red; }
}
public static Color CandyGreen
{
get { return Color.Green; }
}
}
public class Matte
{
public static Color Red
{
get { return Color.Red; }
}
public static Color Green
{
get { return Color.Green; }
}
}
}

Related

#model for _layout.cshtml on MVC4?

I was wondering if there's a way to specify a model for the _layout.cshtml file, i've seen lots of posts with the basic same question with people replying with "alternative" solutions, not saying it's not possible nor showing how exactly we could achieve this
having some experience with webforms I've been trying to migrate to MVC and often find myself with such questions, I've found this website: http://blog.bitdiff.com/2012/05/sharing-common-view-model-data-in.html
which partially solved my problem but even them don't bind their _layout.cshtml with a #model, as far as I know, I have to specify a model on each view if I want to access the SharedContext, please correct if I'm wrong
what I wanted to do is declare a "#model Namespace.MyModel" on _layout.cshtml so it could retrieve its information by itself, instead of having to implement a model for each view inherinting from the LayoutModel
*I hope I'm being clear, basically, I wanted to know how can I declare #model tag on a _layout.cshtml so it can access its own model
with the solution I linked before (even though it's not linked to my question) I have to do:
#(((BaseController)ViewContext.Controller).Context.Property) to get the shared information, and if I could simply declare (and use) a #model instead, I could accomplish the same thing by doing something like: #Model.Property*
as you can see, im struggling trying to migrate whatever I already know from webforms to MVC and it's being quite difficult for me since I have to adopt certain practices which are completely different from what I'm used to
thanks in advance
You should delegate the parts of your layout that "need a model" to a separate controller using partial views and RenderAction:
#Html.RenderAction("SomeAction", "LayoutController")
Have LayoutController.SomeAction return a PartialViewResult, which you can then strongly type to a model.
Even though you already accepted an answer, based on your saying you are just pulling an image URL you should do it using JQuery, not a model.
This code is untested, apologies for that. Feel free to point out if I typed a bug. The HTML element containing the background image has the id="url" attribute so the selectors work.
Controller
[HttpGet]
public string GetSessionUrl()
{
//logic to detmine url
return url;
}
JQuery
$(document).ready(function () {
var $url = $('#url');
var options = {
url: "/Home/GetSessionUrl",
type: "get",
async:false
};
$.ajax(options).done(function (data) {
$url.attr('src', data);
});
});
You can add BaseModel to _Layout.
#model BaseModel
Then all models inherit from that BaseModel class.
public class MyModel : BaseModel
{
}
As others stated, it is not a good practice. If your model forgets to inherit from BaseModel, it'll throws exception at run time. However, it is up to you.
In BaseController you can declare any model as property.
public class BaseController : Controller
{
public BaseController ()
{
MyTag = new TagModel (); // or get db, take any value from there
}
public TagModel MyTag { get; set; }
}
In action:
ViewBag.MyTag = MyTag ;
And in _Layout.cshtml, you can use
#{
var myTag = (TagModel)ViewBag.MyTag;
}

Change icon of codeActivities

I have some code activities.
public class baseAct: CodeActivity
{
}
public sealed class C1: baseAct
{
}
public sealed class C2: baseAct
{
}
public sealed class C3: baseAct
{
}
And i use a custom wf desinger.
I would like set same icon to activites C1,C2,C3 without define activity designer.
There are something (AttributeTableBuilder or similar) to set icon?.
Add the icon to your solution, set its Build Action to Resoruce
In your Activity, set the ActivityDesigner.Icon to this image
Hardest part about this is getting the Uri right. If you have problems, just dump the image into the same folder as the designer.
<sap:ActivityDesigner.Icon>
<DrawingBrush>
<DrawingBrush.Drawing>
<ImageDrawing>
<ImageDrawing.Rect>
<Rect
Location="0,0"
Size="16,16"></Rect>
</ImageDrawing.Rect>
<ImageDrawing.ImageSource>
<BitmapImage
UriSource="RelativeUriToTheImage.bmp" />
</ImageDrawing.ImageSource>
</ImageDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
</sap:ActivityDesigner.Icon>
I have a sample that illustrates exactly what you need to do. How to create a Custom Activity Designer with Windows Workflow Foundation (WF4)

How to create custom filter toolbar html helper in mvc3

I'm strugling on this for quite a while now. I need to create a custom mvc3 html helper for easy filter and toolbar management.
All that helper should look something like this below or something similar. What i want to do is to have a helper where i could give toolbar buttons and filter fields and it generate a toolbar and filter form for me.
I just can't figure it out, because i'm quite new in asp.net mvc
#Html.Toolbar(x => {
x.AddFilterButton();
x.AddButton("Naujas");
x.AddDropDownList();
},
#<text>
#Html.EditorFor(Model.ListFilter.Name)
Filter
ResetFilter
</text>,
filterVisible: false)
How i could achieve this? Any help would be highly apreciated. Thanks indeed.
Something like this:
public static class ToolbarExtensions {
public static Toolbar Toolbar<T>(this HtmlHelper<T> html) {
return new Toolbar();
}
}
public class Toolbar : IHtmlString {
public string ToHtmlString() { /* build your HTML off the state here */ }
public Toolbar AddButton(string label) {
/* build up state here */
return this;
}
}
The syntax on this would be a little different, instead of a lambda, would look like this:
#Html.Toolbar().AddButton("Button 1").AddButton("Button 2")
But you could easily change it to use a chaining object in the lambda instead of on the Toolbar method call.
The IHtmlString interface tells the ViewEngine to output the object as raw HTML when its encountered. The chaining is just achieved by returning the current instance in your methods after modifying the object state.

Make File.nativePath bindable? or how to extend flash.filesystem.file

I would ultimately like to make .nativePath bindable or fire an event when it changes in Adobe Air. I figured I'd just extend the File class and be good.
But I cant find its source anywhere (so I know how to extend it). I've dug through http://opensource.adobe.com/svn/opensource/flex/sdk/ quite a bit and didnt see anything.
Is there a way to make .nativePath bindable or extend File?
alxx, your code was definitely close. Thank you - it gave me an idea on how to extend it. Working code:
public class FileEx extends File
{
public function FileEx(path:String=null)
{
super(path);
}
[Bindable("nativePathChanged")]
override public function get nativePath():String
{
return super.nativePath;
}
override public function set nativePath(value:String):void
{
super.nativePath=value;
dispatchEvent(new Event("nativePathChanged"));
}
}
The File Class is part of the Flash package, so it is not open source and you won't be able to get your hands on the code (unless you're deep in the inner circle of Adobe developers).
In theory you can extend the class, as it is not marked as final, and make the nativePath Bindable that way, but I'm not sure of the benefit. You'd have to expand on your use case to evaluate that.
You don't need source to subclass something. As long as it's not final, just extend it and override something you need:
public class BindableFile extends File {
[Bindable(event="nativePathChanged")]
override public function get nativePath():String {
return super.nativePath;
}
override public function set nativePath(value:String):void {
super.nativePath = value;
dispatchEvent("nativePathChanged");
}
}
Not tested, but looks realistic :)

How do I make a custom ASP.NET control based/subclassed on an existing one?

I want to make a custom ASP.NET control that is a subclasse of System.Web.UI.WebControls.Calendar. I need it to add a few data members and set up a few event handlers in it's constructor.
Is this possible? If so How?
So far I have tried using add new item to add a default web user control and tried editing in Calendar in a few different places. None have worked so far.
Edit: it seems I'm totally missing how this stuff is supposed to work.
Does anyone known of a demo project that I can look at? Something that already exists. (Unless you are really board, don't go out and make one for me.)
Unless I'm misunderstanding the question, you can just create a new class file and inherit from Calendar. Add in the properties you need, and the event handlers you want to set up.
public class MyCalendar : System.Web.UI.WebControls.Calendar
{
public MyCalendar()
{
// set up handlers/properties
}
}
Then anywhere you'd like to add a Calendar to your pages, you can simply create a MyCalendar instead. If you need to do so in the designer, you can look at several good tutorials about how to make your inherited controls show their new properties in the designer, like this one.
In a new class file you need to inherit from System.Web.UI.WebControls.Calendar instead of System.Web.UI.UserControl.
namespace MyNamespace
{
[ToolboxData("<{0}:UserCalendar runat=\"server\" />")]
public class UserCalendar : System.Web.UI.WebControls.Calendar
{
private string property1;
public UserCalendar() { }
public string Property1 { get { return property1;} set { property1 = value; } }
}
}
Then on your .aspx page (or in another control .ascx):
<%# Register TagPrefix="userControl" namespace="MyNamespace" assembly="MyProjectAssembly" %>
<userControl:UserCalendar runat="server" ID="myCalendar" property1="some value" />
Stuff to read: Developing Custom ASP.NET Server Controls.

Resources