I am new to programming. How do I pass the value of a variable to another method? The code variable gets a value, but when the method exits, it is reset to zero. I need to set the visitcode variable to the value of the code variable. I’ve tried declaring a code variable in the public class, but it doesn’t work either. i did it like this
public partial class _Default : System.Web.UI.Page Int32 code = new Int32(); protected void Repeater1_OnItemCommand(object source, RepeaterCommandEventArgs e) { if (e.CommandName == "visitcode") { Int32 code = Convert.ToInt32(e.CommandArgument); } } protected void button1_click(object sender, EventArgs e) { string num = number.Value; string document = doc.Value; string format = "yyyy-MM-dd HH:mm:ss:fff"; string stringDate = DateTime.Now.ToString(format); string visitcode = code }
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Each HTTP request is treated as a new request, this means that your “code” variable is always cleaned every time it reaches the server, so ASP.NET WebForms offers a way to store temporary values in the session variable.
Your code could see like this:
protected void Repeater1_OnItemCommand(object source, RepeaterCommandEventArgs e) { if (e.CommandName == "visitcode") { Session["Code"] = e.CommandArgument; } }
In a second request, you could retrieve the value as follows:
protected void button1_click(object sender, EventArgs e) { ... string visitcode = Session["Code"]; }
As I mentioned, Session variable is temporary so you have to validate if your value is different to NULL, if so this means that the session is over.
I hope it is useful for you
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0