0

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?

4
Contributors
5
Replies
19
Views
3 Years
Discussion Span
Last Post by arun1123
0

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.

0

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.

0

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".

Edited by JorgeM

0

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

Edited by arun1123

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.