locked
find element with session get RRS feed

  • Question

  • User932259438 posted
      var get = ($.session.get('some key'));
    
    

    Result from session: <span id="ContentPlaceHolder1_Label9">test1234</span

    How can I used now:  $(#ContentPlaceHolder1_Label9)  and show in alert text test1234?

    I set source code in session and then i want from session get test1234

    Please help

      var get = ($.session.get('some key'));

        alert("#"+get).html();

    Wednesday, September 7, 2016 11:17 AM

Answers

  • User475983607 posted

    Use the client ID property of the server control when using a Master Page.

    var labelText = $('#<%=Label9.ClientID %>').text();

    Below is a basic example.  The master page has the JQuery reference.

    <head runat="server">
        <script src="//code.jquery.com/jquery-1.12.4.min.js"></script>
        <title></title>
        <asp:ContentPlaceHolder ID="head" runat="server">
        </asp:ContentPlaceHolder>
    </head>

    The content page.  I used the HTML 5 session storage API rather than a the JQuery wrapper library for session storage.

    <%@ Page Title="" Language="C#" MasterPageFile="~/master/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebAppDemo.master.WebForm1" %>
    <%@ MasterType virtualpath="~/Master/Site1.master" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <script type="text/javascript">
        $(function () {
            //Get the label text
            var labelText = $('#<%=Label9.ClientID %>').text();
    
            //Set session
            sessionStorage.setItem('Label9', labelText);
    
            //Get Session
            var data = sessionStorage.getItem('Label9');
    //display Session alert(data); //console.log(data); }); </script> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> <asp:Label ID="Label9" runat="server" Text="Label">test123</asp:Label> </asp:Content>

    Or maybe you are using the label text as a key?

    <script type="text/javascript">
        $(function () {
            //Get the label text
            var key = $('#<%=Label9.ClientID %>').text();
    
            //Set session
            sessionStorage.setItem(key, 'Foo bar');
    
            //Get Session
            var data = sessionStorage.getItem(key);
    
            //alert(data);
            console.log(data);
        });
    
    </script>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 7, 2016 11:36 AM