Jump to content

Need help to get a String value from one aspx to another

i have 2 aspx.cs files know as messages.aspx.cs and reply.aspx.cs. i want to pass a value of col1 variable from message to reply. i have tried the code below and im unable to find whats wrong with it. im getting an error saying: Error 23 A field initializer cannot reference the non-static field, method, or property 'Test_website.Reply.rv'. What am i doing wrong?


 


------------------------------Messages.aspx.cs--------------------------------------------------------------
public partial class Messages : System.Web.UI.Page
{  
  static String col1;
  protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
  {
     col1= GridView1.SelectedRow.Cells[1].Text;
  }

public string rt()
  {
  return col1;
  }
  
-----------------------------------------------------------------------------------------------------------------
  
---------------------------Reply.aspx.cs---------------------------------------------------------------------
 public partial class Reply : System.Web.UI.Page
    {
   
    Messages rv= new Messages();
    string v = rv.col1;
    }

 

Link to comment
Share on other sites

Link to post
Share on other sites

So the error you're getting is because you're trying to access a static properties from an instance of the object. Static properties are accessed using the class itself, where properties are accessed from the instance. For example:

 

 
//Your class
cass testclass
{
	public static string StaticString="static string";
	public string StringProperty {get;set;}

	public testclass()
	{
		StringProperty="string property";
	}
}

//now somewhere else in your code:
testclass tmpobject = new testclass();

//Now you can access StringProperty using:
Console.WriteLine(tmpobject.StringProperty);

//but you need to access StaticString using:
Console.WriteLine(testclass.StaticString);

//trying to use 
Console.WriteLine(tmpobject.StaticString);
//will result in the same error you're getting now

 

But to answer your question, you can't create an instance of a page in another page. You might be able to use the static property to reference the data, BUT it would not be unique to each user. Using static variables for this is a really bad idea. I would suggest you rather use session variables.

 

To set a session variable, you simply use Session["variablename"]=object; and to read session variables you use yourclass object = (yourclass )Session["variablename"];

 

So in your case: 

 

------------------------------Messages.aspx.cs--------------------------------------------------------------
public partial class Messages : System.Web.UI.Page
{  
  protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
  {
     Session["selectedvalue"] = GridView1.SelectedRow.Cells[1].Text;
  }
}
  
-----------------------------------------------------------------------------------------------------------------
  
---------------------------Reply.aspx.cs---------------------------------------------------------------------
 public partial class Reply : System.Web.UI.Page
    {
    string v = (string)Session["selectedvalue"];
    }

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×