简体   繁体   中英

Set TextBox value using javascript on ButtonClick in ASP.NET

I have searched alot but i didn't find any good answer. My Question is:

I want to set the value of a asp.net textbox using external javascript file.

 function setvalue() { document.getElementById('<%=txtbox1.ClientID %>').value = "Hello World"; } 
 <asp:TextBox runat="server" ID="txtbox1" /> <asp:Button runat="server" OnClientClick="setvalue();" Text="SetValue" /> 

i have found some answer but they used internal javascript. I don't know how to do it. Please Help.

Just pass the textbox's ID as parameter, like this

function setvalue(textBoxID)
{
  document.getElementById(textBoxID).value = "Hello World";
}

and in your main file (asp/aspx) call it like this

OnClientClick="setvalue('<%=txtbox1.ClientID %>');"  

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