Click here to Skip to main content
12,503,334 members (65,371 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET
I have two textboxes.I want to change 2nd textbox value according to whatever i entered in 1st textbox..means suppose i enter 5 in first text and then value 50 should come in 2nd textbox. plz help me?
Posted 16-Feb-13 1:09am
Comments
Nilesh Patil Kolhapur 16-Feb-13 7:49am
   
you should try Sandeep Mewara's answer
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

You need to work on TextBox1_TextChanged event.

protected void TextBox1_TextChanged(object sender,EventArgs e)
{
int example=Convert.ToInt32(Textbox1.Text*10);
Textbox2.Text=Convert.ToString(example);
}

Change the AutoPostBack property As true for TextBox1.


It will display.
TextBox1 TextBox2

1----------->10
2----------->20
3-----------> 30
4-----------> 40
5-----------> 50


Hope it will help you.
  Permalink  
Comments
Sandeep Mewara 16-Feb-13 8:22am
   
Why to do all these when one can achieve it on client side.

If OP was really asking for Server side code, then you have my vote too. But, in that case, OP is really a beginner and should read some book before starting to code.
Gunjan Bhasin 16-Feb-13 13:35pm
   
Whats wrong in the code?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Last time, please try such small things by yourself instead of asking questions here. You need to learn and make some effort.

Steps for your thing:
1. Define a onblur or onfocusout event for the textbox1 (if server control then add through attributes)
2. Define the related method in JavaScript
3. In this method, get the value of textbox1 and then based on it set the value in textbox2

You might need to know:
How to access textbox value in JS:
document.getElementById("textbox1").value
How to add attribute via code:
textbox1.Attributes.Add("onblur","javascript: GetT1ValueAndSetT2(this);");

So overall,
HTML:
<asp:TextBox ID="txtValue1" runat="server" ></asp:TextBox>

and Javascript
<script type="text/javascript">
  function GetT1ValueAndSetT2(txtValue1)
  {
     document.getElementById("txtValue2").value=parseFloat(txtValue1.value,10)*5;
  }
 
</script>
Try now.
  Permalink  
v4
Comments
Nilesh Patil Kolhapur 16-Feb-13 8:11am
   
sir sorry but i tried like <asp:TextBox ID="txtValue2" runat="server" onblur="GetT1ValueAndSetT2(this)" >
and its working fine
sorry if i am wrong....
Sandeep Mewara 16-Feb-13 8:19am
   
Is it? onblur event on server textbox? From when?
If it's an input type textbox, it would but with ASP.NET Textbox control?


BTW, it's ok. You tried to help more, it's just that the addition didn't looked right to me. Keep sharing others and keep good work up. :)
Nilesh Patil Kolhapur 23-Feb-13 4:25am
   
Yes sir u can try. just write client side event like onkeypress directly in server side control script.
try this
   
good answer @sandeep, plain and simple.
Sandeep Mewara 16-Feb-13 8:41am
   
Thanks.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Hi,

You can use jquery to solve the issue. Try this solution below,
<script  type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
 
        $("input[type='text']:eq(0)").keyup(function () {
            var txtVal = $("input[type='text']:eq(0)").val();
            var calculated = parseInt(txtVal) * 10;
            $("input[type='text']:eq(1)").val(calculated);
 
        });
    });
</script>
And here is your TextBox's
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

Hope this will help.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160919.1 | Last Updated 16 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100