Thursday, May 8, 2014

Pass Values Between ASP.NET Web Pages without Session or any State Management

If the source page and target page are both ASP.NET Web pages in the same Web application, and if you transfer execution from the source page to the target page on the server by using the transfer method, the target page can access public properties in the source page.

Page One

<asp:TextBox ID="textCity" runat="server" Text="jainik"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

 public String CurrentCity
        {
            get
            {
                return textCity.Text;
            }
        }

        public List<String> Current
        {
            get
            {
                return _Current;
            }
        }
        private List<String> _Current = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            _Current = new List<String>();
            _Current.Add("1");
            _Current.Add("2");
            _Current.Add("3");
            _Current.Add("4");
            _Current.Add("5");


        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Server.Transfer("PageTwo.aspx");
        }


Page Two

<%@ PreviousPageType VirtualPath="~/PageOne.aspx" %> 

        public string strName { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            strName = PreviousPage.CurrentCity;
            List<String> Current = PreviousPage.Current;
        }

1 comment: