Javascript to Get Set or Read Write TextBox value in ASP.Net C# VB.Net

In day to day development we need to get & set TextBox value using Javascript more often. Here in this article i will show you how one can get set or read write Asp.Net C# or VB.Net Server Control TextBox Value or TextBox Text using Javascript. To make you understand the whole thing here I will create a simple summation calculator using Javascript. Where user can put 2 Integer numbers in 2 TextBoxes, the Javascript simple calculator example function will read & assign TextBox Server controls values into 2 Javascript variables. After summing the values the Javascript function will display the result into 3rd Textbox.

Related Article:
How to get set read TextBox value using JQuery C# VB.Net in ASP.Net

Add 3 Serverside TextBoxes and 1 Command Button to do the sum:

    <asp:TextBox runat="server" ID="txtFirst"></asp:TextBox>
    <asp:TextBox runat="server" ID="txtSecond"></asp:TextBox>
    <asp:Button runat="server" ID="btn" Text="+" OnClientClick="return do_ADD();" />
    <asp:TextBox runat="server" ID="txtSum"></asp:TextBox>

Add the below Javascript to read 2 number from txtFirst & txtSecond TextBoxes and set the result into third TextBox:

<script type="text/javascript">
    function do_ADD() {
        // Get or Read TextBox Text or Value Using Javascript document.getElementById
        var First_Num = document.getElementById('<%=txtFirst.ClientID%>').value;
        var Second_Num = document.getElementById('<%=txtSecond.ClientID%>').value;

        // Set or write TextBox Text or Value Using Javascript document.getElementById
        document.getElementById('<%=txtSum.ClientID%>').value = eval(First_Num) + eval(Second_Num);
        return false;

Now run the page. Hope you will get below output:
Javascript to get set read TextBox value in ASP.Net Csharp VB.Net

Hope now you can read or write value to a TextBox using Javascript easily. This example will work in simple web pages as well as web pages under Master pages. If you can’t understand yet please download the working example from below:

Download Code Example C#        Download Code Example VB.Net

Posted in .Net,, C#, Client, Javascript, VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *