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.

5 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
  3. Plz help me.. Iam geeting the following error

    Server Error in '/' Application.

    Compilation Error

    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS0426: The type name 'Site1' does not exist in the type 'System.Collections.Stack'

    Source Error:


    Line 172: }
    Line 173:
    Line 174: [TemplateContainer(typeof(Stack.Site1))]
    Line 175: [TemplateInstanceAttribute(System.Web.UI.TemplateInstance.Single)]
    Line 176: public virtual System.Web.UI.ITemplate Template_head {

    ReplyDelete
  4. Hi Vankat
    Navigating WebForm2 via the a href tag causes
    Server Error in '/' Application.
    The resource cannot be found.
    Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. Requested URL: /WebFormsDemo/WebForm2.aspx. When you open the developer tools in IE11 it says This is causing a DOM7011: The code on this page disabled back and forward caching.
    Is there a way to resolve this? I will continue to look for solutions but your help would be appreciated so I can go to next tutorial.

    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.