ProMesh.NET Quick Walkthrough - Part 5 - Form Handling
Handling forms is one of the most important features of any web application framework. ProMesh.NET handles this quite a bit differently than most other frameworks, but we believe it is very elegant and powerful.
We'll show the basic by presenting a small example of a form to edit an employee record:
[Layout("master")]
[View("editemployee")]
public class EditEmployee : PageController
{
public class Employee
{
public string Name;
public decimal Salary;
}
public class EmployeeForm : WebForm
{
private Employee _employee;
[FormTextBox(MinLength=3,MaxLength=30)]
public string Name;
[FormTextBox(Min=0)]
public decimal Salary;
public EmployeeForm(Employee employee)
{
_employee = employee;
}
// Called when first showing the form
public override void OnFill()
{
Name = _employee.Name;
Salary = _employee.Salary;
}
// Called when the form was posted and all fields are valid
public override void OnPost()
{
_employee.Name = Name;
_employee.Salary = Salary;
}
}
public void Run(int id)
{
Employee employee = new Employee(id);
EmployeeForm form = new EmployeeForm(employee);
form.Bind();
if (form.Validated)
{
employee.Save();
}
}
}
editemployee.htm
<html>
<body>
<form method="post" action="$[_SELF_]">
<label>Name</label> $[[Name]]
<label>Salary></label> $[[Salary]]
<input type="submit" name="btnSave" value="Save" />
</form>
</body>
</html>
The $[[Name]] and $[[Salary]] tags are replaced by text boxes. When posting the form, the input is validated according to the attributes in the form class. If there is a validation error, the values entered in the text boxes will be remembered across postbacks.
Of course you can do more than display text boxes. Other stuff you can do:
- Use dropdowns, radio buttons, checkboxes or any other control
- Create your own controls and attributes
- Use custom validation methods
- Assign CSS classes
- ...
The documentation for these features will be added in the next few days.
Next >>
Executing other methods