Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 85 - Real time example of stack collection class in c#

Suggested Videos 
Part 82 - Generic Queue collection class
Part 83 - Generic stack collection class
Part 84 - Real time example of queue collection class in c#



Two common scenarios, where a stack can be used.
1. Implementing UNDO functionality 
2. Implementing browser back button

In this video, let's implement BACK button using stack.



Step 1: Create an asp.net web application. Use "WebFormsDemo" as the project name.

Step 2: Right click on the project name in solution explorer, and a class file with name = "BasePage.cs". Copy and paste the following code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebFormsDemo
{
    public class BasePage : System.Web.UI.Page
    {
        protected override void OnLoad(EventArgs e)
        {
            if (Session["URLStack"] == null)
            {
                Stack<string> urlStack = new Stack<string>();
                Session["URLStack"] = urlStack;
            }

            if (Request.UrlReferrer != null && !this.Page.IsPostBack 
                && Session["BackButtonClicked"] == null)
            {
                Stack<string> urlStack = (Stack<string>)Session["URLStack"];
                urlStack.Push(Request.UrlReferrer.AbsoluteUri);
            }

            if (Session["BackButtonClicked"] != null)
            {
                Session["BackButtonClicked"] = null;
            }
        }
    }
}

Step 3: Right click on the project name in solution explorer, and a master page with name = Site.Master. Copy and paste the following code.
<table style="width: 500px; border: 1px solid black">
    <tr>
        <td colspan="2">
            <asp:Label ID="lblMessage" runat="server" ForeColor="Red">
            </asp:Label>
        </td>
    </tr>
    <tr>
        <td style="width: 100px">
            <table style="border: 1px solid black; font-family: Arial">
                <tr>
                    <td>
                        <b>Links</b>
                    </td>
                </tr>
                <tr>
                    <td>
                        <a href="http://localhost/WebFormsDemo/WebForm1.aspx">WebForm1 </a>
                    </td>
                </tr>
                <tr>
                    <td>
                        <a href="http://localhost/WebFormsDemo/WebForm2.aspx">WebForm2 </a>
                    </td>
                </tr>
                <tr>
                    <td>
                        <a href="http://localhost/WebFormsDemo/WebForm3.aspx">WebForm3 </a>
                    </td>
                </tr>
                <tr>
                    <td>
                        <a href="http://localhost/WebFormsDemo/WebForm4.aspx">WebForm4 </a>
                    </td>
                </tr>
            </table>
        </td>
        <td style="width: 400px">
            <asp:Button ID="btnBack" runat="server" Text="Back" OnClick="btnBack_Click" />
            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
            </asp:ContentPlaceHolder>
        </td>
    </tr>
</table>

Step 4: Copy and paste the following code in Site.Master.cs
public partial class Site : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Session["BackButtonClicked"] = "YES";
        if (Session["URLStack"] != null)
        {
            Stack<string> urlStack = (Stack<string>)Session["URLStack"];
            if (urlStack.Count > 0)
            {
                string url = urlStack.Pop();
                Response.Redirect(url);
            }
            else
            {
                lblMessage.Text = "There are no pages in the history";
            }
        }
    }
}

Step 5: Right click on "site.master" page in solution explorer and select "Add Content Page". This step should add WebForm1.aspx. 

In the code-behind file change the following line

public partial class WebForm1 : System.Web.UI.Page

TO
public partial class WebForm1 : BasePage

Repeat Step 5, until you add 4 WebForms.

That's it, navigate to http://localhost/WebFormsDemo/WebForm1.aspx and click on the links to navigate to WebForm2.aspxWebForm3.aspx & WebForm4.aspx
Real time example of stack collection class in c#

Now click on the back button, on the page(Not the browser back  button), and notice that we are able to navigate back.

3 comments:

  1. Can you store multiple data types in System.Array?

    ReplyDelete
    Replies
    1. Yes, if you create the array as an object type. Here's an example.
      object[] myArray = new object[3];
      myArray[0] = "Hello"; // String
      myArray[1] = 101; // Integer
      myArray[2] = true; // Boolean

      foreach (object obj in myArray)
      {
      Console.WriteLine(obj.ToString());
      }

      Delete
  2. Hello Venkat,
    Thanks for quality videos i really appreciate all your efforts.Can you please assist on filesystemwatcher class.
    Thanks

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.