0

Wanted to display data in asp:textbox using JavaScript. When I am using HTML textbox the value is displayed on the text box. But data is not displayed when using asp:Textbox.

function SearchDealer()
{
    $.ajax(
    {
        async: false,
        type: "POST",
        url: "DealerDetails.aspx/GetDealerDetails",
        data: "{DlrId:'" + dealerID + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data)
        {  
            //This is a html text box so value is dsplayed.
            document.getElementById('txt_sapcode').value = data.d.sapcode;

            //No values displayed in this asp:textBox
            document.getElementById('tex_dealername').value = data.d.DealerName;
            document.getElementById('txt_addr1').value = data.d.Add1;
        },
        error: function ()
        {
            alert("Failed to update details.");
        }
    });
    srchflg = true;
}

5 Answers 5

2

asp:Textbox controls generate unique IDs, so this:

<asp:Textbox ID="myTextBox" runat="server" Text="Hello, World" />

Could generate something like this:

<input type="text" id="MyControl_myTextBox" value="Hello, World" />

In order to get the ID produced by ASP.NET, you can use the ClientID property inside your javascript:

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

So from your example:

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

If your SearchDealer function is in an external javascript file, you will not have access to the ClientID property, so you will need to pass it from your ASP.NET page:

<!-- This file contains your SearchDealer function -->
<script src="externalFile.js"></script>
<!-- Call SearchDealer with a parameter from your .NET page -->
<script>
    SearchDealer(document.getElementById('<%=tex_dealername.ClientID%>').value);
</script>

Your function definition would then need to be declared as:

function SearchDealer(dealerName) {
    // Do something with dealerName
}
Sign up to request clarification or add additional context in comments.

1 Comment

Small note : This will not work for Javascript that is not included inside the .ASPX page at compile time. (i.e. In an External JS File)
1

Using ASP.NET controls, gives you a unique ID generated on the serverside , your id look like this ctl00_contentPlaceHolder_tex_dealername

Client side code :

$("input[id$='tex_dealername']").val(data.d.DealerName);  //option 1
$('<%= tex_dealername.ClientID %>').val(data.d.DealerName); //option 2 

or if you assigned any unique class so call by class name

$('.yourClassName').val(data.d.DealerName);

1 Comment

@RGraham: downvote accepted, but as OP used $.ajax this clearly indicate jquery lib are there so whats harm in writing with jquery syntax
1

For Html control use : $("#TextboxId").val("Value");

For Asp.net textbox control use : $("[id$=TextboxId]").val("Value");

Comments

0

Have some ways for your case:

1.Using ClientID:

document.getElementById('<%=tex_dealername.ClientID%>')

2.Add a CssClass to your TextBox control:

<asp:Textbox ID="myTextBox" runat="server" Text="Hello, World" CssClass="TestClass" />

then use class selector.

3.Just change ClientIDMode in web.config:

<pages clientIDMode="Static" /> inside <system.web></system.web>

Comments

0

Add a CssClass property to asp:Textbox and get it using jquery ($(".class"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.