0

Guys I want to ask that I have a control naming page1.ascx it has a tool bar and i have there select and close button. on toolbar . I want that when I go from page2.ascx control to page.ascx control then show that button (Select and Close) but when i come from page3.ascx control to Page1.ascx then don't show that button(Select and close) How to do it?

  • create your required public properties in your user controls. and set it according to your requirement.. http://stackoverflow.com/questions/1179645/user-control-ascx-and-properties – Vishal Sharma Nov 02 '13 at 09:13

1 Answers1

0

When you are redirecting one web page to another, you are passing values stored in variables, so 4 ways you can do that

1) Session Variable Previous page/Viewstate Variable

2)QueryString http://mysite.com/currentpage?from=previousPage

3)Using Property of a class eg. By setting Property set while redirecting and get while printing.

Here is another way.

Try it

public string GetCurrentPageName()
{
    string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
    System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
    string sRet = oInfo.Name;
    return sRet;
}


public string GetPreviousPageName()
{
    string sPath = Page.PreviousPage.Request.Url.AbsolutePath;
    System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
    string sRet = oInfo.Name;
    return sRet;
}

If you want Previouspage title then you can try this :

string PPagetitle= this.Page.PreviousPage.Title;

Or you can do little bit Javascript like this :

<asp:button id="m_BackButton" runat="server" onclientclick="goBack()" />

<script type="text/javascript">
   function goBack()
   {
       history.go(-1);
   }
</script>

Explanation of 3rd one : While moving from one web page to another you can pass data through class properties - but it works for Server.Transfer() only and destination page you need to define

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

.Here is the demo

// Source page

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div><div>

        <asp:Label ID="lblUsername" runat="server" BorderStyle="None" Font-Bold="True" Font-Names="Garamond"
            Font-Size="Large" Style="z-index: 100; left: 240px; position: absolute; top: 32px"
            Text="Username" Width="73px"></asp:Label>
       <br />

        <asp:Label ID="lblPassword" runat="server" BorderStyle="None" Font-Bold="True" Font-Names="Garamond"
            Font-Size="Large" Style="z-index: 101; left: 237px; position: absolute; top: 80px"
            Text="Password" Width="80px"></asp:Label>
       <br />
       <br />
        <asp:TextBox ID="txtPassword" runat="server" Style="z-index: 102; left: 355px; position: absolute;
            top: 80px" TextMode="Password" Width="151px"></asp:TextBox>
        <asp:TextBox ID="txtUsername" runat="server" Style="z-index: 103; left: 357px; position: absolute;
            top: 30px" Width="153px"></asp:TextBox>
        <asp:Label ID="lblMessage" runat="server" Font-Bold="False" Font-Names="Bookman Old Style"
            Font-Size="Medium" Style="z-index: 104; left: 354px; position: absolute; top: 130px"
            Text="Message :"></asp:Label>
        <asp:Button ID="btnSubmit" runat="server" Font-Bold="True" Font-Names="Garamond"
            Font-Size="Large" OnClick="btnSubmit_Click" Style="z-index: 106; left: 289px;
            position: absolute; top: 160px" Text="Submit" />
    </div>

    </div>
    </form>
</body>
</html>

Source page code behind :

 using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    private string myUserName;
    /*
     * Defining Properties in the source page to be Accessible on the destination page.
     * means Exposing data to other pages using Properties
     * To retrieve data from source page,Destination page must have 
     * <%@ PreviousPageType VirtualPath="~/Default.aspx" %> Directive added below <%@ Page %> Directive

     */
    public string propUserName
    {
        get { return myUserName; }
        set { myUserName = value; }
    }
    private string myPassword;

    public string propPassword
    {
        get { return myPassword; }
        set { myPassword = value; }
    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if ((txtUsername.Text == "chandan") && (txtPassword.Text == "niit"))
        {
            myUserName = txtUsername.Text;
            myPassword = txtPassword.Text;
        }
       Server.Transfer("Description.aspx");
    }
}

Destination Page :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Description.aspx.cs" Inherits="Description" %>
<%@ PreviousPageType VirtualPath="~/Default.aspx" %> 
<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            &nbsp;
             <asp:Label ID="Label2" runat="server" Text="Password" style="z-index: 100; left: 336px; position: absolute; top: 69px" Font-Bold="True" Font-Size="Larger"></asp:Label>
            <asp:Label ID="Label1" runat="server" Text="UserName" style="z-index: 102; left: 333px; position: absolute; top: 28px" Font-Bold="True" Font-Size="Larger"></asp:Label>
        </div>
    </form>
</body>
</html>

Destination Page Code Behind :

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Description : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = PreviousPage.propUserName;
        Label2.Text = PreviousPage.propPassword;

    }
}
Chandan Kumar
  • 4,570
  • 4
  • 42
  • 62
  • but in your question, you have asked that you are moving from one .aspx page to another .aspx page. As per your question this answer is. Please modify your question or post a new question. – Chandan Kumar Nov 02 '13 at 08:12
  • yes but i just want to make clear my problem.thanks for you time – Ahsan Shoukat Nov 02 '13 at 08:13
  • can you explain it with example pls: 3)Using Property of a class eg. By setting Property set while redirecting and get while printing. – Ahsan Shoukat Nov 02 '13 at 08:27