【问题标题】:How to set the value for textbox with javascript on an ASP.NET如何在 ASP.NET 上使用 javascript 设置文本框的值
【发布时间】:2016-11-09 04:16:18
【问题描述】:

我需要使用来自 javascript 输入的值设置 asp 文本框。

我在这里:

<td ><asp:TextBox ID="AddressTextBox" runat="server" Text='<%# Bind("Address") %>' ClientIDMode="Static" />
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                        ControlToValidate="AddressTextBox" ErrorMessage="*" 
                        ValidationGroup="InsertCustomer" SetFocusOnError="True"></asp:RequiredFieldValidator>
</td>

我使用来自here 的自动完成地址表单

所以我把它放到了 javascript 代码中:

var src = document.getElementById("autocomplete");
var dst = document.getElementById("AddressTextBox");

这个函数fillInAddress()函数:

function fillInAddress() {
        // Get the place details from the autocomplete object.
        var place = autocomplete.getPlace();

        for (var component in componentForm) {
          document.getElementById(component).value = '';
          document.getElementById(component).disabled = false;

        dst.value = src.value;
        }

我试图在选择地址后立即从自动完成获取地址文本框字段的完整地址。

但我收到一个错误,即 AddressTextBox“当前上下文中不存在名称'AddressTextBox'”

有什么想法吗?谢谢。

【问题讨论】:

  • 那个错误一定来自服务器端,JavaScript 不会写这样的错误。
  • 没有看到整个代码AddressTextBox 可能是另一个控件的子控件,您必须遍历每个控件控件才能根据 id 名称查找。

标签: javascript c# jquery asp.net google-maps


【解决方案1】:

AddressTextBox 是服务器控件,因此 ID 可能会更改。 试试看:

var dst = document.getElementById("<%=AddressTextBox.ClientID%>").value;

这可能会解决您的错误。

【讨论】:

  • 还是一样:编译器错误消息:CS0103:当前上下文中不存在名称“AddressTextBox”源错误:第 283 行:var src = document.getElementById("autocomplete");第 284 行:var dst = document.getElementById("").value;
  • 我正在发送一个示例解决方案,请检查它是否解决了您的问题
【解决方案2】:

只需运行这段工作代码,这可能会对您有所帮助:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script type="text/javascript">
        function setValues()
        {
            document.getElementById('dest').value=document.getElementById('src').value;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="src" runat="server"></asp:TextBox>
            <asp:TextBox ID="dest" runat="server"></asp:TextBox>
            <input type="button" value="Set Value" onclick="setValues()" />
        </div>
    </form>
</body>
</html>

现在您可以根据需要自定义解决方案。

【讨论】:

    【解决方案3】:

    看来我成功了。我刚刚添加了以下内容:

    document.getElementById('ctl00_maincontent_FormView1_AddressTextBox').value = document.getElementById('autocomplete').value;
    

    进入函数fillInAddress()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-04
      • 2011-05-31
      • 2016-10-03
      • 2012-09-16
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多