I'm working with VS2010 Beta2, ASP.NET and Dynamic Data. I'm using VB but will certainly be happy to receive a C# example if necessary.
I used a few of Steve's examples (Writing Attributes and Extension Methods for Dynamic Data and
Dynamic Data - Hiding Columns in selected PageTemplates) to create a custom property attribute to be applied to a dynamic data column and want it to accept an array of enum values (each value will represent a dynamic data page template where this column
should not be displayed). My new attribute works fine when I hardcode it like this, meaning the HideColumnIn attribute is properly populated with the array of enum values:
<HideColumnIn(PageTemplate.List, PageTemplate.Edit)> _
Public Property My_DD_Column As Object
However, I want to store the values to be used for the HideColumnIn in the DB as integer values that correspond to the PageTemplate enum values as opposed to hardcoding them as I shown above.
Here is the code to handle the HideColumnIn attribute and enum:
<AttributeUsage(AttributeTargets.[Property], AllowMultiple:=False)> _
Public Class HideColumnInAttribute
Inherits Attribute
Private _pageTemplate() As PageTemplate
Public Property PageTemplates() As PageTemplate()
Get
Return _pageTemplate
End Get
Private Set(ByVal value As PageTemplate())
_pageTemplate = value
End Set
End Property
Public Sub New(ByVal ParamArray pageTemplateArray As PageTemplate())
PageTemplates = pageTemplateArray
End Sub
End Class
Public Enum PageTemplate
Details = 1
Edit = 2
Insert = 3
List = 4
ListDetails = 5
End Enum
What I'm having difficulty with is reading integer values from the DB and casting them to an array of PageTemplate() to be passed to the constructor above. The integer values in the DB will be 1 through 5 to correspond to the enum values listed above. Here is how I'm trying to build the ParamArray to be passed to the attribute constructor:
Dim hideFieldsIn(-1) As PageTemplate
Dim rowCount As Integer = -1
If reader.HasRows Then
While reader.Read()
rowCount += 1
ReDim hideFieldsIn(rowCount)
hideFieldsIn(rowCount) = DirectCast(reader.Item("Page_Template_Number"), PageTemplate)
System.Diagnostics.Trace.Write("Page_Template_Number: " & reader.Item("Page_Template_Number").ToString() & " --- " & DirectCast(reader2.Item("Page_Template_Key"), PageTemplate).ToString())
End While
For i As Integer = 0 To (hideFieldsIn.Length - 1)
System.Diagnostics.Trace.Write("----hideFieldsIn: " & i & " - " & Convert.ToInt32(hideFieldsIn.GetValue(i)))
Next
End If
The output from the Trace.Writes above is:
--Page_Template_Key: 2 - Edit
--Page_Template_Key: 4 - List
----hideFieldsIn: 0 - 0
----hideFieldsIn: 1 - 4
For some reason, the integer value of 2 that is stored in the DB is being cast to 0.
Does anyone know what I'm doing wrong and/or have any ideas how I might do this better (I feel like my solution is "wrong" for some reason)?
I didn't notice your new article until after I posted this message yesterday (same old story
) but I'm in the process of trying to understand/implement as much of it as possible.
Maybe I'm confused, but the GetPageTemplate() method seems to be assuming that routing is setup as the default of "{table}/{action}.aspx".
Is there some way to implement something like Page.RouteData.Values("action") in that method so that it doesn't matter how the routing is setup?
What I was trying to ask was what you thought of using Page.RouteData.Values("action")
to determine the value you're extracting from the URL in stead of the GetPageTemplate method. I mean, if someone sets up routing to use "{action}/{table}.aspx" instead of the default of "{table}/{action}.aspx", then the code will still work even though
the "action" you want to extract has changed its location.
I changed some things but won't post the code now because (1) I'm home and don't have it here and (2) it's VB and I'm assuming you don't want to see that syntax. lol
Anyway, I moved this code below from your "IsHidden" function to the very beginning of the "GetVisibleFields" function because you only need to determine the current page template you're dealing with once for all the colums that GetVisibleFields will return:
// need to get the current page template
var page = (System.Web.UI.Page)System.Web.HttpContext.Current.CurrentHandler;
var pageTemplate = page.GetPageTemplate();
Next, I changed the GetPageTemplate function to this to handle the possible routing issue I mentioned above: (The part I inserted in bold is VB syntax and I'm guessing the syntax may be different for C#):
Anyway... I just figured I'd let you know what I've done in case you have any thoughts (especially if you see something I've done that's asking for trouble ).
Member
2 Points
31 Posts
How to cast integer values as an array of enum values?
Feb 18, 2010 10:16 AM|beeps4848|LINK
Hello all!
I'm working with VS2010 Beta2, ASP.NET and Dynamic Data. I'm using VB but will certainly be happy to receive a C# example if necessary.
I used a few of Steve's examples (Writing Attributes and Extension Methods for Dynamic Data and Dynamic Data - Hiding Columns in selected PageTemplates) to create a custom property attribute to be applied to a dynamic data column and want it to accept an array of enum values (each value will represent a dynamic data page template where this column should not be displayed). My new attribute works fine when I hardcode it like this, meaning the HideColumnIn attribute is properly populated with the array of enum values:
However, I want to store the values to be used for the HideColumnIn in the DB as integer values that correspond to the PageTemplate enum values as opposed to hardcoding them as I shown above.
Here is the code to handle the HideColumnIn attribute and enum:
What I'm having difficulty with is reading integer values from the DB and casting them to an array of PageTemplate() to be passed to the constructor above. The integer values in the DB will be 1 through 5 to correspond to the enum values listed above. Here is how I'm trying to build the ParamArray to be passed to the attribute constructor:
The output from the Trace.Writes above is:
--Page_Template_Key: 2 - Edit
--Page_Template_Key: 4 - List
----hideFieldsIn: 0 - 0
----hideFieldsIn: 1 - 4
For some reason, the integer value of 2 that is stored in the DB is being cast to 0.
Does anyone know what I'm doing wrong and/or have any ideas how I might do this better (I feel like my solution is "wrong" for some reason)?
Thanks!
-Beeps
All-Star
17916 Points
5681 Posts
MVP
Re: How to cast integer values as an array of enum values?
Feb 18, 2010 04:48 PM|sjnaughton|LINK
Hi Bob, is this solved now you have seen my article here A New Way To Do Column Generation in Dynamic Data 4?
VS2010 & .Net 4.0 Beta 2 Hide Column based on page template Dynamic Data 4
Always seeking an elegant solution.
Member
2 Points
31 Posts
Re: How to cast integer values as an array of enum values?
Feb 19, 2010 10:09 AM|beeps4848|LINK
Hi again Steve!
I didn't notice your new article until after I posted this message yesterday (same old story
) but I'm in the process of trying to understand/implement as much of it as possible.
Maybe I'm confused, but the GetPageTemplate() method seems to be assuming that routing is setup as the default of "{table}/{action}.aspx".
Is there some way to implement something like Page.RouteData.Values("action") in that method so that it doesn't matter how the routing is setup?
-Beeps
All-Star
17916 Points
5681 Posts
MVP
Re: How to cast integer values as an array of enum values?
Feb 19, 2010 08:25 PM|sjnaughton|LINK
Hi Bob, GetPageTemplate just extracts the last part of the URL
http://site/Customer/List.aspx
then trims off the .aspx and finally tries to match the filename with the Enum PageTemplate.
Hope this helps [:)]
Dynamic Data 4
Always seeking an elegant solution.
Member
2 Points
31 Posts
Re: How to cast integer values as an array of enum values?
Feb 20, 2010 07:59 AM|beeps4848|LINK
Sorry, I wasn't clear...
What I was trying to ask was what you thought of using Page.RouteData.Values("action") to determine the value you're extracting from the URL in stead of the GetPageTemplate method. I mean, if someone sets up routing to use "{action}/{table}.aspx" instead of the default of "{table}/{action}.aspx", then the code will still work even though the "action" you want to extract has changed its location.
It was just a suggestion.
I fully implemented the code in your A New Way To Do Column Generation in Dynamic Data 4 article yesterday and it works great! Thanks for that!
I changed some things but won't post the code now because (1) I'm home and don't have it here and (2) it's VB and I'm assuming you don't want to see that syntax. lol
Anyway, I moved this code below from your "IsHidden" function to the very beginning of the "GetVisibleFields" function because you only need to determine the current page template you're dealing with once for all the colums that GetVisibleFields will return:
// need to get the current page template
var page = (System.Web.UI.Page)System.Web.HttpContext.Current.CurrentHandler;
var pageTemplate = page.GetPageTemplate();
Next, I changed the GetPageTemplate function to this to handle the possible routing issue I mentioned above: (The part I inserted in bold is VB syntax and I'm guessing the syntax may be different for C#):
public static PageTemplate GetPageTemplate(this Page page)
{
try
{
return (PageTemplate)Enum.Parse(typeof(PageTemplate), Page.RouteData.Values("action").ToString());
}
catch (ArgumentException)
{
return PageTemplate.Unknown;
}
}
Anyway... I just figured I'd let you know what I've done in case you have any thoughts (especially if you see something I've done that's asking for trouble ).
Thanks again!
-Beeps
All-Star
17916 Points
5681 Posts
MVP
Re: How to cast integer values as an array of enum values?
Feb 20, 2010 08:11 AM|sjnaughton|LINK
Yes great idea Bob, I'll update the articel as soon as I've tried it out, there is so much new stuff in DD4 [:)]
Dynamic Data 4
Always seeking an elegant solution.
All-Star
17916 Points
5681 Posts
MVP
Re: How to cast integer values as an array of enum values?
Feb 24, 2010 05:27 AM|sjnaughton|LINK
Hi Bob, updated the article with referance to you and this thread [:)]
Hide Column based on page template Dynamic Data 4 VS2010 & .Net 4.0 RC
Always seeking an elegant solution.