C# 如何从 javascript 更改 asp:textbox 的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17581915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:54:46  来源:igfitidea点击:

How to change the value of an asp:textbox from javascript

c#javascriptasp.net

提问by user1758415

I have an asp:textbox

我有一个 asp:textbox

<asp:textbox ID="NewName" runat="server" />

and when a user clicks the "Cancel" button, I want to erase whatever is in that text box by making the value just be "". If it were a an input field of type text, I could just do this in javascript:

当用户单击“取消”按钮时,我想通过将值设为“”来擦除该文本框中的任何内容。如果它是文本类型的输入字段,我可以在 javascript 中执行此操作:

document.getElementById('NewName').value= "";

But I can't just do this because it is an asp:textbox.

但我不能这样做,因为它是一个 asp:textbox。

采纳答案by Adrian Wragg

When your asp:textbox is rendered to the UI, then yes, it is just an input field of type text. The only part that may be an issue is the ID, but that can be set using ClientIDMode:

当您的 asp:textbox 呈现到 UI 时,是的,它只是一个文本类型的输入字段。唯一可能有问题的部分是 ID,但可以使用 ClientIDMode 进行设置:

<asp:textbox ID="NewName" runat="server" ClientIDMode="Static" />

renders to:

呈现为:

<input type="text" id="NewName" />

and thus the JavaScript you use should work perfectly.

因此,您使用的 JavaScript 应该可以完美运行。

回答by Dai

<asp:TextBox>renders to an <input type="text">which you can use as you would, the only complication is the id=""attribute is rewritten, but you can do this:

<asp:TextBox>渲染为<input type="text">您可以随意使用的 ,唯一的复杂之处是id=""属性被重写,但您可以这样做:

document.getElementById("<%= NewName.ClientID %>").value = "";

Assuming that this script is in the same page as the textbox. If this is in a referenced <script src="">then you'll need to use a global script variable of the element's ID.

假设此脚本与文本框在同一页面中。如果这是在引用中,<script src="">那么您将需要使用元素 ID 的全局脚本变量。

回答by Some_Yahoo

Try setting the textbox's ClientIDMode to Static

尝试将文本框的 ClientIDMode 设置为静态

That way the javascript should be able to find them with the same ID as in ASPX.

这样,javascript 应该能够找到与 ASPX 中具有相同 ID 的它们。

Here a whole test page

这里有一个完整的测试页面

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <script type="text/javascript">
    function ClearTB1() {
        document.getElementById("TextBox1").value="";
    }
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

    <p>
        <asp:TextBox ID="TextBox1" runat="server" Text="test" ClientIDMode="Static"></asp:TextBox><br />
        <asp:TextBox ID="TextBox2" runat="server" Text="test" ClientIDMode="Static"></asp:TextBox><br>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="ClearTB1()"
            CausesValidation="False" UseSubmitBehavior="False" /><br />
    </p>
</asp:Content>

回答by holldollar

I had the same problem. I solved it once I remembered there is a mangling going on with asp.net on the Ids.

我有同样的问题。一旦我想起在 Id 上对 asp.net 进行了修改,我就解决了它。

within my aspx, i had:

在我的 aspx 中,我有:

<input type="text" id="sessSubs"/>

further down, within a gridview (hence the use of "class" instead of "id"), i had a column which had an asp:TextBox:

再往下,在gridview中(因此使用“class”而不是“id”),我有一列有一个asp:TextBox:

<asp:TextBox runat="server" class="paid" />

once i recalled the id mangling issue, it was just a case of changing the way i referenced the textbox:

一旦我回忆起 id mangling 问题,这只是改变我引用文本框的方式的一个例子:

<script type="text/javascript">
$(document).ready(function() {
  function updateSubs()
  {
    var sub = parseFloat($('[id$=sessSubs]').val());
    $('.paid').val(sub);
  }
$(document).on('change, keyup', '[id$=sessSubs]', updateSubs);
});
</script>