2016-07-06 7 views
0

javascript入力から値を指定してaspテキストボックスを設定する必要があります。ASP.NETでJavaScriptを使用してテキストボックスの値を設定する方法

私はここにある:

<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> 

を私は私のJavaScriptコードにこれを配置here

からオートコンプリートアドレス形式を使用:

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

、これはfillInAddress()funcion機能します:

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「名 『AddressTextBox』は現在のコンテキスト内に存在しない」というイムエラーを取得

任意のアイデア?ありがとう。

+0

このエラーはサーバー側からのものでなければなりません。そのようなエラーはJavaScriptでは書き込まれません。 –

+0

コード全体を見ることなく、 'AddressTextBox'はおそらく別のコントロールの子コントロールです。各コントロールコントロールをループしてID名に基づいて検索する必要があります。 –

答えて

1

AddressTextBoxはサーバーコントロールであるため、IDが変更される可能性があります。これはあなたのエラーを解決することができる

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

: として試してみてください。

+0

まだ同じ: コンパイラエラーメッセージ:CS0103: ライン283:名 'AddressTextBoxは、' 現在のコンテキスト ソースエラーに存在しないのvar SRC =のdocument.getElementById( "オートコンプリート");行284:var dst = document.getElementById( "<%= AddressTextBox.ClientID%>")。値; –

+0

サンプルソリューションを送信していますが、問題が解決しているかどうかを確認してください – Vipul

0

ただ、これはあなたを助けるかもしれない、このコードの作業部分を実行します。

<%@ 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> 

今すぐあなたの要件ごとにソリューションをカスタマイズすることができます。

0

私はそれを機能させるように見えます。私はちょうど追加しました

document.getElementById( 'ctl00_maincontent_FormView1_AddressTextBox')。値= document.getElementById( 'オートコンプリート')。

into関数function fillInAddress()

関連する問題