locked
Get value from JavaScript set label RRS feed

  • Question

  • User-94466089 posted
    I have a JavaScript which sets the innerHTML attrubute of a asp:Label like this:

    ..... 
    sb.Append(
    "document.getElementById(\"" + lbl.ClientID + "\").innerHTML = 'something....';");

    But on postback I can't retrieve the value by calling lbl.Text as it's blank...how to save the javascript set value between postbacks?

    Friday, September 5, 2008 11:32 AM

Answers

  • User533502567 posted

    It should work..one option is using "Session variable". Populate session variable in JS and read between postbacks.

     "Hidden Field" also a good opion.., Populate hiddenfield in JS and read from code-behind.. 

    <input id="Hidden1" type="hidden" runat="server" />
     

    Check this link, if you want to call server side function from java script

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 5, 2008 12:42 PM
  • User-2115483147 posted
    I have a JavaScript which sets the innerHTML attrubute of a asp:Label like this:

    ..... 
    sb.Append(
    "document.getElementById(\"" + lbl.ClientID + "\").innerHTML = 'something....';");

    But on postback I can't retrieve the value by calling lbl.Text as it's blank...how to save the javascript set value between postbacks?

    Hi mm8,

    Please refer the following code to solve your problem,

    <%@ Page Language="C#" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Response.Write(lblMessage.Text);
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            lblMessage.Text = hdText.Value;
        }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>Label Demo</title>
    
        <script language="javascript" type="text/javascript">
        function setValue()
        {
            document.getElementById("lblMessage").innerHTML ="I Love ASP.NET !";  
            document.getElementById("hdText").value = document.getElementById("lblMessage").innerHTML;  
            alert(document.getElementById("lblMessage").innerHTML);
        }
        </script>
    
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
         
            <asp:Label ID="lblMessage" runat="server" Text="Demo"></asp:Label>
            <asp:Button ID="btnSubmit"
                runat="server" Text="Submit" OnClientClick="setValue();" 
                onclick="btnSubmit_Click" />
            <asp:HiddenField ID="hdText" runat="server" />
        </div>
        </form>
    </body>
    </html>
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 10, 2008 5:39 AM
  • User213768233 posted

    It looks to me that you are mixing code behind added javascript rather than run time javascript. The time that the script runs will decide the final value. I coud be wrong on this next one, but I believe that setting the value of an asp label would be document.getElementById('lblClientId').value = "something...."

     

    Hope that helps. If not then post some more of your code

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 10, 2008 6:05 AM

All replies

  • User533502567 posted

    It should work..one option is using "Session variable". Populate session variable in JS and read between postbacks.

     "Hidden Field" also a good opion.., Populate hiddenfield in JS and read from code-behind.. 

    <input id="Hidden1" type="hidden" runat="server" />
     

    Check this link, if you want to call server side function from java script

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 5, 2008 12:42 PM
  • User-94466089 posted
    What is "PageMethods" in the link example?
    Friday, September 5, 2008 1:29 PM
  • User-94466089 posted

    Pagemethods is undefined it says....a void function can't return a string and a hyperlink doesn't have a onclientclick property...could please give a better example? How to change the value of a label during runtime using javascript??

    Tuesday, September 9, 2008 3:53 AM
  • User-2115483147 posted
    I have a JavaScript which sets the innerHTML attrubute of a asp:Label like this:

    ..... 
    sb.Append(
    "document.getElementById(\"" + lbl.ClientID + "\").innerHTML = 'something....';");

    But on postback I can't retrieve the value by calling lbl.Text as it's blank...how to save the javascript set value between postbacks?

    Hi mm8,

    Please refer the following code to solve your problem,

    <%@ Page Language="C#" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Response.Write(lblMessage.Text);
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            lblMessage.Text = hdText.Value;
        }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>Label Demo</title>
    
        <script language="javascript" type="text/javascript">
        function setValue()
        {
            document.getElementById("lblMessage").innerHTML ="I Love ASP.NET !";  
            document.getElementById("hdText").value = document.getElementById("lblMessage").innerHTML;  
            alert(document.getElementById("lblMessage").innerHTML);
        }
        </script>
    
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
         
            <asp:Label ID="lblMessage" runat="server" Text="Demo"></asp:Label>
            <asp:Button ID="btnSubmit"
                runat="server" Text="Submit" OnClientClick="setValue();" 
                onclick="btnSubmit_Click" />
            <asp:HiddenField ID="hdText" runat="server" />
        </div>
        </form>
    </body>
    </html>
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 10, 2008 5:39 AM
  • User213768233 posted

    It looks to me that you are mixing code behind added javascript rather than run time javascript. The time that the script runs will decide the final value. I coud be wrong on this next one, but I believe that setting the value of an asp label would be document.getElementById('lblClientId').value = "something...."

     

    Hope that helps. If not then post some more of your code

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 10, 2008 6:05 AM