Tuesday, 9 December 2014

Pin It

Widgets

3 Layered ASP.NET Application Example - Part 4


In this post, we will create UI for PersonalTaskTracker application.
  • Open Web.config file of the application.
  • Add the following lines to the Web.config file.


Web.config
 
<connectionstrings>
  <add connectionstring="data source=YOUR_SERVER_NAME; uid=YOUR_USER_ID; Pwd=YOUR_PASSWORD; database=PTT" name="PersonalTaskTrackerConnectionString">
 </add>
</connectionstrings>

Right-click on PersonalTaskTracker project and add a new class named Resource.cs and add the following code. Resource.aspx
namespace PersonalTaskTracker
{
    /// 
    /// Resource class. It contains static strings, constants etc.
    /// 
    public static class Resource
    {
        /// 
        /// Tag for Open status.
        /// 
        public const int OPENSTATUS = 1;

        /// 
        /// Tag for Done status.
        /// 
        public const int DONESTATUS = 2;

        /// 
        /// URL parameter value for delete operation.
        /// 
        public static string delete = "delete";

        /// 
        /// URL parameter value for edit operation.
        /// 
        public static string edit = "edit";

        /// 
        /// URL parameter value for add operation.
        /// 
        public static string add = "add";

        /// 
        /// URL parameter value for view operation.
        /// 
        public static string view = "view";

        /// 
        /// URL parameter for type of operation occurred like for example deleted or edit.
        /// 
        public static string operation = "op";

        /// 
        /// URL parameter for id of the task.
        /// 
        public static string idParameter = "id";

        /// 
        /// URL for Home page.
        /// 
        public static string HomeURL = "Home.aspx";
    }
}

Now, right-click on PersonalTaskTracker project and add a new Webform and name it Home.aspx

Home.aspx
 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="PersonalTaskTracker.Home" EnableEventValidation="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
            <asp:Label ID="lblAddEdit" runat="server" Font-Bold="True" Font-Size="Larger" 
                ForeColor="Blue"></asp:Label>
            <br />
            <br />
            ID               
            <asp:Label ID="lblTaskId" runat="server"></asp:Label>
                                                                           
            <asp:Button ID="btnAddNewTask" runat="server" Font-Bold="True" 
                ForeColor="#FF6600" onclick="btnAddNewTask_Click1" Text="Add  NewTask" 
                Visible="False" />
            <br />
            <br />
            <asp:Label ID="lblSummaryTxt" runat="server" Text="Summary"></asp:Label>
   
        <asp:TextBox ID="txtSummary" runat="server" Width="221px"></asp:TextBox>
 <br />
            <br />
            <asp:Label ID="lblDescriptionTxt" runat="server" Text="Description"></asp:Label>
 
        <asp:TextBox ID="txtDescription" runat="server" Height="66px" TextMode="MultiLine" Width="228px"></asp:TextBox>
            <br />
            <br />
 <asp:Label ID="lblStatusTxt" runat="server" Text="Status" Visible="False"></asp:Label>
     
            <asp:CheckBox ID="cbxStatus" runat="server" Text="Closed" Visible="False" />
            <br />
            <br />
                            
        <asp:Button ID="btnUpdateTask" runat="server" Text="Update" OnClick="btnUpdateTask_Click" />
    
                                
        <asp:Button ID="btnCancel" runat="server" Text="Cancel" 
                OnClick="btnCancel_Click" />
    
        <br />
        <br />
        <asp:GridView ID="gvTaskList" runat="server" AllowPaging="True" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnPageIndexChanging="gvTaskList_PageIndexChanging">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:TemplateField HeaderText="ID">
                    
                    <ItemTemplate>
                        <asp:Label ID="lblTaskId" runat="server" Text='<%# Bind("TaskID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Summary">
                    
                    <ItemTemplate>
                        <asp:Label ID="lblSummary" runat="server" Text='<%# Bind("Summary") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Description">
                    
                    <ItemTemplate>
                        <asp:Label ID="lblDescription" runat="server" Text='<%# Bind("Description") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Created On">
                    
                    <ItemTemplate>
                        <asp:Label ID="lblCreatedOn" runat="server" Text='<%# Bind("CreatedOn") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Status">
                    
                    <ItemTemplate>
                        <asp:Label ID="lblStatusName" runat="server" Text='<%# Bind("StatusName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:HyperLink ID="HyperLink2" runat="server" Text="Edit" NavigateUrl='<%# string.Format("Home.aspx?id={0}&op={1}", Eval("TaskID").ToString(), "edit") %>'></asp:HyperLink>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:HyperLink ID="HyperLink1" runat="server" Text="Delete" onClick="javascript: return confirm('Are you sure you want to delete?');" NavigateUrl='<%# string.Format("Home.aspx?id={0}&op={1}", Eval("TaskID").ToString(), "delete") %>'></asp:HyperLink>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:HyperLink ID="HyperLink3" runat="server" Text="View" NavigateUrl='<%# string.Format("Home.aspx?id={0}&op={1}", Eval("TaskID").ToString(), "view") %>'></asp:HyperLink>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>
 <br />
    
    </div>
    </form>
