Strongly-Typed Container.DataItem? Why Not?
This type of code is really annoying:
<asp:GridView ID="ctlGridBrowse" runat="server"
AutoGenerateColumns="false"
>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%#
(Container.DataItem as MyItemType).ItemTitleFormatted %>
</ItemTemplate>
</asp:TemplateField>
Even worse is Eval("ItemTitleFormatted").
A pure hack to get around this is something like:
<asp:GridView ID="ctlGridBrowse" runat="server"
AutoGenerateColumns="false"
>
<Columns>
<asp:TemplateField Visible="
false">
<ItemTemplate><%#
SetItem(Container.DataItem) %></ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ItemID" HeaderText="Item #" />
<asp:TemplateField>
<ItemTemplate>
<%#
Item.ItemTitleFormatted %>
</ItemTemplate>
</asp:TemplateField>
Where SetItem is a function that sets a Page level property of type MyItemType, so now the rest of the row has Item as a strongly typed, page-level property. But, this is not very clean. What about having multiple types of grids or lists on a page? It would be very cumbersome.
In ASP 3.0 you could use the With statements and simply do <%= .ItemTitleFormatted %>
Now, we have these huge drawn out casting or "
Eval"ing statements which have no intellisense and no compile time safety.
Shouldn't it be possible to specify a datatype for databound list template and then Container.DataItem (at worst) would be strongly typed? Since code is being generated that ends up looking something like:
public void @__DataBind__control15(object sender, System.EventArgs e) {
System.Web.UI.IDataItemContainer Container;
System.Web.UI.DataBoundLiteralControl target;
#line 31 "C:\Documents and Settings\jgough\My Documents\Visual Studio 2005\Projects\TestTelerikGrid.aspx"
target = ((System.Web.UI.DataBoundLiteralControl)(sender));
#line default
#line hidden
#line 31 "C:\Documents and Settings\jgough\My Documents\Visual Studio 2005\Projects\TestTelerikGrid.aspx"
Container = ((System.Web.UI.IDataItemContainer)(target.BindingContainer));
#line default
#line hidden
#line 31 "C:\Documents and Settings\jgough\My Documents\Visual Studio 2005\Projects\TestTelerikGrid.aspx"
target.SetDataBoundString(0, System.Convert.ToString(SetItem(Container.DataItem), System.Globalization.CultureInfo.CurrentCulture));
#line default
#line hidden
}
Couldn't we get to a point where something like:
<asp:GridView ID="ctlGridBrowse" runat="server"
AutoGenerateColumns="false"
Type="MyItemType"
>
Ends up causing the generated code to create a generic instance of Container, maybe something like:
System.Web.UI.IDataItemContainer<MyItemType> Container;
And then IDataItemContainer<MyItemType>.DataItem is actually of type MyItemType?
I understand the rationale of having these databinding methods be in page scope, so that it would never really be possible to just do Item.SomeProperty, but at least we could get to:
Container.DataItem.SomeProperty without having to do:
Ev

al("SomeProperty"); // no strong type, no compile time safety
or
((MyItemType)Container.DataItem).SomeProperty // better but verbose and ugly
or
(Container.DataItem as MyItemType).SomeProperty // less ugly but not the kind of stuff you want non-programmers to be playing around with when shuffling the UI around.
I'd say we should even have a "with like" syntax where you could just do:
<asp:GridView ID="ctlGridBrowse" runat="server"
AutoGenerateColumns="false"
Type="MyItemType"
>
<asp:TemplateField>
<ItemTemplate>
<%#
.ItemTitleFormatted %>
</ItemTemplate>
</asp:TemplateField>
And then when the code is generated it translates this to Container.DataItem.ItemTitleFormatted