Is there a way to bind the content of a textbox in another text box? I mean, the second one, should displayed a calculation based on the fisrt thextbox.
You can use javascript to do this functionality in a easier way, just write some function in javascript , for example function add(), which performs addition of textbox1 value and 10(assumed) and the result will be assigned to the textbox2 and make this
add() function raised on the onblur event in textbox1.
Ch Mahesh Kumar
www.jkc-forum.in Mark this post as answer if this helps.It can save others time.
Thanks for your help. The code mentioned above will work, however the real problem I'm facing is that the textbox is inside a repeater control. I've done something similar. This is a piece of the repeater control code (itemTemplate section)
function txtActWeightingKeyUp(sender){ var container = document.getElementById("<%=
rptActivityWeighting.ClientID %>"); var sumPrice
= container.getElementsByTagName("txtPrice");
for(i = 0; i < sumPrice .length; i++){
alert(sumPrice[i].value);
}
}
The problem is that the sumPrice variable doesn't have anyting. the idea is to have a collection of all textboxes render by the repeater control.
Does anyone know how I can refer to those textboxes inside a repeater control once it has been render in html?
Can't you retrived the calculated value of the field directly from the db table and assign it to the second text box as you do for the first textbox in the repeater?
B
Kindly mark this post as "Answer", if it helped you.
Your code works :-). I just adapted it to my page and it worked. I'm not very familiar with jQuery, however that code is pretty easy to understand. I've read a couple of documentation about jQuery. Thanks a lot for your help.
Likewise, I would like to commented that I achieved the same functionallity in javascript. This is the code:
function MyFunction() { var sumPrice = 0; var container
= document.getElementById("<%= MyRepeaterID.ClientID %>");
//here we get the collection
of all the input elements that are render inside the repeater control var txtPrices
= container.getElementsByTagName("input"); for(i = 0;
i < txtPrices.length; i++){ //validate that only the inputs of text are take in consideration if(txtPrices[i].attributes["type"].value
!= "hidden" && txtPrices[i].attributes["id"].value.lastIndexOf
("txtPrice") != -1){ sumPrice += parseInt(txtPrices[i].attributes["value"].value); }
} //for
The last line of code is for a textbox in the footer section of my Repeater control which only display the total of all the other textboxes.
Just in case that someone need to do this in javascript I pasted the code, but jQuery really do the same with less code. I will learning jQuery :-). Seems interesting
Cheers!!
Marked as answer by Lesthad_mk on Mar 01, 2012 09:36 PM
Lesthad_mk
Member
96 Points
159 Posts
How to bind a textbox to another textbox?
Feb 29, 2012 11:24 PM|LINK
Hi all
Is there a way to bind the content of a textbox in another text box? I mean, the second one, should displayed a calculation based on the fisrt thextbox.
I appreaciate your help
ItsSunny
Contributor
2163 Points
676 Posts
Re: How to bind a textbox to another textbox?
Mar 01, 2012 03:00 AM|LINK
Arun Sunny.
MaheshKumarC...
Member
84 Points
51 Posts
Re: How to bind a textbox to another textbox?
Mar 01, 2012 03:24 AM|LINK
You can use javascript to do this functionality in a easier way, just write some function in javascript , for example function add(), which performs addition of textbox1 value and 10(assumed) and the result will be assigned to the textbox2 and make this add() function raised on the onblur event in textbox1.
www.jkc-forum.in
Mark this post as answer if this helps.It can save others time.
ramiramilu
All-Star
95443 Points
14106 Posts
Re: How to bind a textbox to another textbox?
Mar 01, 2012 03:11 PM|LINK
something like this -
<%@ Page Language="C#" AutoEventWireup="True" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Calendar Example</title> <script language="C#" runat="server"> protected void Page_Load(object sender, EventArgs e) { } </script> <script type="text/javascript"> function call() { var num = parseInt(document.getElementById('TextBox1').value); document.getElementById('TextBox2').value = num * 2; return false; } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return call()" /> </div> </form> </body> </html>JumpStart
Lesthad_mk
Member
96 Points
159 Posts
Re: How to bind a textbox to another textbox?
Mar 01, 2012 04:43 PM|LINK
Hi all
Thanks for your help. The code mentioned above will work, however the real problem I'm facing is that the textbox is inside a repeater control. I've done something similar. This is a piece of the repeater control code (itemTemplate section)
<ItemTemplate>
<tr>
<td valign="middle" style='white-space:nowrap; text-align:left; width: 75%; height: 40%'>
<asp:Label ID="lblActivityName" runat="server" Text="SomeText"></asp:Label>
</td>
<td valign="middle" style='white-space:nowrap; text-align:left; height: 40%'>
<asp:Textbox ID="txtPrice" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Price") %>' Width="45" CssClass="small" onChange="javascript:txtActWeightingKeyUp(this)"></asp:Textbox>
The javascript function is:
function txtActWeightingKeyUp(sender){
var container = document.getElementById("<%= rptActivityWeighting.ClientID %>");
var sumPrice = container.getElementsByTagName("txtPrice");
for(i = 0; i < sumPrice .length; i++){
alert(sumPrice[i].value);
}
}
The problem is that the sumPrice variable doesn't have anyting. the idea is to have a collection of all textboxes render by the repeater control.
Does anyone know how I can refer to those textboxes inside a repeater control once it has been render in html?
This is the url where I got the idea: http://www.eggheadcafe.com/community/asp-net/17/10259647/getting-value-of-checkbox-through-javascript-in-repeater.aspx
basheerkal
Star
10672 Points
2426 Posts
Re: How to bind a textbox to another textbox?
Mar 01, 2012 04:58 PM|LINK
Hi
Can't you retrived the calculated value of the field directly from the db table and assign it to the second text box as you do for the first textbox in the repeater?
B
(Talk less..Work more)
me_ritz
Star
9337 Points
1447 Posts
Re: How to bind a textbox to another textbox?
Mar 01, 2012 05:00 PM|LINK
If you can use some jquery:
<asp:Repeater ID="rptActivityWeighting" runat="server"> <ItemTemplate> <asp:TextBox ID="txtPrice" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Price") %>' Width="45" CssClass="small"></asp:TextBox> </ItemTemplate> </asp:Repeater> ------------------------------------------- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function () { $("input[id*=txtPrice]").keyup(function () { var sum = 0; $("input[id*=txtPrice]").each(function () { alert(Number($(this).val())); //Individual Price TextBoxes sum += Number($(this).val()); }, null); alert(sum);//Sum of all Price TextBoxes }); }); </script>Lesthad_mk
Member
96 Points
159 Posts
Re: How to bind a textbox to another textbox?
Mar 01, 2012 09:36 PM|LINK
Hi me_ritz
Your code works :-). I just adapted it to my page and it worked. I'm not very familiar with jQuery, however that code is pretty easy to understand. I've read a couple of documentation about jQuery. Thanks a lot for your help.
Likewise, I would like to commented that I achieved the same functionallity in javascript. This is the code:
function MyFunction()
{
var sumPrice = 0;
var container = document.getElementById("<%= MyRepeaterID.ClientID %>");
//here we get the collection of all the input elements that are render inside the repeater control
var txtPrices = container.getElementsByTagName("input");
for(i = 0; i < txtPrices.length; i++){
//validate that only the inputs of text are take in consideration
if(txtPrices[i].attributes["type"].value != "hidden" && txtPrices[i].attributes["id"].value.lastIndexOf ("txtPrice") != -1){
sumPrice += parseInt(txtPrices[i].attributes["value"].value);
}
} //for
txtPrices[txtPrices.length-1].attributes["value"].value = sumPrice;
}
The last line of code is for a textbox in the footer section of my Repeater control which only display the total of all the other textboxes.
Just in case that someone need to do this in javascript I pasted the code, but jQuery really do the same with less code. I will learning jQuery :-). Seems interesting
Cheers!!