简体   繁体   中英

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

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.

您只需从cs文件中访问标签文本,如下所示。

string myLabelText = this.lbltext.Text;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM