Get and Set Label Value on Button Click Event using JQuery in ASP.Net

Hi, In Previous post we understood concept of JQuery and How to use JQuery in ASP.Net.

In this ASP.Net Jquery Post we will learn to Set text value to Label control and get value from Label control in ASP.Net.

First, Download JQuery library from http://jquery.com/. and include in web page at head section.

Design the ASP.Net web form with one Button and one Label control.

First, we will learn to get the Label control value when we click the button control.

Here, we assign label text = Meera Academy. when we click the button then label value shows in alert message box.

code for design asp.net web form:

 <asp:Label ID=”Label1″ runat=”server” Text=”Meera Academy”></asp:Label>
<asp:Button ID=”Button1″ runat=”server” Text=”Get Label Value” />

 

Get Label value while button click in asp.net using JQuery
Get Label value while button click in asp.net using JQuery

write the below JQuery code for get the label value on button click event.

<head runat=”server”>
<title>JQuery in ASP.Net</title>
<script src=”jquery-1.4.3.min.js” type=”text/javascript”></script>
<script>
$(document).ready(function() {
$(“#Button1”).click(function() {
  alert($(‘#<%=Label1.ClientID%>’).text());    
});
});
</script>
</head>

The out put of above Jquery Example is :

Get Label value while button click in asp.net using JQuery
Get Label value while button click in asp.net using JQuery

Now, we will learn to set value to label control using JQuery. First clear the text value of label control.

we assign value “Meera Academy” to label control while clicking the Button control in asp.net.

code for design asp.net web form:

 <asp:Label ID=”Label1″ runat=”server” Text=””></asp:Label>
<asp:Button ID=”Button1″ runat=”server” Text=”Set Label Value” />

 

Set Label value while button click in asp.net using JQuery
Set Label value while button click in asp.net using JQuery

In above figure we already removed the label text value.

write below JQuery code for assign value to Label control.

<head runat=”server”>
<title>JQuery in ASP.Net</title>
<script src=”jquery-1.4.3.min.js” type=”text/javascript”></script>
<script>
$(document).ready(function() {
$(“#Button1”).click(function() {
$(‘#<%=Label1.ClientID%>’).text(“Meera Academy”); 
});
});
</script>
</head>

The Result of above JQuery Example is :

Set Label value while button click in asp.net using JQuery
Set Label value while button click in asp.net using JQuery

I hope this JQuery Exmaple Set and Get Value of Label control will help you….

http://jquery.com/.#sthash.EVaPn3WB.dpuf
http://jquery.com/.#sthash.EVaPn3WB.dpuf
http://jquery.com/.#sthash.EVaPn3WB.dpuf

Leave a Reply

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