4
votes

I am making a custom web control for my ASP page that inherits from CompositeDataBoundControl. I have a public property in the definition of my control that is required, if the user does not provide this property in the control definition on an ASP page it will break and we will get an exception. I want the compiler to throw a warning similar to the one when a user forgets to provide the 'runat' property of a Control.

"Validation (ASP.Net): Element 'asp:Button' is missing required attribute 'runat'."

Here is basically what my code looks like:

public class MyControl : CompositeDataBoundControl, IPostBackEventHandler
{
    private string _someString;
    public string SomeString
    {
        get { return _someString; }
        set { _someString = value; }
    }

    // Other Control Properties, Functions, Events, etc.
}

I want "SomeString" to be a required property and throw a compiler warning when I build my page.

I have tried putting a Required attribute above the property like so:

[Required]
public string SomeString
{
    get { return _someString; }
    set { _someString = value; }
}

but this doesnt seem to work.

How can I generate such a compiler message?

1
I think you are going to find that runnat is a special case. It is a signal to the aspnet runtime that the markup it is to be rendered as a control, and a signal to the IDE that the control needs to be added to the page class (or whatever the container) as a field. Notice this property: 'btnmybutton.runnat' does not exist in your code behind.brian chandley
So is there any way to achieve what I am asking?user1018669
Rob Stevenson-Leggett already linked me to that and it did not help...user1018669

1 Answers

0
votes

Thats quite simple, on page load you can check if the property has some value or not. You can check it for Null or Empty case depending on your property type. like if i have this

private string _someString;
    public string SomeString
    {
        get { return _someString; }
        set { _someString = value; }
    }

On page_load event i will check if

if(_someString != null && _someString != "")
{ 
    String message = "Missing someString property";
    isAllPropertySet = false; //This is boolean variable that will decide whether any property is not left un-initialised
}

All The Best.

and finally