Keep (retain) generated number on Button Click using C# in ASP.Net

Last Reply 2 months ago By nagaraju60

Posted 2 months ago

I have a reference number that is auto generated when a web page is loaded, but each time I click on a button on the page the reference number changes. Is it possible to keep the first number that is generated and stop it from changing?

        protected void Page_Load(object sender, EventArgs e)
            string numbers = "1234567890";

            string characters = numbers;
            int length = 7;
            string id = string.Empty;
            for (int i = 0; i < length; i++)
                string character = string.Empty;
                    int index = new Random().Next(0, characters.Length);
                    character = characters.ToCharArray()[index].ToString();
                } while (id.IndexOf(character) != -1);
                id += character;
            Labelreceipt.Text = id;


You are viewing reply posted by: dharmendr 2 months ago.
Posted 2 months ago Modified on 2 months ago

Use the code inside the !this.IsPostBack condition.