Reputation: 73
I have problem with one variable.
protected void godziny_Click(object sender, EventArgs e)
{
var id_filmu = Request["id"];
var data = Request["data"];
var godzina = TimeSpan.Parse(hidden2.Value);
var query = from h in bazaDC.seanses
where h.godzina == godzina && h.id_filmu == int.Parse(id_filmu) && h.data == DateTime.Parse(data)
select h;
foreach (var a in query)
{
Session["id_seansu"] = a.id_seansu;
}
}
id_seansu is declared outside function, just in partial class. I have to get that variable in another function:
protected void rezerwujButton_Click(object sender, EventArgs e)
{
DateTime dzisiejszaData = DateTime.Today;
TimeSpan godzinaRezerwacji = DateTime.Now.TimeOfDay;
DateTime dataZarezerwowania;
TimeSpan czasZarezerwowania;
var query = from wszystkieRezerwacje in bazaDC.rezerwacjes
select wszystkieRezerwacje;
foreach(var i in query)
{
if(i.data_rezerwacji.HasValue && i.czas_rezerwacji.HasValue)
{
dataZarezerwowania = i.data_rezerwacji.Value;
czasZarezerwowania = i.czas_rezerwacji.Value;
}
}
rezerwacje nowaRezerwacja = new rezerwacje();
if (Session["id_seansu"] != null)
{
Response.Write(Session["id_seansu"]);
};
/*nowaRezerwacja.imie_klienta = imieTextBox.Text;
nowaRezerwacja.nazwisko_klienta = nazwiskoTextBox.Text;
nowaRezerwacja.email_klienta = emailTextBox.Text;
nowaRezerwacja.nrtel_klienta = nrKomTextBox.Text;
nowaRezerwacja.numer_miejsca = Hidden1.Value;
nowaRezerwacja.data_rezerwacji = dzisiejszaData;
nowaRezerwacja.czas_rezerwacji = godzinaRezerwacji;
nowaRezerwacja.id_seansu = id_seansu;
bazaDC.rezerwacjes.InsertOnSubmit(nowaRezerwacja);
bazaDC.SubmitChanges();*/
}
But when I wanna write that variable by Response.Write("id_seansu") in rezerwujButton_Click It is always "0". But when I wanna write it in godziny_Click It have correct value. Why variable is getting 0 value in another function?
Upvotes: 0
Views: 62
Reputation: 29006
When you perform the first click, the page will post back and hence the value of the variable get reset, to Persist Variable on Postback you have to use either session or ViewState, if the you want this variable in other pages then you can go ahead with session if it is specifically for this page then you have to opt ViewState. You can assign like this:
ViewState.Add("id_seansu","some value");
And get value like this:
if (ViewState["id_seansu"] != null)
{
var id_seansu = ViewState["id_seansu"];
}
Upvotes: 1