简体   繁体   中英

Call JS function from Codebehind

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?

If document.getElementById("Text1") is textarea tag, use

document.getElementById("Text1").innerText = "value change";

Could you provie HTML for clarification?

Why not accessing the HTML Textbox via codebehind?

Use this to insert a value to a textbox:

Request.Form["yourHTMLTBName"] = "new Value inserted";

Decleration in HTML:

<input id="tbHTML" type="text" readonly="readonly" name="yourHTMLTBName"/>

Try to make it static: ClientIDMode="Static"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM