Bryan
Bryan

Reputation: 8697

How to display random generated ID into more than 1 textbox

I'm randomly generating an ID. i'm able to display out the generated random ID into one textbox via this method.

protected void btnRandom_Click(object sender, EventArgs e)
    {
        Random slumpGenerator = new Random();
        int tal = slumpGenerator.Next(10000, 999999);
        tbpid.Text = tal.ToString();

    }

I'm trying to display this random generated ID into more than 1 textbox which i attempted like this

protected void btnRandom_Click(object sender, EventArgs e)
    {
        Random slumpGenerator = new Random();
        int tal = slumpGenerator.Next(10000, 999999);
        tbpid.Text = tal.ToString();
        tbPassword.Text = tal.ToString();
        tbconfirmpassword.Text = tal.ToString();
    }

However, it doesn't work. I tried changing to

protected void btnRandom_Click(object sender, EventArgs e)
    {
        Random slumpGenerator = new Random();
        int tal = slumpGenerator.Next(10000, 999999);
        tbpid.Text = tal.ToString();
        tbPassword.Text = tbpid.Text;
        tbconfirmpassword.Text = tbpid.Text;
    }

But this particular randomly generated ID did not appear in the password and confirmpassword textbox. It only appears in the pid's textbox. I would like this particular generated ID to appear on all the 3 textbox once i click the generate ID button

Appreciate any help given :)

Upvotes: 1

Views: 664

Answers (3)

Sushant Srivastava
Sushant Srivastava

Reputation: 761

Why don't you do it in Javascript

$( document ).ready(function() {
    $(tbPassword).Value = $(tbpid).Value;
    $(tbconfirmpassword).Value = $(tbpid).Value 
});

Upvotes: 0

Ron Deijkers
Ron Deijkers

Reputation: 3101

I assume that the textboxes tbPassword and tbconfirmpassword have the property TextMode set to password. You then cannot set the value with the Text property. Try changing the TextMode to see if that is indeed the problem.

A workaround is:

tbPassword.Attributes.Add("value", tal.ToString());
tbconfirmpassword.Attributes.Add("value", tal.ToString());

Upvotes: 3

Sergio
Sergio

Reputation: 8259

In ASP.NET you can't set the value of a textbox with textmode="Password" directly (for safety reasons).

Try this instead:

tbPassword.Attributes.Add("value", tal.ToString());

Upvotes: 1

Related Questions