rai nalasa
rai nalasa

Reputation: 859

Label changes during page load

I'm trying to capture the value of my password to Label. 4 digit letter and 1 lower case letter

This is my method to add both digit and num

public void SaveTransactionID()
{
    string password = lblStart.Text + lblStop.Text;
    lblPassword.Text = password;
}

The generators:

private void GenRandomNumber()
{
    Random generator = new Random();
    String r = generator.Next(0, 10000).ToString("D4");
    lblStart.Text = r;
}

//Generate Random Letter
static class RandomLetter
{
    static Random _random = new Random();
    public static char GetLetter()
    {
        // This method returns a random lowercase letter.
        // ... Between 'a' and 'z' inclusize.
        int num = _random.Next(0, 26); // Zero to 25
        char let = (char)('a' + num);
        return let;
    }
}

My page load

protected void Page_Load(object sender, EventArgs e)
{
    char lowerCase;
    lowerCase = Convert.ToChar(RandomLetter.GetLetter());
    lblStop.Text = lowerCase.ToString();
    GenRandomNumber();
}

I know that my password will change every page load. That is why I tried to save it on my Label so I could capture the password in case the page loads again. But the things is my SaveTransactonId() also change during page load. How could I store the value of my password even with page load?

Upvotes: 0

Views: 998

Answers (3)

Here's an example:

protected void Page_Load(object sender, EventArgs e)
    {
       if (!IsPostBack)
        {
           char lowerCase;
           lowerCase = Convert.ToChar(RandomLetter.GetLetter());
           lblStop.Text = lowerCase.ToString();

           GenRandomNumber();
        }
    }

This will solve your problem.

EDIT:

Here's a short explanation of what conditions occur when IsPostBack = true or false. For a single computer for developing and debugging code, the "Client" is your browser and the "Server" is your computer. (In the linked article, the question is not "What is IsPostBack?" The correct question is "What is PostBack?" There is a better, more intricate diagram; I cannot find it, but this'll do.)

PostBack is the name given to the process of submitting an ASP.NET page to the server for processing. PostBack is done if (for example) certain credentials of the page are to be checked against some sources (such as verification of username and password for a database). This is something the client is not able to accomplish on its own and thus these details have to be 'posted back' to the server via user interaction.

A postback is round trip from the client (Browser) to the server and then back to the client. This enables your page to go through the asp engine on the server and any dynamic content to be updated.

For a more detailed answer to the PostBack question, see here.

Here is a description of the ASP.NET (web-) page life cycle overview, some of which involve PostBack.

Upvotes: 2

Saurabhchauhan232
Saurabhchauhan232

Reputation: 89

write your code inside if(!Page.IsPostBack){// put your logic here.}

and You can save your value in Session["sessionKey"] = value;

and you can retrieve it by checking session is not null

if(Session["sessionKey"] !=null);

lblPassword.Text = Session["sessionKey"];

Upvotes: 1

CoolBots
CoolBots

Reputation: 4889

You can store the value in a Session variable; you can also control what runs in Page_Load on initial page load vs. subsequent page reloads (per session) via Page.IsPostBack property.

Upvotes: 0

Related Questions