user6558380
user6558380

Reputation:

How to set the value for textbox with javascript on an ASP.NET

I need to set up asp textbox with a value from javascript input.

i have here:

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

I use autocomplete address form from here

so i placed this to the javascript code:

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

and this to function 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;
        }

Im trying to get the full address from the autocomplete to the AddressTextBox field as soon as the address is selected.

But im getting an error that AddressTextBox "The name 'AddressTextBox' does not exist in the current context"

Any ideas? thanks.

Upvotes: 0

Views: 14312

Answers (3)

user6558380
user6558380

Reputation:

It looks that I made it work. I just added the following:

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

into function fillInAddress()

Upvotes: 0

Vipul
Vipul

Reputation: 356

Just run this working piece of code, this might help you:

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

Now you can customise the solution as per your requirement.

Upvotes: 0

Vipul
Vipul

Reputation: 356

AddressTextBox is server control so the ID might be changed. Try as:

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

This may resolve your error.

Upvotes: 1

Related Questions