I am making web application in asp.net, I have one label control in my .aspx page. I have to set label text value using jquery. want to access this value in my .cs file.
<asp:Label ID="lbltext" runat="server" Text=""></asp:Label>
By using this am able to change label text :
$('#<%= lbltext.ClientID %>').text("Test");
I want to access label text value in code behind page
Thanks in advance..
You can access label value using any event like button client click
here I have given cssclass name for label.
    <asp:Label ID="lbltext" runat="server" CssClass="cssTextLabel" Text="Test">
    </asp:Label>
    <asp:Button ID="btnGetLabelData" Text="Get Data" runat="server" OnClientClick="GetData()" />
define javascript function like below.
    <script type="text/javascript">
    function GetData() {            
        var lbltxt = $.find('span.cssTextLabel')[0].innerHTML            
        __doPostBack('GET_DATA', lbltxt);
    }
    </script>
handle postback in page load of page as below.
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Handles Me.Load
    Dim strLblData As String = String.Empty
    If Request("__EVENTTARGET") = "GET_DATA" Then
        strLblData = Request("__EVENTARGUMENT").ToString()
        Response.Write(strLblData)
    End If
    End Sub
Hope this will help you.
Hi Yashwant Using HiddenField Control You can solve this issue. use Following Code for that
.aspx File
 <asp:HiddenField ID="HiddenField1" runat="server" />
        <asp:Label ID="lbltext" runat="server" Text=""></asp:Label>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
By using this am able to HiddenField Value :
<script type="text/javascript">
        $(document).ready(function () {
            $("#HiddenField1").val('Hello');            
        });
    </script>
In .CS File
protected void Button1_Click(object sender, EventArgs e)
    {
        lbltext.Text = HiddenField1.Value;
        Page.RegisterStartupScript(new Guid().ToString(), "<script type='text/javascript'>alert('"+lbltext.Text+"');</script>"); // alert the label value
    }
I am sure that is useful for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With