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.
Some of my
previous articles are as follows: Export
GridView Or Table Data Into jSon Format Data By C#.Net In Asp.Net Using jQuery, Add
JavaScript Function To Button Control in Asp.net Using C#, Call
C# Code or Function or Method From Javascript In Asp.Net, Get
All Checked Checkbox Value Using jQuery in Asp.Net CheckBoxList, Get
RadioButton Value Using jQuery In Asp.Net, Display
Selected Row Value Of GridView In JavaScript Alert Message Asp.Net Using C#.Net, Display
Calendar Control Selected Date Into Javascript Alert Message In Asp.Net, Asp.net,
C#.net and jQuery articles, Confirm
Message Box With Ok and Cancel Detect On Button Click Using JavaScript In HTML
and Asp.net, Alert
Message On Button Click Using JavaScript In HTML.
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
0 comments:
Please let me know your view