ASP.NET IronPython Resource

Learn python and .net

IronPython - Dynamically creating objects and binding them to a form

Coding is fun and all, but what if we could generate a form instantly without having to create sql statements or create classes?  Sure, LINQ does that, but it's annoying to have to recreate the LINQ-TO-SQL file every time the database schema changes. Since IronPython is a dynamic language, it has the ability to add new members to a type at runtime.

I've created two classes. One creates an object on the fly from the table schema. The second one binds the object to the controls on the page, based off the article Using Reflection to Bind Business Objects to ASP.NET Form Controls. Fortunately, there is no need to use reflection in Python, making it quicker than its C# counterpart.

First, let's create a simple form from the Purchasing.Vendor table.
 

<color:blue; "><asp:Label ID="uxMessage" runat="server" />
    <asp:Panel ID="Panel1" runat="server">
        <table>
            <tr>
                <th>
                    Account Number
                </th>
                <td>
                    <asp:TextBox ID="uxAccountNumber" runat="server" />
                </td>
            </tr>
            <tr>
                <th>
                    Name
                </th>
                <td>
                    <asp:TextBox ID="uxName" runat="server" />
                </td>
            </tr>
            <tr>
                <th>
                    Credit Rating
                </th>
                <td>
                    <asp:TextBox ID="uxCreditRating" runat="server" />
                </td>
            </tr>
            <tr>
                <th>
                    Preferred Vendor Status
                </th>
                <td>
                    <asp:RadioButtonList ID="uxPreferredVendorStatus" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow">
                        <asp:ListItem Value="True">Yes</asp:ListItem>
                        <asp:ListItem Value="False">Yes</asp:ListItem>
                    </asp:RadioButtonList>
                </td>
            </tr>
            <tr>
                <th>
                    Active Flag
                </th>
                <td>
                    <asp:RadioButtonList ID="uxActiveFlag" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow">
                        <asp:ListItem Value="True">Yes</asp:ListItem>
                        <asp:ListItem Value="False">Yes</asp:ListItem>
                    </asp:RadioButtonList>
                </td>
            </tr>
            <tr>
                <th>
                </tholor:#A31515; ">th>
                <td>
                    <asp:TextBox ID="uxPurchasingWebServiceURL" runat="server" />
                </td>
            </tr>
        </table>
        <br />
        <asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="btnSubmit_Click" />
    </asp:Panel>


Next, for the code-behind, I've added to references to the DBObject and Formbinder classes.

import clr
clr.AddReference('System.Data')
clr.AddReference('System.Configuration')
clr.AddReference('System.Web')
from System import *
from System.Data import *
from System.Configuration import *
from System.Web import *
from System.Web.Security import *
from System.Web.UI import *
from System.Web.UI.WebControls import *
from System.Web.UI.WebControls.WebParts import *
from System.Web.UI.HtmlControls import *
from DBObject import *
from FormBinder import *

id = 8

def Page_Load(sender, e):
    if not sender.IsPostBack:
        Form.DataBind()

        obj = DBObject("Purchasing.Vendor", id) # Get record from database
        FormBinder.BindObjectToControls(obj, Panel1, "ux") # Populate form controls from record

def btnSubmit_Click(sender, e):
    obj = DBObject("Purchasing.Vendor", id)
    obj.ModifiedDate = DateTime.Now
    FormBinder.BindControlsToObject(obj, Panel1, "ux")
    obj.Update()
    uxMessage.Text = "Record has been updated!"

In the Page_Load method, there are three lines. The Form.DataBind method is called to bind all the datasources before we populate them. In the next line, a new object is created from the Purchasing.Vendor table. The id of the record is passed in the constructor and the object will be created with all the properties and data from the table. The BindObjectsToControls method will go through all the properties of the object we created, and bind the values to the controls on the page that have the corresponding id. I prefix my controls with "ux", so I've added that as the third parameter. Simple form binding in only three lines. I've used the id of "8" in this scenario. In a live form, you will probably want to use a querystring variable.

Now to update the record, create the object again. As you can see in the second line of the btnSubmit_Click method, I've set the ModifiedDate to the current time, demonstrating that the property has been created and can be used. I then call the BindControlsToObject method to get the data from the form and update the object. The Update method will update the data in the database.

 

The source code can be downloaded below. As you can see, quick form binding with no need to worry about updating class files when the schema changes are writing crud methods. You can inherit the DBObject class to extend its functionality. 

IronPython Dynamic Data Access.zip (800.80 kb)

Posted: Aug 23 2008, 02:03 by Administrator | Comments (619) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: IronPython ASP.NET
Tags:
Social Bookmarks: E-mail | Kick it! | DZone it! | del.icio.us

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading