C# - Get Property's Display Name From A Custom Attribute?

Nov 7, 2010

I am trying to create a minimum length validation attribute which will force users to enter the specified minimum amount of characters into a textbox

public sealed class MinimumLengthAttribute : ValidationAttribute
{
public int MinLength { get; set; }
public MinimumLengthAttribute(int minLength)
{
MinLength = minLength;
}
public override bool IsValid(object value)
{
if (value == null)
{
return true;
}
string valueAsString = value as string;
return (valueAsString != null && valueAsString.Length >= MinLength);
}
}

In the constructor of the MinimumLengthAttribute I would like to set the error message as follows:

ErrorMessage = "{0} must be atleast {1} characters long"

How can I get the property's display name so that I can populate the {0} placeholder?

View 1 Replies


Similar Messages:

Custom Server Controls :: Category Attribute Of User Control Property Does Not Work Correctly In Categories Tab

Aug 19, 2010

I have a User Control (ascx) and a property which a want to display in my categories tab in Visual Studio in the category named "Styles".

[Code]....

And here is the problem: Actually I do not need a get, because I only have to set the property (write only property). But when I omit the get, the property is displayed in the "Misc" category in the categories tab in Visual Studio. Only when I code the get as well, then the property is displayed correctly in the "Styles" category in the categories tab in Visual Studio.

Does anybody know why? How can I display the category correctly only with set?

View 2 Replies

Custom Server Controls :: Binding UserControl Property To Page Property?

Sep 15, 2010

So what I'm trying to accomplish is this

[Code]....

The user control has public properties named accordingly and the page has protected properties accordingly which I've verified have the desired values.

For some reason the values are always empty strings or 0s in the usercontrol, no matter what the page property is.

View 1 Replies

MVC :: Retrieve Attribute With Property Name

Sep 6, 2010

What is the best way to obtain an attribute such as the DisplayNameAttribute of a model's property using the property's name. I am using the following method currently:

[Code]....

Looking at this long statement, I cannot help wondering if there is a better way to do it.

View 4 Replies

MVC :: Get Property / Field Value Inside Attribute

Dec 22, 2010

I want to disallow entering html tags inside specific textFields using custom attribute HtmlRemove, but I don't know how can I acces property/Field value? Model:

[Code]....

My custom Attribute

[Code]....

View 8 Replies

MVC :: Setting Property On A Filter Attribute From Controller?

Feb 17, 2010

here's my situation - I've created some ActionFilterAttributes that I want to apply to a custom Controller class that my other controllers can inherit from. The problem is, I need to set a property on the attribute from the final inheriting controller. So I have something like this :

[Code]....

[Code]....

View 2 Replies

Get DisplayName Attribute From MVC2 Model's Property?

Dec 28, 2010

So, I've got an contact form in my MVC 2 application.

I'd like to programatically email all properties of my "ContactModel".

Here is what I'd like to do in psuedo-ish code:

[Code]....

In case it matters...ContactModel sets up the DisplayName attributes like this:

[DisplayName("First Name")]
public string FirstName {get; set ;}

I'd like to keep this nice and DRY by not repeating the DisplayName names.

Specifically, I'd like to enumerate over each property in my ContactModel, get its DisplayName, and get its submitted value.

View 1 Replies

MVC :: HttpRequestValidationException After Using [SkipRequestValidation] Attribute On Model Property?

Nov 12, 2010

I think I might have stumbled onto a bug in ASP.NET MVC 3 RC. When I setup my MVC2 project in a new MVC3 project, copy paste classes, code, change name spaces, etc, etc, I ran into an issue in the following, simplified for explanation purpose, scenario:

Model:

public class WineDetails
{
[SkipRequestValidation]
[Required(ErrorMessage = "Beschrijving verplicht")]
public string Description { get; set; }
}

ViewModel:

public class ViewModelCreateWine
{
public MasterData MasterData { get; set; }
public WineDetails WineDetails { get; set; }
}

