0

I have been through 4hrs of posts now. I can not get this js code to work. This is the latest iteration, I have tried many combinations.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebFormRedisTest.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

<script type="text/javascript">

    function updateText(strData) {
        debugger;
        document.getElementById("<%=lbl.ClientID%>").innerHTML(strData);
    }

</script> 
</head>
<body>
    <form id="form1" runat="server">
     <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Test_OnClientClick"/>

        <div>
        </div>
        <asp:Label ID="lbl" runat="server" Text="Label" ClientIDMode="Static"></asp:Label>
    </form>
</body>
</html>

public void Test_OnClientClick(object sender, EventArgs e)
        {
            Console.WriteLine("test");
            ScriptManager.RegisterStartupScript(this, this.GetType(), "updateText", "updateText('test');", true);
        }
3
  • Open the devtools of your browser and check for error messages in the console. Also check whether the generated JavaScript is what you expect Commented Feb 28, 2020 at 21:58
  • can you share more details about question, what are you trying to do? Commented Mar 1, 2020 at 9:31
  • sorry i thought it was obvious that i'm just trying in this simplified version to change the text of a label via js. I know it's something simple as i've done this before but can't get the syntax correct, it is just not working to change label text. Commented Mar 2, 2020 at 13:14

1 Answer 1

2

I don't think innerHTML() is a function. I think you have to write something like:

document.getElementById("<%=lbl.ClientID%>").innerHTML = strData;

Update:

I started a quick webforms project and this is what my script tag section looks like:

<script type="text/javascript">

    function updateText(strData) {
        debugger;
        document.getElementById("lbl").innerHTML = strData;
    }

</script>

The back-end is just like yours.

public void Test_OnClientClick(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(this, this.GetType(), "updateText", "updateText('test');", true);
}
Sign up to request clarification or add additional context in comments.

1 Comment

If you are trying to change the text of the label specifically, you probably need to use "document.getElementById().innerText =". I've just used both innerHTML and innerText and they both work for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.