5

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

1

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

-2

Simply you can access your label text from your cs file as follows.

string myLabelText = this.lbltext.Text;

1 Comment

this.lbltext.Text is not sent in the Http Query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.