Variable scope problem?

  • Thread starter Thread starter Ciaran
  • Start date Start date
C

Ciaran

The variable value will be lost between postbacks. If it need to be
persisted it should be saved to teh view state by the control and got back
when a postback occurs.

Ciaran
 
Hi,

You could simply save it in a session as
Session("myvar") = myvar;

later you retrieve it using

myvar = Session("myvar").ToString();

Please note that you should make sure that you saved the variable before
retrieve , usually you do so in the onload event checking for the IsPostBack
property.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
 
Hi

I have the following code and why can't i change the value of myvar in the
DataGrid1_editcommand event so that the (changed) value of myvar is there in
the Update event?? (it fires i debugged it!):

namespace MYproject

{

using System;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;



public class MyUsercontrol: System.Web.UI.UserControl

{

public string myvar ="hello";



private void Page_Load(object sender, System.EventArgs e)

{


}


public void DataGrid1_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)

{


myvar ="goodbye"; //enters this code first


}



private void DataGrid1_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)

{

string temp; // enters this code next!!!

temp = myvar; // myvar = "hello" ???? instead of the wanted "goodbye"


}

}

}

ch Tom
 
Back
Top