Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Comments
Nilesh Patil Kolhapur 16-Feb-13 7:49am    
you should try Sandeep Mewara's answer

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.
 
Share this answer
 
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?
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:
JavaScript
document.getElementById("textbox1").value

How to add attribute via code:
C#
textbox1.Attributes.Add("onblur","javascript: GetT1ValueAndSetT2(this);");


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


and Javascript
JavaScript
<script type="text/javascript">
  function GetT1ValueAndSetT2(txtValue1)
  {
     document.getElementById("txtValue2").value=parseFloat(txtValue1.value,10)*5;
  }

</script>

Try now.
 
Share this answer
 
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
Shahriar Iqbal Chowdhury/Galib 16-Feb-13 8:39am    
good answer @sandeep, plain and simple.
Sandeep Mewara 16-Feb-13 8:41am    
Thanks.
Hi,

You can use jquery to solve the issue. Try this solution below,
XML
<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
XML
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>


Hope this will help.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900