How to add custom property to asp.net custom server control? - asp.net

I have created an asp.net custom control and now want to pass some values to it from the page which will use the control.Is there any way i can add my own property to the control (like Text property is present for a Label control) ?

It's a class. Add a public property to it.

Assuming ASP.NET 3.5+ just create properties
public string YourProperty {get; set;}
Would be declarative on the control

Adding a property to a custom control in asp.net is no different than adding a property on any class in C# (for instance).
public class Custom : Control
{
public string Text { get; set; }
}

just add it as public property, then it should be useable for your needs.

Related

Strongly typed form in MVC which maps to a different type?

When I specify form inputs with the #Html.TextBoxFor method, then the generated input element would not necessarily map to the type that is expected in the form's action method.
Let's say I have two classes:
public class HomeA
{
public int A { get; set; }
}
public class HomeB
{
public int B { get; set; }
}
HomeA is the model of my view. If a controller action expects HomeB, then I can't provide the necessary input element in a strongly typed manner in my form:
#using (Html.BeginForm())
{
#Html.TextBoxFor(model => model.A)
}
This form will obviously not map to HomeB's property.
The controller action should not expect HomeB.
Use one view model per action.
If you are sending a ViewModel of XYZ, then in general your ActionMethod takes a ViewModel of XYZ.
Thats my general thoughts anyways for consistency/readability.
However if it works for you, do it as long as the relation is there.
ASP.net MVC - One ViewModel per View or per Action?
As for the note on composition vs. inheritance check out
ASP.NET MVC Architecture : ViewModel by composition, inheritance or duplication?
Check out
http://lostechies.com/jimmybogard/2009/04/24/how-we-do-mvc/
You would create a HomeAB class that contains both a HomeA and HomeB
If you have to create and to show some items which belong to both classes A & B, you can design an interface and then inherit that interface. Or you can create another class AB which inherits from A & B.
Hope this helps!

How to override label using EditorTemplate?

For a string property in MVC3, I've created a partial view at ~/Shared/EditorTemplates/String.cshtml and I've placed within it the following:
#model System.String
<div class="Input">
#Html.TextBox("", this.Model)
#Html.ValidationMessage("")
</div>
Viewing the result, it looks good. But the label is still showing up. As you can see it's not included in the partial view, above, so it must be coming from the base view.
How do I override the label output so when I do #Html.EditorForModel(), my string properties will have a customized label?
Use System.ComponentModel.DisplayName attribute to override the member name.
For example:
public string MyProperty {get;set;} //displays "MyProperty"
[DisplayName("My Property")]
public string MyProperty {get;set;} //displays "My Property"
I ended up creating a custom Object.cshtml template.

How can I dynamically access user control attributes?

Im trying to create a "user control menu" where links to a page's usercontrols are placed at the top of the page. This will allow me to put several usercontrols on a page and allow the user to jump to that section of the page without scrolling so much. In order to do this, I put each usercontrol in a folder (usercontrols) and gave each control a Description property (<%# Control Language="C#" Description = "Vehicles" .... %>).
My question is how can I access this description dynamically? I want to use this description as the link in my menu. So far, I have a foreach on my page that looks in the ControlCollection for a control that is of the ASP.usercontrols type. If it is I would assume that I could access its attributes and grab that description property. How can I do this? (Im also open to a better way to achieve my "user control menu", but maybe thats another question.) Should I use ((System.Web.UI.UserControl)mydynamiccontrol).Attributes.Keys?
you can iterate over the collection and do either a switch or a few if statements
I would suggst you have an interface or an abstract base class for all your user controls:
public abstract class MyBaseClass : UserControl
{
public abstract string MyDescription {get;}
}
public MyUserControlA : MyBaseClass
{
public string MyDescription {get {return "my description";}}
}
public MyUserControlB : MyBaseClass
{
public string MyDescription {get {return "my other description";}}
}
Then you can loop over them as you do:
foreach ...
if (mydynamiccontrol is MyBaseClass)
{
Response.Write(((MyBaseClass)mydynamiccontrol).MyDescription);
}
Hope this helps

Change which field is displayed in a dropdown

I have a Dynamic Data website and while inserting a record into a table, the foreign key relationship shown as a drop-down uses the wrong field for it's select item text value.
How can I change the drop-down such that when working with this one table, it will use a different column as the value in the drop-down?
thank you
The solution is to add a partial class with some attributes from the System.ComponentModel.DataAnnotations namespace & assembly. Notice the [DisplayColumn("Description")] below. That's what field is used to render as the text in a list.
Further reading
[MetadataType(typeof(ProductMetadata))]
**[DisplayColumn("Description")]**
[ScaffoldTable(true)]
public partial class Product
{
}
public class ProductMetadata
{
[UIHint("TextReadOnly")]
public string CreatedBy;
[UIHint("TextReadOnly")]
public string CreatedDate;
[ScaffoldColumn(false)]
public EntityCollection<OrderItem> OrderItem;
}

How to get design surface rendering and design time datasource selection with an asp.net user control

If I create a user control (EDIT:not a web control/server control) it's pretty trivial to get databinding. I just add a datasourceID property.
In code behind (vb)
Partial Public Class BandedControl
Inherits UserControl
Public Property DataSourceID() As String
Get
Return MyGridView.DataSourceID
End Get
Set(ByVal value As String)
MyGridView.DataSourceID = value
End Set
End Property
End Class
In code behind (c#)
public partial class BandedControl : UserControl
{
public string DataSourceID {
get { return MyGridView.DataSourceID; }
set { MyGridView.DataSourceID = value; }
}
}
My issue is that this breaks design time rendering and also I don't get a drop down list to choose my datasource. How do I resolve this. (Hint: I think I need a type convertor, but all the info I can find relates to server controls not user controls).
You could try adding the IDReferenceProperty attribute to your property definition...
public partial class BandedControl : UserControl
{
[System.Web.UI.IDReferenceProperty(typeof(DataSourceControl))]
public string DataSourceID
{
get
{
return MyGridView.DataSourceID;
}
set
{
MyGridView.DataSourceID = value;
}
}
}
See http://msdn.microsoft.com/en-us/library/system.web.ui.idreferencepropertyattribute.aspx for more info about the IDReferencePropertyAttribute class.
If that doesn't work - I'd also try to inherit from DataBoundControl instead of UserControl and see if that gets you anywhere.
Web UserControls are compiled dynamically at run time and so are not rendered at Design time, what you want to do is create a Web Custom Control. Your best bet here is to extend one of the existing Bindable Web Controls
http://msdn.microsoft.com/en-us/library/aa651710(VS.71).aspx
Not sure if this is exactly what you want but I seem to remember them showing something similar to this in some dnr tv episodes.
I think it was Miguel Castro episodes 1 & 2, but it could be episode 31.
An archive of all the videos is here

Resources