简体   繁体   中英

ASP.NET Create textbox on fly using javascript

I want to create a textbox on the fly (in the comment section im creating). Now I would like your opinion on whats the best solution. I was thinking about using a webmethod and add a textbox control dynamically, but since this requires a call to the server I'm not sure if that's the best option. Or can i also spawn a textbox using plain old javascript and still getting it's value on postback?

Thanks again guys

Kind regards, Mark

Put the TextBox using ordinary HTML input element on page and set its visibility property to collapsed using style tag then in code behind make it visible.

HTML Tag:

<input id="myTextBox" type="text" style="visibility: collapse;" />

Javascript:

var txt = document.getElementById("myTextBox");
txt.style.visibility = "visible";

Hope this helps!

You can create a new element using plain old JavaScript:

var textbox = document.createElement("textarea");
textbox.className = "my-textarea"; //Styling with CSS 
document.getElementById("myelement").appendChild(textbox);

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