connectseekers.com

Wednesday, 25 May 2016

Get Control Value By Id in Asp.Net Using JavaScript

5/25/2016 - By Pranav Singh 0

This article will show you how you can get the textbox control value on click on button in asp.net using JavaScript. In this I have displayed the control value in alert message box.

So for this article first we will create a new asp.net application and add a textbox and a button control in your form. 

<body>
    <form id="form1" runat="server">
        <div>          
            Enter Text :
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
             <asp:Button ID="Button1" runat="server" Text="Click To Get Value"/>
        </div>
    </form>
</body>
</html>

Now create javascript function to get the value of textbox control.

  function GetValue() {
            var controlValue = document.getElementById("<%=TextBox1.ClientID%>").value;
            alert(controlValue);
        }

Now check the complete code of the page.

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

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Get Any Control Value By Id in Asp.Net Using JavaScript</title>
    <script>
        function GetValue() {
            var controlValue = document.getElementById("<%=TextBox1.ClientID%>").value;
            alert(controlValue);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>          
            Enter Text :
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
             <asp:Button ID="Button1" runat="server" Text="Click To Get Value" OnClientClick="javascript:GetValue();"/>
        </div>

    </form>
</body>
</html>

In above code I have used ClientID to get the control id. As we know when we use master page custom control in your asp.net application on that case your control id get change. So by using ClientID you don’t need to worry about the control id, it will take care.

Now we have done. Run the application to check the output.
 

  
DOWNLOAD 

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top