Answered by:
How to define attributes to Custom GridView?

Question
-
User829868397 posted
Hi,
I have a custom GridView and have custom attributes.
e.g.
C#:
public class EpwGridView : GridView
{
....
public string EpwDataSessionId { get; set; }
public string EpwDataSourceType { get; set; }
....
}ASPX:
<epw:EpwGridView ID="GridView1" SkinID="epwGridView" runat="server" DataKeyNames="EpwRowId" EpwDataSessionId="statsData" EpwDataSourceType="DataView"> ... </<epw:EpwGridView></epw:EpwGridView>>
EpwDataSourceType has to be either 'DataView', 'DataTable', 'ObjectDataSource'.
Both , EpwDataSourceType and EpwDataSessionId are required attributes. Meaning the ASPX page should throw an error if these are not defined.
How can I do that?
Any help or reference links are greatly appreciated.
Thanks
hpdotnet
Friday, July 25, 2008 11:30 AM
Answers
-
User-1995538749 posted
I've never used it, but you can look at the RequiredAttribute class:
http://msdn.microsoft.com/en-us/library/microsoft.build.framework.requiredattribute.aspx
Your Enum should be defined as Public outside of your CustomControl class so that it can be accessed outside of the class.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, July 28, 2008 1:35 PM
All replies
-
User-1995538749 posted
First off, I'd change your EpwDataSourceType to an Enum. Secondly, within the getter of your properties, you can throw a NullReferenceException if the underlying member is null. This however will most likely exclude you from using auto-implemented properties.
Friday, July 25, 2008 11:42 AM -
User829868397 posted
Thanks ecbruck for the quick reply.
I tried this:
public enum EpwDataSourceType
{
ObjectDataSource,
DataView,
DataTable
}
But how do I see it as a property in my custom EpwGridView aspx tag. I want it similar to the runat="server" tag where the developer can only put in the "server" option. The only difference being, this one has 3 options.
Thanks
hpdotnet
Friday, July 25, 2008 11:59 AM -
User-1995538749 posted
It will need to be defined outside of your custom GridView class.
Friday, July 25, 2008 12:09 PM -
User829868397 posted
Required property -
If I throw an exception in the getter, it means I am not forcing the developer to set a value for that property. It will throw an exception only when the code tries to use that property.
Is there no other way to make a property of custom Control required?
Property value options -
What do you mean by 'outside of the custom GridView class'? The property is defined in the Custom class. You mean the 'value options' should be defined in a separate class?
Thanks
hpdotnet
Monday, July 28, 2008 1:12 PM -
User-1995538749 posted
I've never used it, but you can look at the RequiredAttribute class:
http://msdn.microsoft.com/en-us/library/microsoft.build.framework.requiredattribute.aspx
Your Enum should be defined as Public outside of your CustomControl class so that it can be accessed outside of the class.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, July 28, 2008 1:35 PM -
User829868397 posted
The RequiredAttribute does not work. For not, I am explicitly checking the value and throwing an exception if null.
Thanks for the input on Enum. I now have an enumerator that is used by the control property. This enforces the developer to pick from the enum list as the property value.
Here's one more useful link:
http://msdn.microsoft.com/en-us/library/aa720440(VS.71).aspx
Here's my code:namespace EPWWEB.WebControls { ... // The DataSourceType enumeration. public enum DataSourceType { DataView, DataTable, ObjectDataSource, } .. public class EpwGridView : GridView { ... private DataSourceType epwDataSourceType; [ DefaultValue(DataSourceType.DataView), Description("Defines the underlying data source"), ] public DataSourceType EpwDataSourceType { get { return epwDataSourceType; } set { epwDataSourceType = value; } }
}Tuesday, July 29, 2008 4:22 PM