</body>
</html>

Home.aspx.cs
 
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;

namespace PersonalTaskTracker
{
    /// <summary>
    /// Home class.
    /// </summary>
    public partial class Home : System.Web.UI.Page
    {
        /// <summary>
        /// List of tasks.
        /// </summary>
        List<Task> taskList = new List<Task>();

        /// <summary>
        /// Page Load method. It runs whenever the page loads.
        /// </summary>
        /// <param name="sender">sender.</param>
        /// <param name="e">arguments of the event occurred.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                taskList = TaskList.GetAllTasks();
                gvTaskList.DataSource = taskList;
                gvTaskList.DataBind();
                lblAddEdit.Text = "Add New Task";

                if (Request.QueryString[Resource.operation] == Resource.edit || Request.QueryString[Resource.operation] == Resource.view)
                {
                    if (Request.QueryString[Resource.operation] == Resource.edit)
                    {
                        lblAddEdit.Text = "Edit Task"; 
                    }
                    else
                    {
                        lblAddEdit.Text = "View Task";

                        txtDescription.Enabled = false;
                        txtSummary.Enabled = false;
                        cbxStatus.Enabled = false;
                        btnUpdateTask.Visible = false;
                        btnCancel.Visible = false;
                    }

                    int taskId = int.Parse(Request.QueryString["id"]);
                    Task task = Task.GetTask(taskId);

                    txtDescription.Text = task.Description;
                    txtSummary.Text = task.Summary;
                    lblTaskId.Text = task.TaskID.ToString();
                    lblStatusTxt.Visible = true;
                    cbxStatus.Visible = true;
                    btnAddNewTask.Visible = true;

                    if (task.StatusID == Resource.OPENSTATUS)
                    {
                        cbxStatus.Checked = false;
                    }
                    else if (task.StatusID == Resource.DONESTATUS)
                    {
                        cbxStatus.Checked = true;
                    }
                }
                else if (Request.QueryString[Resource.operation] == Resource.delete)
                {
                    int taskId = int.Parse(Request.QueryString[Resource.idParameter]);
                    Task task = Task.GetTask(taskId);
                    task.DeleteTask();
                    Response.Redirect(Resource.HomeURL);
                }
            }
        }

        protected void gvTaskList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvTaskList.PageIndex = e.NewPageIndex;
            gvTaskList.DataSource = taskList;
            gvTaskList.DataBind();
        }

        /// <summary>
        /// Event called whenever Add button is clicked.
        /// </summary>
        /// <param name="sender">sender.</param>
        /// <param name="e">arguments of the event occured.</param>
        protected void btnAddNewTask_Click(object sender, EventArgs e)
        {
            Task task = new Task();
            task.Summary = txtSummary.Text;
            task.Description = string.Empty;
            task.CreatedOn = DateTime.Today;
            task.StatusID = Resource.OPENSTATUS;

            task.AddTask();

            Response.Redirect(Resource.HomeURL);
        }

        /// <summary>
        /// Event called whenever Update button is clicked.
        /// </summary>
        /// <param name="sender">sender.</param>
        /// <param name="e">arguments of the event occurred.</param>
        protected void btnUpdateTask_Click(object sender, EventArgs e)
        {
            if (Request.QueryString[Resource.operation] == Resource.edit)
            {
                int taskId = int.Parse(Request.QueryString[Resource.idParameter]);
                Task task = Task.GetTask(taskId);

                task.Description = txtDescription.Text;
                task.Summary = txtSummary.Text;

                task.StatusID = (cbxStatus.Checked == true) ? Resource.DONESTATUS : Resource.OPENSTATUS;
                task.UpdateTask();
            }
            else if (Request.QueryString[Resource.operation] == Resource.add || Request.QueryString[Resource.operation] == null)
            {
                lblAddEdit.Text = "Add New Task";
                Task task = new Task();
                task.Summary = txtSummary.Text;
                task.Description = txtDescription.Text;
                task.StatusID = Resource.OPENSTATUS;
                task.CreatedOn = DateTime.Now;
                task.AddTask();
            }

            Response.Redirect(Resource.HomeURL);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(Resource.HomeURL);
        }

        protected void btnAddNewTask_Click1(object sender, EventArgs e)
        {
            Response.Redirect(Resource.HomeURL);
        }
    }
}
Now, right-click on PersonalTaskTracker project and select Set As StartUp Project. Then, right-click on Home.aspx and select Set As Start Page and hit F5.

No comments: