AqDWeb : FirstForm

HomePage :: Categories :: PageIndex :: Login/Register
HomePage - Tutorials - First Form

First Form

Step 1 -- The Model Class

A plain and simple user profile:
  1. public class User {
  2.     private string _name;
  3.     private int _age;
  4.  
  5.     public string Name {
  6.         set { _name = value; }
  7.         get { return _name; }
  8.     }
  9.  
  10.     public string Age {
  11.         set { _age = value; }
  12.         get { return _age; }
  13.     }
  14. }

Step 2 -- Mapping to Database

Map your class to database table(s) using NHibernate:
  1. using NHMA = NHibernate.Mapping.Attributes;
  2.  
  3. // Map your class to the [User] table
  4. [NHMA.Class]
  5. // The class AqDWeb.Utils.PersistentEntity includes basic entity properties such as PersistenceID
  6. // (GUID, primary key) and a version field for nhibernate to track state.
  7. public class User: AqDWeb.Utils.PersistentEntity {
  8.     private string _name;
  9.     private int _age;
  10.  
  11.     // Map to [User].[Name] column, type NVARCHAR(200)
  12.     [NHMA.Property(NotNull=true, Length=200)]
  13.     public string Name {
  14.         set { _name = value; }
  15.         get { return _name; }
  16.     }
  17.  
  18.     // Map to [User].[Age] column, type INT
  19.     [NHMA.Property(NotNull=true)]
  20.     public int Age {
  21.         set { _age = value; }
  22.         get { return _age; }
  23.     }
  24. }
Now you can either create the schema by yourself, or just open http://localhost/YourApp/NHibernateInfo.axd, paste & execute the schema creation SQL (at the bottom). You will also find the generated mapping xml there. PS: Don't worry about table/column name conflict with reversed words in your db - all of them are quoted by default.

Step 3 -- Write your code-behind page class

A page that takes one query parameter "id" to update existing user profile, or create a new user if there is no id given:
  1. public class MyPage: AqDWeb.Base.ManagedPage {
  2.     // UserID will be set to the ID in request query string (and the type is converted to Guid).
  3.     // If there isn't any ID given, its value will remain to be Guid.Empty or whatever you set below.
  4.     [QueryParameter("id")]
  5.     public Guid UserID = Guid.Empty;
  6.  
  7.     public User User = null;
  8.  
  9.     // OnDataBinding is the place where you prepare the model objects for the view layer to use.
  10.     // It could be called more than once - I'll explain the details later.
  11.     protected override void OnDataBinding(EventArgs e) {
  12.         if (UserID != Guid.Empty)
  13.             User = AqDWeb.Services.PersistentEntityBroker<User>.FindByID(UserID);
  14.         else
  15.             User = new User();
  16.     }
  17.  
  18.     // This method will be invoked when "save" button is pressed. Its return value is an URL that this
  19.     // request will be redirected to, or null to stay in the same page.
  20.     public virtual string SaveUser() {
  21.         AqDWeb.Services.PersistentEntityBroker<User>.Persist(User);
  22.         return null;
  23.     }
  24. }

Step 4 -- Write your aspx page!

Now the easist part comes :)
  1. <%@ Page Inherits="MyPage" %>
  2. <html>
  3.     <head></head>
  4.     <body>
  5.         <h1>You're editing user profile for ${User.Name}:</h1>
  6.         <form runat="server">
  7.             ID: <span runat="server" bind-innerText="'[' + User.ID + ']'"/>
  8.             Name: <input runat="server" type="text" bind-value="User.Name"/><br/>
  9.             Age: <input runat="server" type="text" bind-value="User.Age"/><br/>
  10.             <input runat="server" type="submit" value="Save" ext-serverClick="SaveUser()"/>
  11.         </form>
  12.     </body>
  13. </html>
In real world the binding is much more complex than that and you will need to consider a bit more things (we haven't mentioned loops and state management yet). But for a simple form, that's all!
  1. Note that you can use C#-like syntax inside the bind-whatever attributes - it's OGNL, visit http://www.ognl.org/2.6.9/Documentation/html/LanguageGuide/index.html if you want to learn more.
  2. The ${something} syntax, new in 0.9.1.0, it's for all literal cntrols. However there is no way to escape } yet ;)
SourceForge.net Logo
Valid XHTML 1.0 Transitional :: Invalid CSS :: Powered by Wikka Wakka Wiki 1.1.6.1
Page was generated in 0.3931 seconds