ActionMethod:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult CreateWine(ViewModelCreateWine viewModelCreateWine)
{
GetMasterDataRegions(viewModelCreateWine);
if (Request.Params.ToString().IndexOf("Save") > 0)
{
if (TryValidateModel(viewModelCreateWine.WineDetails))
{
m_wineService.CreateWine(viewModelCreateWine.WineDetails);
return RedirectToAction("index", "Admin");
}
}
return View(viewModelCreateWine);
}

The ActionMethod "CreateWine" needs to call the "CreateWine" method in the WineService so that in the end a new Wine is added to the Database. So far it looks ok. As shown in the above code the [SkipRequestValidation] is set on the "Description" property of the WineDetails model so that the user can add Rich Text to the description and HTML elements are allowed during the Request validation. This works perfectly fine until the Params collection of the Request is accessed in the code to check if the Save button is clicked. When this line of code is trying to execute the following exception is thrown:

Exception Details: System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client (ViewModelCreateWine.WineDetails.Description="<p>HTML Content with...").

The same exception is thrown when I put the [ValidateInput(false)] attribute on the action method. When I comment out the "if" statement and its content there is no issue and the model validation works just fine and skips the Request Validation on the Description property as expected.

In MVC2 the above code worked fine with the [ValidateInput(false)] attribute on the action method.As I said I'm not sure if this is a bug, it very well might be my own stupidity, but I thought it would be worth to mention here. So any feedback is more than welcome.

View 2 Replies

C# - DesignOnly Attribute Does Not Hide A Property In Runtime?

Jan 17, 2010

I'm building a custom web control with a public property which I only want to be available in design time (i.e. make it unavailable in code behind). The DesignOnly attribute promises to do just that, but when I set [DesignOnly(true)] it has no noticeable effect whatsoever:

[Bindable(true)]
[Category("Appearance")]
[DefaultValue(null)]
[Localizable(false)]
[DesignOnly(true)]
public string MyProp
{
get
{
return ViewState["MyProp"] as string;
}
set
{
ViewState["MyProp"] = value;
}
}

The property still appears in code behind IntelliSense. Setting a value to it in code behind works. In these respects, the behavior is just as if the attribute had never been set. And I've cleaned and rebuilt the complete solution. Twice.

View 1 Replies

Web Forms :: ID Property Of A Control Can Only Be Set Using ID Attribute In Tag And Simple Value

Mar 19, 2013

I am using repeater control and in that i am having div in item template. Now I am setting its id using eval. I want to access this div in item databound for that i am making it runat=server. But in doing so I am getting :

"The ID property of a control can only be set using the ID attribute in the tag and a simple value."

<div id="<%# Eval("RENTER_ID") %>" style="display:none"></div>

How to achieve this functionality. One more thing i am using master page and repeater is in content page.

View 1 Replies

MVC :: Cannot Assign Timespan Display Format Attribute On Create/Edit And Display Views

Jul 6, 2010

inside my model metadata, i have a timespan field that would like to display in all Create, Edit and Display views like "hh:mm". For this, is set DisplayFormatAttribute as follows:

[Code]....

When i try to create a new model object, the time field gets displayed as "00:00:00" im my model (00 is the initial values) instead of "00:00". Is there something i am doing wrong? Note that the same approach works for date fileds, where i want to be displayed as "dd/MM/yyyy" (for this is assign "{0:dd/MM/yyyy}" to the display format attribute).

View 4 Replies

Custom Server Controls :: Insert A Drop List Property Into A Custom Webcontrol?

Feb 18, 2010

How insert a drop list property into a custom webcontrol? This a drop list property is show all textbox control on usercontrol or webpage.

View 3 Replies

Custom Server Controls :: Custom Checkbox Control 'checked' Property Is Not Working?

Sep 3, 2010

Iam developing a custom checkbox control deriving from the 'compositecontrol' which i need to use it for a composite control purpose.

public
class
TngCheckBox :
CompositeControl
{

i have written the required properties which are working fine except the "checked" property. here is my code for 'checked' property. i tried both the below ways (commented one and not commneted) . the problem is the 'checked' value is not setting the value at the get set part of the property.its assigning the value at the design time , but when i check and uncheck its not updating the property value.which is resulting me wrong .

[Code]....

View 2 Replies

Custom Server Controls :: Custom Checkbox Control 'checked' Property Is Not Working ?

Oct 15, 2010

Iam developing a custom checkbox control deriving from the 'compositecontrol' which i need to use it for a composite control purpose.

public
class
TngCheckBox :
CompositeControl
{

i have written the required properties which are working fine except the "checked" property. here is my code for 'checked' property. i tried both the below ways (commented one and not commneted) . the problem is the 'checked' value is not setting the value at the get set part of the property.its assigning the value at the design time , but when i check and uncheck its not updating the property value.which is resulting me wrong .

[Code]....

View 4 Replies

Custom Server Controls :: List Property Of A Custom Control Is Not Persisted?

Mar 15, 2010

I have a custom control, inherited from Button. This is the class definition for the control and 2 properties:

[Code]....

I show you 2 properties, only to illustrate the problem.

as you see Aplicacion property is of a custom type Sistema, and Roles property is of type List<UserRol>.

The Aplicacion property is rendered well, this way:

[Code]....

The problem I have is with List<UserRol>. I couldn't get it to be rendered. I expeect to be rendered this way:

[Code]....

Finally, this is the TypeConverter and Editor definitions for the list:

[Code]....


The UserRol class is a typical class, without any special attributes.

I have discovered that when I use the custom editor for Roles property, the collection is not persisted. When I use the default editor for the collection, the collection is persisted.

View 1 Replies

Custom Server Controls :: Need Custom Text Box Control With DataSource,TableName,ColumnName Property?

Nov 26, 2010

I need to create one custom text box control which should be bind with data base.

I am referring following link...

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.databoundcontrol.aspx

I used DataBoundControl to create my control but i am not able to do it properly.

public class SimpleTextBoxControl : DataBoundControl
{
private TextBox nameTextBox;
public string DataTextField
{
get
{
object o = ViewState["DataTextField"];
return ((o == null) ? string.Empty : (string)o);
}
set
{
ViewState["DataTextField"] = value;
}
}
[
Bindable(true),
Category("Data"),
DefaultValue(""),
Description("The text to display on the link."),
Localizable(true),
PersistenceMode(PersistenceMode.InnerDefaultProperty)
]
public virtual string DataTexValue
{
get
{
string s = (string)ViewState["Text"];
return (s == null) ? String.Empty : s;
}
set
{
ViewState["Text"] = value;
}
}
protected override void PerformDataBinding(IEnumerable retrievedData)
{
if (retrievedData == null)
return;
base.PerformDataBinding(retrievedData);
Controls.Clear();
nameTextBox = new TextBox { ID = "nameTextBox" };
foreach (object dataItem in retrievedData)
{
if (DataTextField.Length > 0)
{
nameTextBox.Text = DataBinder.GetPropertyValue(dataItem, DataTextField, null);
}
DataTexValue = nameTextBox.Text;
}
this.Controls.Add(nameTextBox);
}
}

View 1 Replies

Custom Server Controls :: How To Create Custom Property (DataSource) In Custom Controls

May 15, 2010

I know that how to create custom control and add custom property.

I want to create property like DataSource to my custom control.

View 1 Replies

Add Custom Property To Custom Server Control?

Jan 11, 2011

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)?

View 4 Replies

C# - Custom Attribute In A UserControl (ascx)?

Mar 8, 2011

Supposing I have a user control like this <MyTag:MyWidget runat="server" /> I am wondering if I can do something like <MyTag:MyWidget runat="server" MemberHeight="400" PublicHeight="200" /> So that in MyWidget.ascx I can have <div height="<%=IsLoggedIn ? MemberHeight : PublicHeight%>"> or something like that...? Because I want the height to vary in each page that is using this widget.

View 2 Replies

MVC :: Bit Lost On First Custom Authorization Attribute?

Mar 2, 2010

I want to create a single custom authorization attribute to be added to controller actions that require authenticated users.

My first shot at this, so I may have it all wrong

Got 3 Roles which corresponds to 3 Areas in my application (Client app, so I'll just call them RoleOne, RoleTwo, RoleThree and AreaOne, AreaTwo, AreaThree).

Each area have similiar (to a point) views and actions, like Sign Up, Area specific home, etc.

In the attribute I want to determine the current area, controller and action. Then I just add code like:

If not logged in, and in AreaOne, go to AreaOne Signup (etc)If logged in as RoleOne, and AreaOne Home is requested, go there, but if AreaTwo Home is requested ,go to AreaTwo Signup So in my attribute class I need to determine the current area, controller and action, and see what kind of user we have logged in (if any)

Using ActionExecutingContext it seems I can determine the contoller and action method names, however I cannot find the Area name, and altered the attribute to be called like this:

[Code]....

View 4 Replies

C# - Access Parent Class From Custom Attribute?

Jun 6, 2010

Is it possible to access a parent class from within an attribute.

For example I would like to create a DropDownListAttribute which can be applied to a property of a viewmodel class in MVC and then create a drop down list from an editor template. I am following a similar line as Kazi Manzur Rashid here.

He adds the collection of categories into viewdata and retrieves them using the key supplied to the attribute.

I would like to do something like the below,

public ExampleDropDownViewModel {
public IEnumerable<SelectListItem> Categories {get;set;}
[DropDownList("Categories")]
public int CategoryID { get;set; }
}

The attribute takes the name of the property containing the collection to bind to. I can't figure out how to access a property on the parent class of the attribute. Does anyone know how to do this?

View 2 Replies

Retrieving Custom Attribute On A Webpage Class

Jan 20, 2010

I've create a custom attribute for my web pages... In the base page class I've created I'm trying to pull if that attribute has been set. Unfortunately it does not come back as part of the GetCustomAttributes function. Only if I explicitly use the typeof(myclass) to create the class. I have a feeling it's due to how asp.net classes are generated.

[Code]....

View 1 Replies

C# Get Custom Attribute Of Hidden From Request.Form

Jul 22, 2010

I have some code which is rendering some custom attributes of Hidden Control (without runat=server).

Something like,

And I have a lot of these hidden elements on my form.

When I submit the form, I am looping through a set of ids, say 1200 to 1250, so I will read controls based on that, and also hidden with Request.Form["hdn "]

Now problem is, as we know we get value attribute from form when we do Request.Form["id"] for hidden, I want to read my custom attributes from hidden element. Is there any way? Note that, I can not touch the rendering part here.

View 1 Replies

C# - Specify A Range With The IntegerValidator Attribute On A Custom ConfigurationSection?

Jan 21, 2010

I have a class containing the following ConfigurationSection:

namespace DummyConsole {
class TestingComponentSettings: ConfigurationSection {
[ConfigurationProperty("waitForTimeSeconds", IsRequired=true)]
[IntegerValidator(MinValue = 1, MaxValue = 100, ExcludeRange = false)]

[Code]....

The value for the property 'waitForTimeSeconds' is not valid. The error is: The value must be inside the range 1-100.

If I change the IntegerValidator to have an ExcludeRage = true, I (obviously) get:

ConfigurationErrorsException was unhandled

The value for the property 'waitForTimeSeconds' is not valid. The error is: The value must not be in the range 1-100

If I then change the value of the property in the .config to a number higher than 100, it works.

If I change the validator to just have a MaxValue of 100 it works, but will also accept a value of -1.

Is it possible to use the IntegerValidatorAttribute with a range like this?

Edit to add

Confirmed as an issue by Microsoft.

View 1 Replies

Web Forms :: Custom Errors Tag Should Then Have Its Mode Attribute Set To Off

Jun 12, 2012

To enable the details of this specific error message to be viewable on remote machines, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".

I got this error at time of  running ma published website  but only for 1page this error occurred how it can be solved.

View 1 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved