Pass variable between methods
-
Hi I am sure this is a stupid question, but I can't work it out and its driving me mad. I have a asp.net C# web page. (Visual Studio 2005) In the class declarations at the top I have declared a
Public String strTeacherName;
In a button click method I give the string a valueprotected void btnBook_Click(object sender, EventArgs e) { strTeachername = "xxx"; }
In a second button click methods I try to use the variable, but it is emptyprotected void btnConfirm_Click(object sender, EventArgs e) { lblMessage.Text = "You booked your lesson with " + strTeacherName; }
I am assuming that the second postback is clearing the value of strTeachername. What is the best way of passing the variable from one method to the other. At the moment I am using a Session variable, but that can't be the best way. Any advice greatly appreciated...Words fade as the meanings change, but somehow, it don't bother me.
-
Hi I am sure this is a stupid question, but I can't work it out and its driving me mad. I have a asp.net C# web page. (Visual Studio 2005) In the class declarations at the top I have declared a
Public String strTeacherName;
In a button click method I give the string a valueprotected void btnBook_Click(object sender, EventArgs e) { strTeachername = "xxx"; }
In a second button click methods I try to use the variable, but it is emptyprotected void btnConfirm_Click(object sender, EventArgs e) { lblMessage.Text = "You booked your lesson with " + strTeacherName; }
I am assuming that the second postback is clearing the value of strTeachername. What is the best way of passing the variable from one method to the other. At the moment I am using a Session variable, but that can't be the best way. Any advice greatly appreciated...Words fade as the meanings change, but somehow, it don't bother me.
i can not see any error in the way u are using the variable. you should look on another place where you are using this variable and making it empty. Try the find utility (Ctrl + F) and type the variable name it will lead u at all the places where the variable is found. if you can provide all the code, maybe i can help more Jamil abou khalil
-
Hi I am sure this is a stupid question, but I can't work it out and its driving me mad. I have a asp.net C# web page. (Visual Studio 2005) In the class declarations at the top I have declared a
Public String strTeacherName;
In a button click method I give the string a valueprotected void btnBook_Click(object sender, EventArgs e) { strTeachername = "xxx"; }
In a second button click methods I try to use the variable, but it is emptyprotected void btnConfirm_Click(object sender, EventArgs e) { lblMessage.Text = "You booked your lesson with " + strTeacherName; }
I am assuming that the second postback is clearing the value of strTeachername. What is the best way of passing the variable from one method to the other. At the moment I am using a Session variable, but that can't be the best way. Any advice greatly appreciated...Words fade as the meanings change, but somehow, it don't bother me.
This question is better in the ASP.NET Message-Board I'm not so good in ASP.NET but i think you must it store in the session variable. Because the page is loaded all the time aat new, so all informations are cleared (re-settet) The Session-Object is all the time avaible. There you stock normally the choises of the Visitor or his entered infos ... So, why not the Teachers Name :) You can also pass the Name by sending it to your requested page via URL, for example But that isn't a good idea
-
Hi I am sure this is a stupid question, but I can't work it out and its driving me mad. I have a asp.net C# web page. (Visual Studio 2005) In the class declarations at the top I have declared a
Public String strTeacherName;
In a button click method I give the string a valueprotected void btnBook_Click(object sender, EventArgs e) { strTeachername = "xxx"; }
In a second button click methods I try to use the variable, but it is emptyprotected void btnConfirm_Click(object sender, EventArgs e) { lblMessage.Text = "You booked your lesson with " + strTeacherName; }
I am assuming that the second postback is clearing the value of strTeachername. What is the best way of passing the variable from one method to the other. At the moment I am using a Session variable, but that can't be the best way. Any advice greatly appreciated...Words fade as the meanings change, but somehow, it don't bother me.
Your class is regenerated every time you postback. Think of each postback as a new instance of the same class. You have several options, including session state ( lives on the server ) and viewstate ( gets encoded in the page, so goes down to the client ). Typically, I would do what you're wanting to do with a property, like this: private const string teacherName = "TeacherName"; private string strTeacherName { get { string s = ViewState[teacherName]; return string.IsNullOrEmpty(s) ? "" : s; } set { ViewState[teacherName] = value; } } I put the key for the viewstate into a constant string so that there is never a mix up between them, and use IsNullOrEmpty to not have to deal with nulls in the rest of my code ( it will be null to start with, obviously. )
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
i can not see any error in the way u are using the variable. you should look on another place where you are using this variable and making it empty. Try the find utility (Ctrl + F) and type the variable name it will lead u at all the places where the variable is found. if you can provide all the code, maybe i can help more Jamil abou khalil
That's because it's an ASP.NET question.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Your class is regenerated every time you postback. Think of each postback as a new instance of the same class. You have several options, including session state ( lives on the server ) and viewstate ( gets encoded in the page, so goes down to the client ). Typically, I would do what you're wanting to do with a property, like this: private const string teacherName = "TeacherName"; private string strTeacherName { get { string s = ViewState[teacherName]; return string.IsNullOrEmpty(s) ? "" : s; } set { ViewState[teacherName] = value; } } I put the key for the viewstate into a constant string so that there is never a mix up between them, and use IsNullOrEmpty to not have to deal with nulls in the rest of my code ( it will be null to start with, obviously. )
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Thanks everyone. I thought the Postback was the problem. I will try your method Christian, thanks.
Words fade as the meanings change, but somehow, it don't bother me.
No worries. You should probably do some reading as well, to get a better idea of the nature of ASP.NET and how your classes are used to generate HTML.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
No worries. You should probably do some reading as well, to get a better idea of the nature of ASP.NET and how your classes are used to generate HTML.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
Yes. I have got a C# book which is good for describing classes and object orientation, and asp.net books which are good for other aspects, but none of them seemed to be able answer this one for me. I think they are too basic. If you have any good suggestions for a suitable book... I have an online voucher for www.ComputerManuals.co.uk to use up. :)
Words fade as the meanings change, but somehow, it don't bother me.
-
Hi I am sure this is a stupid question, but I can't work it out and its driving me mad. I have a asp.net C# web page. (Visual Studio 2005) In the class declarations at the top I have declared a
Public String strTeacherName;
In a button click method I give the string a valueprotected void btnBook_Click(object sender, EventArgs e) { strTeachername = "xxx"; }
In a second button click methods I try to use the variable, but it is emptyprotected void btnConfirm_Click(object sender, EventArgs e) { lblMessage.Text = "You booked your lesson with " + strTeacherName; }
I am assuming that the second postback is clearing the value of strTeachername. What is the best way of passing the variable from one method to the other. At the moment I am using a Session variable, but that can't be the best way. Any advice greatly appreciated...Words fade as the meanings change, but somehow, it don't bother me.
Making it static will also cause the value to persist between postbacks. public static string strTeacherName;
-
Making it static will also cause the value to persist between postbacks. public static string strTeacherName;
That works! Thanks very much. I tried Christian's method but I was just getting a stack overflow exception caused by the Set contructor, for some reason. I still have much to learn. Thank you all for you help.
Words fade as the meanings change, but somehow, it don't bother me.