Intellisense and strongly typed data controls - no more typos ...?

I read on ASP.NET 4.5 web form functions to find out what's new. I recently tried the function of strongly typed data controls using a GridView. However, as far as I can tell, you can use a strongly typed tutorial if you use asp: TemplateField ..... like this:

<asp:TemplateField HeaderText="Name" > <ItemTemplate> <%# Item.Name.ToString() %> </ItemTemplate> </asp:TemplateField> 

It's true? You can't use strong typing with asp: BoundField, asp: DynamicField, or any other possible column controls in a GridView?
For example, when I put the standard asp: BoundField, it did not provide me with any automatic completion of intellisense. I can only type a string for the DataField attribute. For instance:

 <asp:BoundField DataField="JyooobTitle" HeaderText="Job Title" /> 

Oops! It seems that I used the "JobTitle" incorrectly, but the compiler did not notice this. Therefore, in order to capitalize on strong typing, it would seem that I have to use the TemplateField parameter .... for each individual column, whether I need it or not!

Also, if you want to put the SortExpression attribute on your TemplateField ..., you should hope that you say it too, because it does not appear, you also type them a lot. Hopefully I just missed something obvious, because it is a great new feature, but it seems that it was only half implemented .

Can someone check if this is true ... or if something is missing?

+4
source share
2 answers

As a relatively new ASP.NET developer, I probably don't have the understanding or experience to find out the full answer to this question, but I will give my best understanding of the problem. I also used ASP.net 4.5 "strongly typed model binding". When I first discovered this through Scott Hanselman, a great video , it exploded. The issue you mention is certainly an unfortunate limitation of this feature. However, I believe that we should be grateful for the help we receive.

View your other work on the .aspx page. While Intellisense provides some help with filling in control attributes (for example, expanding true / false for Visible= ), checking declarative markup as a whole is very minimal. This is part of his very nature. This is not a โ€œprogrammaticโ€ document, but rather a semantic document. Because of this, it can be changed without recompiling the project, which can be very convenient at times.

ASP.NET provides a method for placing actual compiled code in aspx markup across sectors as a string (<% #%>, etc.). They are like a back door in the programming world and give you the full Intellisense experience.

I expect that someday we will see strong typing in the DataField attribute, but I will not look for it anytime soon. Now I will use ItemType and will delight him with convenience at any time.

0
source

This is how I worked on the problem of binding a sort expression.

Instead of setting the SortExpression name of the real property, I set it to the ordinal position of the column.

 <Columns> <asp:TemplateField HeaderText="Name" SortExpression="1" > <ItemTemplate> <%# Item.Name %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Job Title" SortExpression="2" > <ItemTemplate> <%# Item.JobTitle %> </ItemTemplate> </asp:TemplateField> </Columns> 

Then attach the handler to the gridview Sorting event.

 <asp:GridView runat="server" OnSorting="Sorting" ... 

One of the arguments to the event is the sort expression that you defined earlier (that is, "1" or "2" ). You can use this value to programmatically re-associate the sort expression with the actual property name.

Note : following this approach means refusing to automatically switch between sorting in ascending / descending order. It is not particularly difficult to repeat, though.

Note I use a reflection-based approach to determine property names at runtime.

 protected void Sorting(object sender, GridViewSortEventArgs e) { int columnPosition; if (!int.TryParse(e.SortExpression, out columnPosition)) { return; } switch (columnPosition) { case 1: e.SortExpression = Reflector.GetPropertyName<Employee>(o => o.Name); break; case 2: e.SortExpression = Reflector.GetPropertyName<Employee>(o => o.JobTitle); break; } var grid = (GridView)sender; if (grid.SortExpression == e.SortExpression) { if (grid.SortDirection == SortDirection.Ascending) { e.SortDirection = SortDirection.Descending; } } } 

Finally, here is my code for the Reflector class, which I use in the above example.

 /// <summary>Utility class. Provides static reflection-based utility methods.</summary> public static class Reflector { #region Public Methods and Operators /// <summary>Gets the name of the property that is accessed by the given expression. Usage: <c>Reflector.GetPropertyName&lt;MyClass&gt;(obj =&gt; obj.MyProperty);</c></summary> /// <param name="selectorExpression">The expression that selects the property of which the name should be returned.</param> /// <typeparam name="T">The type that defines the property.</typeparam> /// <returns>The name of the property.</returns> public static string GetPropertyName<T>(Expression<Func<T, object>> selectorExpression) where T : class { MemberExpression memberExpression; var unaryExpression = selectorExpression.Body as UnaryExpression; if (unaryExpression != null) { memberExpression = unaryExpression.Operand as MemberExpression; } else { memberExpression = selectorExpression.Body as MemberExpression; } if (memberExpression == null) { return null; } return memberExpression.Member.Name; } #endregion } 
0
source

Source: https://habr.com/ru/post/1432601/


All Articles