Solved

Copy value from one textbox to another textbox

Posted on 2009-05-06
6
781 Views
Last Modified: 2012-05-06
Hello Guys! I am stuck in a situation where in i need to copy txtbox1's value into txtbox2. I am able to accomplish this using the following code, which is in the page_load event

        txtbox1.Attributes.Add("onblur", String.Format("javascript:document.getElementById('{0}').value=document.getElementById('{1}').value;", txtbox2.ClientID, txtbox1.ClientID))

Everything seems to work perfectly, but to prevent the user from editing the text i have disabled txtbox2. So now what happens is when txtbox2 receives the focus, it gets the value from txtbox1. The problem arises when we go to the next control which is got postback enabled, and when the post back occurs, the value in txtbox2 disappears.

How do I retain the value in the text box. My main aim is that the user should not be able to edit the text in txtbox2.



example.JPG
0
Comment
Question by:preethamonline
6 Comments
 
LVL 33

Expert Comment

by:jppinto
ID: 24312699
0
 
LVL 9

Accepted Solution

by:
RobertNZana earned 500 total points
ID: 24312773
You cant just use Ajax and do something like...

txtCTC.text = txtAllowance.text

Do it on the txtAllowance.text event and enable autopostback.
0
 
LVL 4

Expert Comment

by:cauos
ID: 24312785
you can check the value of the textbox in the page load if is it empty and the second textbox is not empty then assign the value of the first textbox to the second textbox like in the snippet code
<body onload="doChange()">
function doChange()
{
    if (document.getElementById("TextBox1").value =" " && document.getElementById("TextBox2").value !="")
        document.getElementById("TextBox1").value = document.getElementById("TextBox2").value
}

Open in new window

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 3

Expert Comment

by:DreamsTech
ID: 24312870
its happening as the changes made by script on a text box which is disabled are lost during the Postback,means if you populated any value by means of script without manually writing ,these value will be lost during the PostBack.Its a perfect ok and no mistake from ur end.

The work around is ,make a thumb rule that any changes u do in script ,re write the same the same logic in Pre Render Event. In your case ,Populate a Hidden Html Input with the values you populated in your disable TextBox ,and in Prerender load the value from the Html Hidden Input back to your disables textbox
0
 

Author Comment

by:preethamonline
ID: 24313173
Thanks guys for your help. Will try it out and let you know.
0
 

Author Closing Comment

by:preethamonline
ID: 31578370
Thank you all. This was the most effective and simple soluton i felt.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question