How to change text of Label

Last post 09-09-2008 9:54 AM by atanu.podder. 7 replies.

Sort Posts:

  • How to change text of Label

    09-09-2008, 5:04 AM
    • Member
      61 point Member
    • mm8
    • Member since 02-07-2008, 6:51 PM
    • Posts 400

    I have a javascript to change the textattribute on a label at runtime. The problem is that the value is not saved when the page is postbacked and now I wonder how to change the text value of a label with AJAX? The script does like this: document.getElementById("id").innerHTML = "something"; and it works fine until the I make a postback, then the info gets lost. Any suggestions how to implement this in AJAX or in any other wat make the value stay during postbacks?

  • Re: How to change text of Label

    09-09-2008, 5:38 AM
    • Member
      488 point Member
    • hemantksh
    • Member since 09-08-2005, 2:22 PM
    • ND, India
    • Posts 79

    You can have a hidden text field and store the desired label text in this field too. When the page is posted back, check for the value in this hidden field. If there is some value, set the label's text to this value.

  • Re: How to change text of Label

    09-09-2008, 5:54 AM
    • Member
      61 point Member
    • mm8
    • Member since 02-07-2008, 6:51 PM
    • Posts 400

    Do you mean an asp:HiddenField or a input type=hidden? I guess these values are lost as well?

  • Re: How to change text of Label

    09-09-2008, 6:26 AM
    • Member
      488 point Member
    • hemantksh
    • Member since 09-08-2005, 2:22 PM
    • ND, India
    • Posts 79

    Use asp:HiddenField. The value won't get lost.

  • Re: How to change text of Label

    09-09-2008, 6:32 AM
    • Member
      77 point Member
    • churrasco
    • Member since 07-30-2008, 1:12 PM
    • Posts 20

    Better than use labels in order to use javascript to modify them, try to use <span> or <div> html elements, and add runat="server" to them to make them accesible from the codebehind.

    If you want to keep your Labels in code, then you will need to save the modified value in a asp:hiddenField or in a input="hidden" runat="server" item, and on postback update the Label with the saved value. 

    r
  • Re: How to change text of Label

    09-09-2008, 6:37 AM

     u can use asp:hiddenfields for the purpose.

    Regards,Meetu Choudhary
    Microsoft MVP-ASP/ASP.NET

    MsDnM || My Forums || My Blog
  • Re: How to change text of Label

    09-09-2008, 9:22 AM
    • Member
      4 point Member
    • arockiaraj
    • Member since 09-08-2008, 11:32 AM
    • Posts 4

    set the follwing property in that hidden fiels

    <input id="hidTextBoc" type="hidden" value="0" enableviewstate="true" runat="server"/>

    enableviewstate="true" will maintain the textbox data when postback happaning.

    and run at server will user user to we can able to get this control on the server script with out any type cast or findcontrol methord

    try this and reply me

    Thanks,
    Arockiaraj

    P.S. Dont forget to mark this as answer, if you have got resolution to your problem from this post.
  • Re: How to change text of Label

    09-09-2008, 9:54 AM
    Answer
    • Participant
      1,274 point Participant
    • atanu.podder
    • Member since 08-20-2008, 7:40 AM
    • Bangalore
    • Posts 191

     

    Labels don't post their innerText values to the server during a postback. That's just not how html works. You could make a custom control that stored the value in both the innerText and some hidden input, then use the hidden input value on the serverside.

    Instead of a label, you could also use a read-only textbox. You can then use CSS to style it so that it looks nothing like a textbox.

    Visit http://forums.asp.net/t/416926.aspx.

     

    Thanks,
    Atanu, Symphony Services

    P.S. Dont forget to mark this as answer, if you have got resolution to your problem from this post.
Page 1 of 1 (8 items)