Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
how to bound a TextBox value with a label in asp.net, SO that whatever be typed in the TextBox it reflects the same in same time in the label.
Posted

You can use the TextBox TextChanged Event to do that.. Text Entered in Textbox will be displayed in Label after hitting enter


C#
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
    Label1.Text = TextBox1.Text;
}



Hope that gona help...
 
Share this answer
 
Comments
kool15th 8-Jul-13 6:17am    
Thanks...But it will only happen when the complete entry have been made in the textbox. I want every instant update.
VICK 8-Jul-13 6:40am    
Than You can use JavaScript for that...
u can use javascript

XML
<script type="text/javascript">
function CalculateBMI() {
            var Weight = parseFloat(document.getElementById('<%=txtWeigh %>').value);
            var Height = parseFloat(document.getElementById('<%=txtHeight %>').value);
            if (!isNaN(Weight) && !isNaN(Height)) {
                var Ms = parseFloat((Height) / 100);
                document.getElementById('<%lblBmi%>').value = parseFloat((Weight) / (Ms * Ms));
                return false;
            }
            else {
                document.getElementById('<%lblBmi%%>').value = "";
                return false;
            }
        }
 </script>;



these are ur textboxes to call javascript method

ASP.NET
<asp:TextBox ID="txtWeight" runat="server"
                onkeyup="CheckFirstChar1(this);CalculateBMI();"
                ></asp:TextBox>

 <asp:TextBox ID="txtHeight" runat="server"
                onkeyup="CheckFirstChar1(this);CalculateBMI();"
                ></asp:TextBox>

Accept as answer if solve you problem.
 
Share this answer
 
v2
Comments
kool15th 8-Jul-13 6:44am    
Their are some error in it which i m not getting...
The one I have corrected txtHeight textbox is not closed.

The error shown is:

Compiler Error Message: CS1002: ; expected

Source Error:


Line 302: #line default
Line 303: #line hidden
Line 304: @__w.Write("\').value = parseFloat((Weight) / (Ms * Ms));\r\n return false;\r\n " +
Line 305: "}\r\n else {\r\n document.getElementById(\'");
Line 306:

Source File: c:\Users\manish\AppData\Local\Temp\Temporary ASP.NET Files\root\1d4d24dc\9b25ae0e\App_Web_webform1.aspx.cdcab7d2.nenlpaot.0.cs Line: 304
kool15th 8-Jul-13 6:45am    
<%@ Page Title="" Language="C#" MasterPageFile="~/admin.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="masterji.WebForm1" %>

and it shows error in the above line too that ";is expeted"
JavaScript
<script>
        function WriteText()
        {
            var TextBox1= document.getElementById('TextBox1');
            var Label1= document.getElementById('Label1');
            Label1.innerText=TextBox1.value;
        }
    </script>



use this WriteText function on onkeyup="WriteText();" of textbox

like this
XML
<asp:textbox id="TextBox1" onkeyup="WriteText();" runat="server" xmlns:asp="#unknown"></asp:textbox>
 
Share this answer
 
v2
actually txtHeight is not closed if u Observe above code u can find out.
that is the Mistake

so pls change as like </asp:TextBox> and remove this CheckFirstChar1(this) method

copy this code
XML
<asp:TextBox ID="txtWeight" runat="server"
                onkeyup="CalculateBMI();"
                ></asp:TextBox>

 <asp:TextBox ID="txtHeight" runat="server"
                onkeyup="CalculateBMI();"
                ></asp:TextBox>


Accept as answer if solve you problem.
 
Share this answer
 
v2

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