Home   Cover Cover Cover Cover
 

Registration form

Web Page

/book/solutions/6/Registration.aspx
<%@ Page Language="C#" Inherits="Registration" Src="Registration.aspx.cs" %>
<html>
  <head>
    <title>Registration for the .NET seminar</title>
  </head>
  <body>
    <h2>Use this page to register for the .NET seminar</h2>
    <form method="post" Runat="server">
      <table>
        <tr>
          <td>Name</td>
          <td>
            <asp:TextBox ID="name" Width="300" Runat="server"/>
          </td>
        </tr>
        <tr>
          <td>First Name</td>
          <td>
            <asp:TextBox ID="firstName" Width="300" Runat="server"/>
          </td>
        </tr>
        <tr>
          <td>Email</td>
          <td>
            <asp:TextBox ID="email" Width="300" Runat="server"/>
          </td>
        </tr>
        <tr>
          <td> </td>
          <td>
            <asp:CheckBox ID="workshop" Text="with workshop" Runat="server"/>
          </td>
        </tr>
        <tr>
          <td> </td>
          <td>
            <asp:RadioButton ID="programmer" Text="programmer" GroupName="job" Checked="true" Runat="server"/><br>
            <asp:RadioButton ID="webDesigner" Text="web designer" GroupName="job" Runat="server"/><br>
            <asp:RadioButton ID="manager" Text="manager" GroupName="job" Runat="server"/>
          </td>
        </tr>
      </table>
      <br>
      <asp:Button ID="submit" Text="Register" OnClick="Register" Runat="server"/>
      <br><br>
      <asp:Label ID="message" ForeColor="Red" Text="" Runat="server"/>
    </form>
  </body>
</html>

Code Behind

/book/solutions/6/Registration.aspx.cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;

public class Registration : Page {
  protected TextBox name;
  protected TextBox firstName;
  protected TextBox email;
  protected CheckBox workshop;
  protected RadioButton programmer;
  protected RadioButton webDesigner;
  protected RadioButton manager;
  protected Button submit;
  protected Label message;
  
  public void Register(object sender, EventArgs e) {
    if (name.Text == "")
      message.Text = "Please fill in all text fields";
    else {
      try {
        FileStream s = new FileStream(Server.MapPath("registrations.txt"), FileMode.Append, FileAccess.Write);
        StreamWriter w = new StreamWriter(s, Encoding.UTF8);
        w.Write(name.Text + " " + firstName.Text + " " + email.Text);
        if (workshop.Checked) w.Write(" +workshop");
        if (programmer.Checked) w.Write(" programmer");
        else if (webDesigner.Checked) w.Write(" designer");
        else if (manager.Checked) w.Write(" manager");
        w.WriteLine();
        w.Flush();
        s.Close();
        message.Text = "Thank you for registering";
      } catch (IOException) {
        message.Text = "An error occurred while processing your request";
      }
    }
  }
}

Try it

Click here.