I use this code for call JS function from codebehind.

This is button click event.

    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "myfunction", "testFunc();", true);

This is the code of JS.

function testFunc() {
//alert("msg");
document.getElementById("Text1").value = "value change";
//Text1 is html a text box
}

It's not change the value of the text box. But if i add alert, it is showing the alert which we can identify that the JS function is executing successfully.

What could be the problem of my code?

Ok do your problem is likely that when the textbox is rendered, the ID that is being generated is not "text1". You can easily verify this by looking at the client side source code using your browser.

What you can do is set "ClientIDMode=Static" to the asp.net textbox in your code behind page. This will force asp.net not to generate another id for that element. Then your js will work.

Thanks for the reply both geniusvishal and JorgeM.
Actually I couldn't say you that the text box is html text box. hense the ClientIDMode is not a attribute of the text box.

Maybe I confused you about the code behind comment. I meant aspx page, not sure why i said code behind. here is an example.

<asp:TextBox ID="TextBox1" runat="server" ClientIDMode="Static"></asp:TextBox>

The asp.net textbox control renders as an input element of type "text".

The script in Head tag.

<script>
        function Script_CalledFrom_CodeBehind(servertime) {
            alert('Current Server Time: ' + servertime);
        }
    </script>

Code behind

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), 
                "Server Time", "javascript:Script_CalledFrom_CodeBehind('" + 
                DateTime.Now.TimeOfDay + "');", true);

Write the JavaScript in the Head section, else the browser won't recognize the script and will not be called in page load or some other event.

Arun

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.