ASP.NET 2 Way Databinding Suggestions/Improvements
Hi, i’ve searched the internet to see if others have documented this problem but i could not find anything so i thought i would post it here. The problem i have is with 2 way databinding.
First off I must say that I am a big fan of 2 way databinding introduced in asp.net 2.0 but i feel it can be improved. The main issue i have is that when you using Bind(”name”) the name must be the same both ways. Imagine i have a DateTime field in my database and i have a calendar to handle the date. Now i have three drop down lists to handle the time (one each for hour, minute and second). I would set the SelectedValue for the hour to <%# Bind(”Date”, “{0:HH}”) %> but the name in my update method is DateHour. The only way i can achieve this is to use Eval and then pass the value on the OnInserting/Updating event handler with the appropriate name. My solution to this would be to have an additional databinding method called EvalBind (as an example) which you can pass 2 (or 3 if formatting) parameters. The first would be for the Eval part and the second the Bind part. Using the example above i could then say <%# EvalBind(”Date”, “DateHour”, “{0:HH}”) %>.
However i still have one further issue with the databinding syntax. I am unable to easily add a default value to a control with 2 way databinding without using the code behind file. Therefore i have come up with a better solution which would be to add an additional property to the controls called Bind (again as an example) and then i could say <asp:DropDownList ID=”ddlDateHour” runat=”server” SelectedValue=’<%# Eval(”Date”, “{0:HH}”) %>’ Bind=”DateHour”>…</asp:DropDownList> on my edit page and on my insert page i could set the SelectedValue (or Text for a TextBox) to a default value.
I know my examples are not brilliant but i have come across these problems many times. Also both solutions above are backwards compatible.