how to dynamically set the text of a label using the text from textbox?

Please support our ASP.NET advertiser: 50% off 6 Months Dedicated Server Hosting from 1&1!
Thread Solved

Join Date: Aug 2009
Posts: 9
Reputation: maybach_hp is an unknown quantity at this point 
Solved Threads: 0
maybach_hp maybach_hp is offline Offline
Newbie Poster

how to dynamically set the text of a label using the text from textbox?

 
0
  #1
Aug 20th, 2009
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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 977
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 224
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: how to dynamically set the text of a label using the text from textbox?

 
0
  #2
Aug 20th, 2009
Does this label1.Text need to be updated after each key stroke in the TextBox, or can it be updated after user has finished editing?
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 9
Reputation: maybach_hp is an unknown quantity at this point 
Solved Threads: 0
maybach_hp maybach_hp is offline Offline
Newbie Poster

Re: how to dynamically set the text of a label using the text from textbox?

 
0
  #3
Aug 20th, 2009
It has to be updated for each key stroke. Like we type in the textbox and the letters start appearing in the label.

Thanks!
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 977
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 224
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: how to dynamically set the text of a label using the text from textbox?

 
0
  #4
Aug 20th, 2009
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.

  1. <head runat="server">
  2. <title></title>
  3. </head>
  4. <script type="text/javascript">
  5. function UpdateLabel()
  6. {
  7. document.getElementById('Label1').innerText = document.getElementById('TextBox1').value;
  8. }
  9. </script>
  10. <body>
  11. <form id="form1" runat="server">
  12. <div style="height: 83px">
  13. <br />
  14. <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
  15. <asp:TextBox ID="TextBox1" runat="server" onblur="UpdateLabel();" onkeyup="UpdateLabel();">
  16. </asp:TextBox>
  17. </div>
  18. </form>
  19. </body>
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 9
Reputation: maybach_hp is an unknown quantity at this point 
Solved Threads: 0
maybach_hp maybach_hp is offline Offline
Newbie Poster

Re: how to dynamically set the text of a label using the text from textbox?

 
0
  #5
Aug 20th, 2009
Thankyou very much. It works like charm!
But this works with one text box. How can I do with 3 textboxes?
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 977
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 224
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: how to dynamically set the text of a label using the text from textbox?

 
0
  #6
Aug 20th, 2009
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:
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. String scriptText = "";
  4. scriptText += "function UpdateLabel2(){";
  5. scriptText += " Label1.innerText = " +
  6. " document.forms[0].TextBox1.value";
  7. scriptText += "}";
  8. this.ClientScript.RegisterClientScriptBlock(this.GetType(),
  9. "OnKeyUpScript", scriptText, true);
  10. TextBox1.Attributes.Add("onkeyup", "UpdateLabel2()");
  11. TextBox1.Attributes.Add("onblur", "UpdateLabel2()");
  12. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 977
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 224
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: how to dynamically set the text of a label using the text from textbox?

 
0
  #7
Aug 20th, 2009
Originally Posted by maybach_hp View Post
Thankyou very much. It works like charm!
But this works with one text box. How can I do with 3 textboxes?
OK. You need to concatenate all three textboxes into the one label?
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 9
Reputation: maybach_hp is an unknown quantity at this point 
Solved Threads: 0
maybach_hp maybach_hp is offline Offline
Newbie Poster

Re: how to dynamically set the text of a label using the text from textbox?

 
0
  #8
Aug 20th, 2009
Yes that's right. I have to concatenate the text from textboxes.

Like textbox1.text <space or symbol> textbox2.text and so on...
Last edited by maybach_hp; Aug 20th, 2009 at 2:00 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 977
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 224
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: how to dynamically set the text of a label using the text from textbox?

 
0
  #9
Aug 20th, 2009
You can just modify the function to concatenate the values and call it to process keyup events for all three textboxes:
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. String scriptText = "";
  4. scriptText += "function UpdateLabel2(){";
  5. scriptText += " Label1.innerText = " +
  6. " document.forms[0].TextBox1.value + ',' + " +
  7. " document.forms[0].TextBox2.value + ',' + " +
  8. " document.forms[0].TextBox3.value";
  9. scriptText += "}";
  10. this.ClientScript.RegisterClientScriptBlock(this.GetType(),
  11. "OnKeyUpScript", scriptText, true);
  12. TextBox1.Attributes.Add("onkeyup", "UpdateLabel2()");
  13. TextBox1.Attributes.Add("onblur", "UpdateLabel2()");
  14. TextBox2.Attributes.Add("onkeyup", "UpdateLabel2()");
  15. TextBox2.Attributes.Add("onblur", "UpdateLabel2()");
  16. TextBox3.Attributes.Add("onkeyup", "UpdateLabel2()");
  17. TextBox3.Attributes.Add("onblur", "UpdateLabel2()");
  18. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 9
Reputation: maybach_hp is an unknown quantity at this point 
Solved Threads: 0
maybach_hp maybach_hp is offline Offline
Newbie Poster

Re: how to dynamically set the text of a label using the text from textbox?

 
0
  #10
Aug 20th, 2009
Brilliant! I was actually working like that. But now the problem is when the user deletes the text in the textbox the ',' should also go right?

How to remove that?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1160 | Replies: 18
Thread Tools Search this Thread



Tag cloud for ASP.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC