Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access asp:lable value which set using jquery?

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..

like image 701
Yashwant Kumar Sahu Avatar asked Nov 05 '22 02:11

Yashwant Kumar Sahu


2 Answers

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.

like image 101
Prakash Patani Avatar answered Nov 09 '22 04:11

Prakash Patani


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.

like image 27
Rony SP Avatar answered Nov 09 '22 03:11

Rony SP