Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Thursday, 23 April 2015

Introduction:

In this article I will explain how to get value of Label control on server side where value of control is set by using JavaScript or Jquery.


Following Javascript method is used to set Value to Label Control and Hidden Field:

   <script type="text/javascript">
       function SetName() {
           var label = document.getElementById("<%=lblName.ClientID %>");

           //Set the value of Label.
           label.innerHTML = "Mazik Solutions";

           //Set the value of Label in Hidden Field.
           document.getElementById("<%=hfName.ClientID %>").value = label.innerHTML;
       }
</script>

Full Source Code for sample Application:
Design Section:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
   <script type="text/javascript">
       function SetName() {
           var label = document.getElementById("<%=lblName.ClientID %>");

           //Set the value of Label.
           label.innerHTML = "Mazik Solutions";

           //Set the value of Label in Hidden Field.
           document.getElementById("<%=hfName.ClientID %>").value = label.innerHTML;
       }
</script>
</head>
<body>
<form id="form1" runat="server">
  <center>
 <fieldset style="width:300px">
    <legend><strong>Set Label Control Value Using Javascript</strong></legend>
    <br />
   <asp:Label ID = "lblName" Text="Name" runat="server" />
<asp:HiddenField ID = "hfName" runat = "server" />
<br />
<br />
<asp:Button Text="Set Name" runat="server" OnClientClick = "SetName()" /></fieldset></center></form>
</body>
</html>

ASP.NET Code using C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Jquery : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            lblName.Text = Request.Form[hfName.UniqueID];
        }
    }
}


ASP.NET Code using Vb.Net:

Protected Sub Page_Load(sender As Object, e As EventArgsHandles Me.Load
    If Me.IsPostBack Then
        lblName.Text = Request.Form(hfName.UniqueID)
    End If

End Sub

0 comments:

Post a Comment