简体   繁体   English

如何使用带有asp.net的javascript将字符串值获取到文本框

[英]How to get the String Value to the textbox using javascript with asp.net

I am creating a folder by generating a random string and I need to get that string value to the textbox using javascript. 我通过生成随机字符串来创建文件夹,我需要使用javascript将字符串值获取到文本框中。

Here is my javascript Code: 这是我的JavaScript代码:

<script type="text/javascript">
 var tempDir = randomString(8);
                document.getElementById("currentDirectory").value = tempDir;
                alert(tempDir);
</script>

This is the textbox where I need to display 这是我需要显示的文本框

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

That's It, 而已,

document.getElementById('<%= TextBox1.ClientID  %>').value = tempdir;

In asp.net the id of elements changes when rendered in browser so you need to take the.Id from control's ClientID property. 在asp.net中,元素的ID在浏览器中呈现时会更改,因此您需要从控件的ClientID属性中获取.Id。

我通过简单地在body标签内以这种方式声明控件来解决它。

('<%= TextBox1.ClientID %>')

You can also try JQuery 您也可以尝试JQuery

 $("#ControlID").val(tempDir);

You'll want to check how the control is rendered in the HTML though since it's an ASP.Net control. 由于这是一个ASP.Net控件,因此您将需要检查控件在HTML中的呈现方式。 Sometimes the ID changes a bit and you'll want the command to use that in place of ControlID above. 有时,ID会发生一些变化,您会希望命令使用它代替上面的ControlID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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