|
Who Are We?Fluent Consulting is a software development and consulting firm that specializes in enterprise application integration, web applications, and software product development. We are a dedicated team focused on providing the highest level of quality and value for our clients. Please feel free to visit our corporate site or get in touch! |
Bi-directional Data Binding for ASP.NET 1.0/1.1
There's only so much casting, converting, parsing that one can take trying to push and pull data from custom objects into form controls. Although, ASP.NET 2.0 has improved the situation, there's another simple model to package up some of this common code into a reusable component. To implement this strategy, we will use a core class, our DataBindingManager, and a series of subclassed components that wrap our common code, our DataBindings.<label for="TextBoxName">Name:</label> <asp:TextBox ID="TextBoxName" Runat="server" /><br /> <label for="TextBoxEstimatedHours">Estimated Hours:</label> <asp:TextBox ID="TextBoxEstimatedHours" Runat="server" /><br /> <label for="DropDownListClient">Client:</label> <asp:DropDownList CssClass="dropDown" ID="DropDownListClient" Runat="server" /><br /> <asp:CheckBox ID="CheckBoxIsActive" Runat="server" Text="Is Active?" /><br /> <asp:Button Runat="server" Text="Save"OnClick="ButtonSave_OnClick" ID="ButtonSave"/> <DataBinding:DataBindingManager ID="DataBindingManagerProject" runat="server"> <DataBinding:TextBoxBinding ControlToBind="TextBoxName" DataMember="Name" /> <DataBinding:TextBoxBinding ControlToBind="TextBoxEstimatedHours" DataMember="EstimatedHours" /> <DataBinding:ListControlBinding ControlToBind="DropDownListClient" DataMember="ClientID" /> <DataBinding:CheckBoxBinding ControlToBind="CheckBoxIsActive" DataMember="IsActive" /> </DataBinding:DataBindingManager>For the DataBinding objects, we first define a base class DataBindingBase, which handles the common behavior of finding the 'bound' control and handling basic conversions, converting, or parsing to a desired type. With the reuse provided by this model we can spend a little more time and handle parsing sqltypes, nulls, etc. Then we can subclass this control for more specific behaviors related to our various controls, such as TextBoxes, ListControls, Calendars, etc. When we are done, this gives us a simple and single interface to handle our data binding.
protected DataBindingManager DataBindingManagerProject; protected DropDownList DropDownListClients; private void Page_Load(object sender, System.EventArgs e) { if(!IsPostBack){ //Bind list of clients DropDownListClients.DataSource = Client.FindAll(); DropDownListClients.DataValueField = "ID"; DropDownListClients.DataTextField = "Name"; DropDownListClients.DataBind(); //Bind our object to form DataBindingManagerProject.DataSource = Project.FindByID(Request["ID"]); DataBindingManagerProject.PushData(); } } public void ButtonUpdate_OnClick(object sender, System.EventArgs e){ Project p = Project.FindByID(Request["ID"]); //Set our object with data from form DataBindingManagerProject.DataSource = p; DataBindingManagerProject.PullData(); //Save to database p.Update(); }
This entry was posted in the following categories:
.NET
Posted by mlehman at April 12, 2006 04:09 PM
Comments
Copyright © 2002-2003 Fluent Consulting. All rights reserved.



