0

i have one page i.e default.aspx and its in the main root and i have another folder which has a file with name of test.aspx . it means my test file is => Folder->test.aspx .

Now can you help me that how can i declare a string variable in my main file ( default.aspx ) and call or change its value in my test.aspx file .

I tryed this code but i didnt get result :-

Thank you

in default.cs

public static class globalvar
{
    public static string test="null";
}

in folder->test.cs

class program
{
    public static void main()
    {
        globalvar.test = "arash";

    }
}
5
  • What do you mean by "didnt get result", was there an error/exception or you didn't see the change ? Commented Jun 26, 2014 at 12:49
  • Shouldn't you use Main (with capital letter). Commented Jun 26, 2014 at 12:49
  • But don't use Session, it would be maintained per user, and by the look of static variable, you are looking at Cache Commented Jun 26, 2014 at 12:51
  • In asp.net you can't simply make objects of class available in code behind. Aspx page is accessed by writing URL in browser. You can pass data between pages using Querystring, Session variables etc. Commented Jun 26, 2014 at 12:51
  • Is this a web site or web application? In an application there a global.asax.cs file in the project. Normally it's public class Global : System.Web.HttpApplication and inside you can put public static strings that you can to use throughout the code. Commented Jun 26, 2014 at 12:56

3 Answers 3

3

Why don´t you use Session state?

in default.cs

public void SetSessionValue (string value)
{
    Session["test"] = value;
}

And on the other page:

in folder->test.cs

public string GetSessionValue ()
{
    return Session["test"];   
}
Sign up to request clarification or add additional context in comments.

4 Comments

can you tell me where exactly should i put Session["test"] = "null";
Actually I was just following your code.. you don´t need to put null... just use Session["test"] = "value"; to set the value you want in any page and string myValue = Session["test"] to get this value =)
in default.cs wherever i add Session["test"] = "null"; it will show me error . can you show me should i add it into any specific class or under the using lines ?
You can use in any class, for example, inside a function. Can you show me your code?
2

There are numerous ways in accomplishing the above said tasks, selecting an appropriate way for an appropriate situation is vested in the hands of programmer. Some of the most prominent techniques are:

  • QueryString.
  • PreviousPage.FindControl() and Request.Form[]
  • Session State
  • Cookies
  • Application Variables
  • Context.Items[]

See link with examples http://www.intstrings.com/ramivemula/asp-net/data-transfer-between-two-asp-net-pages/

Comments

1
  • session

You can use session instead using global .

In your main page you should :

Session["test"] = "arash";

and if you want to access its value just do this :

string test = Session["test"];

For more information take a look : http://www.codeproject.com/Articles/32545/Exploring-Session-in-ASP-Net

  • Url

You can use urlto transfer your value between the pages

here is an example :

http://www.website.com/test/names.asp?test=arash 

And to get the values:

    <%        
      Response.Write(Request.QueryString("test"))

    %>

The file names.asp would display the following:

arash

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.