| | |
how to dynamically set the text of a label using the text from textbox?
Thread Solved |
•
•
Join Date: Aug 2009
Posts: 9
Reputation:
Solved Threads: 0
Hello,
I am newb to c# and I am working on an example. The challenge is to dynamically set the text of the label using the textboxes' text.
- There are 3 textboxes and a label. All are asp controls.
- The text should be displayed in the label as you type in the textbox and should hold the information of that particular text box when the user goes to another textbox.
- The user enters another string in the 2nd textbox which should add up to the label.
- If the user deletes the text of a textbox that should again remove the text from the label.
Please let me know how to do this with code or an idea.
I found something on internet like label.innerHTML can do this but I am not able to do it.
Thanks in advance!
I am newb to c# and I am working on an example. The challenge is to dynamically set the text of the label using the textboxes' text.
- There are 3 textboxes and a label. All are asp controls.
- The text should be displayed in the label as you type in the textbox and should hold the information of that particular text box when the user goes to another textbox.
- The user enters another string in the 2nd textbox which should add up to the label.
- If the user deletes the text of a textbox that should again remove the text from the label.
Please let me know how to do this with code or an idea.
I found something on internet like label.innerHTML can do this but I am not able to do it.
Thanks in advance!
•
•
Join Date: Jul 2009
Posts: 977
Reputation:
Solved Threads: 224
Note the attributes for the TextBox:
onblur: control losing focus
onkeyup: after each keystroke
The value "UpdateLabel();" tells it to run that script upon those events.
onblur: control losing focus
onkeyup: after each keystroke
The value "UpdateLabel();" tells it to run that script upon those events.
ASP.NET Syntax (Toggle Plain Text)
<head runat="server"> <title></title> </head> <script type="text/javascript"> function UpdateLabel() { document.getElementById('Label1').innerText = document.getElementById('TextBox1').value; } </script> <body> <form id="form1" runat="server"> <div style="height: 83px"> <br /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <asp:TextBox ID="TextBox1" runat="server" onblur="UpdateLabel();" onkeyup="UpdateLabel();"> </asp:TextBox> </div> </form> </body>
•
•
Join Date: Jul 2009
Posts: 977
Reputation:
Solved Threads: 224
I thought I sent this a few minutes ago, but I guess not. Here is another way to do it in the asp form's page load and without the Java function:
ASP.NET Syntax (Toggle Plain Text)
protected void Page_Load(object sender, EventArgs e) { String scriptText = ""; scriptText += "function UpdateLabel2(){"; scriptText += " Label1.innerText = " + " document.forms[0].TextBox1.value"; scriptText += "}"; this.ClientScript.RegisterClientScriptBlock(this.GetType(), "OnKeyUpScript", scriptText, true); TextBox1.Attributes.Add("onkeyup", "UpdateLabel2()"); TextBox1.Attributes.Add("onblur", "UpdateLabel2()"); }
•
•
Join Date: Jul 2009
Posts: 977
Reputation:
Solved Threads: 224
You can just modify the function to concatenate the values and call it to process keyup events for all three textboxes:
ASP.NET Syntax (Toggle Plain Text)
protected void Page_Load(object sender, EventArgs e) { String scriptText = ""; scriptText += "function UpdateLabel2(){"; scriptText += " Label1.innerText = " + " document.forms[0].TextBox1.value + ',' + " + " document.forms[0].TextBox2.value + ',' + " + " document.forms[0].TextBox3.value"; scriptText += "}"; this.ClientScript.RegisterClientScriptBlock(this.GetType(), "OnKeyUpScript", scriptText, true); TextBox1.Attributes.Add("onkeyup", "UpdateLabel2()"); TextBox1.Attributes.Add("onblur", "UpdateLabel2()"); TextBox2.Attributes.Add("onkeyup", "UpdateLabel2()"); TextBox2.Attributes.Add("onblur", "UpdateLabel2()"); TextBox3.Attributes.Add("onkeyup", "UpdateLabel2()"); TextBox3.Attributes.Add("onblur", "UpdateLabel2()"); }
![]() |
Similar Threads
- Changing label text from dataset (C#)
- label that display all textbox input in prose form (C#)
- Dynamically Set CSS Class of an Element (JavaScript / DHTML / AJAX)
- Update label text from another thread (C#)
- Bind data to controls (SQL) (ASP.NET)
- Positioning a long text file in a Text Box (Visual Basic 4 / 5 / 6)
Other Threads in the ASP.NET Forum
- Previous Thread: add data into a sql with a drop down
- Next Thread: "Sign In as different user" is not working in Safari
Views: 1160 | Replies: 18
| Thread Tools | Search this Thread |
Tag cloud for ASP.NET
.net 2.0 3.5 2008 access advice ajax alltypeofvideos anathor asp asp.net asp.net-mvc authentication box browser button c# chat checkbox click connectionstring content control css database datagrid datagridview datalist deployment development dgv dialog directory domain edit editing feedback files flash form form-auth-subdomain formatdecimal forms formview ftp google gridview iframe iis image images impersonation insert javascript john-raasch learn-asp.net linq-sql list login migration mono mouse mssql numerical objects panel pocketpc popup-page print problem profile programmer redirect reports rows running search server session silverlight simple smtp-asp.net source sql sql-server studio suse translate-site update validation vb.net videos vista visual visualstudio web webpage website wizard xml





