1
I’m making a website in ASP.NET where I have an array type variable of type int in a Webform (we’ll call it Default.aspx.cs
) and would like to pass this variable to another Webform (Data.aspx.cs
).
I think it’s important to note that the way I’m moving from one page to the next is by using Server.Transfer.
Is there any way to make a constructor that passes the variable to Data.aspx.Cs?
I would also like to add that I already used the following method but could not specify the index of the array:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Page lastPage = (Page)Context.Handler;
if (lastPage is WebForm2)
{
Label1.Text = ((WebForm1)lastPage).dados.ToString();
}
}
}
The dados
is an encapsulation method with the array inside.
If you notice what is wrong and if you can help me use this method that is already somewhat familiar to me, please do so.
can use Session, already tried?
– Ricardo Pontual
@Ricardopunctual I don’t know this method. Could explain to me how it works or direct me to somewhere that explains me?
– Guilherme Oliveira
Session is an object in the context of the server that is a dictionary, that is, you can store a data in that object and reference by a key,:
Session["Dados"] = dados
. On the other page just read this value with the same key "Data":var dados = Session["Dados"]
. Note that in Session, everything isobject
, i.e., you need to cast to be able to reuse. Imagine that your data variable is an int array:int[] dados
, when reading this Session value, you need to convert:var dados = (int[])Session["Dados"]
– Ricardo Pontual
@Ricardopunctual it is necessary to make another type of connection between the Forms?
– Guilherme Oliveira
@Ricardopunctual in my case, would declare as Session["array"] = array; and in the other form declare as
int[]array = (int[])Session["array"];
right?– Guilherme Oliveira
yes, you can use the "array" key, but prefer something more explanatory if you can. Ah, as you are about to cast the array, you can declare so on the second page:
var array = (int[])Session["array"];
– Ricardo Pontual