locked
Load Label.Text in AJAX .? RRS feed

  • Question

  • User739446304 posted

    Hi ;

    I am trying to design an asp.net webpage . it has two <asp:label>s which their text will load from a remote webpage by a behind code function. it takes time . I need to design my page that load first then after page load these two labels get their text . 

    How can I succeed it ?

    protected void Page_Load(object sender, EventArgs e)
            {
                    kohandr.Classes.findNumbers fn = new Classes.findNumbers();
               
                    fn._url = "some website url";
                    memberNumber.Text = fn.memNumbers();// this line takes time 
               
            }

    Saturday, August 23, 2014 5:35 PM

Answers

  • User281315223 posted

    Obviously if you need to set the content for your Labels within your code-behind, you could simply reference them via their assigned IDs as you might expect :

    protected void Page_Load(object sender, EventArgs e)
    {
           LabelA.Text = GetYourTextHere();
           LabelB.Text = GetYourOtherTextHere();
    }

    Additionally, you could also make a Web Request through your code-behind as well to target a remote address and attempt to retrieve content from it as well. However, if you want to handle this through AJAX, you'll want to create a script that will trigger when your page is loaded and then make two separate AJAX calls to populate your Labels :

    <!-- Example jQuery Reference -->
    <script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
    <!-- Your Loading Code -->
    <script type='text/javascript'>
        $(function(){
              // When your page is loaded, populate your values for each of your labels
              $.ajax({ url: "URL", context: document.body}).done(function(data) { $('#<%= LabelA.ClientID %>').html(data); });
              $.ajax({ url: "URL", context: document.body}).done(function(data) { $('#<%= LabelB.ClientID %>').html(data); });
        });
    </script>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 9, 2014 9:58 AM

All replies

  • User-1806150748 posted

    You can try the following.

    1.Do not do this in page_load.

    2. User ajax query on page load, to hit remote URL and get value from that and display in label.

    3. Optional -- you can setTimeout for ajax query ( in document.ready() ).

    All these will load the page 1st, then ajax will get value in async manner.

    Saturday, August 23, 2014 11:49 PM
  • User-1179126167 posted

    Hi,

    Don't do anything in Page_Load event.

    Create a javascript function which uses $.ajax which hits the remote url and gets the result and display the result in label.

    Call this function in $(document).ready(function() {});

    Tuesday, September 9, 2014 9:42 AM
  • User281315223 posted

    Obviously if you need to set the content for your Labels within your code-behind, you could simply reference them via their assigned IDs as you might expect :

    protected void Page_Load(object sender, EventArgs e)
    {
           LabelA.Text = GetYourTextHere();
           LabelB.Text = GetYourOtherTextHere();
    }

    Additionally, you could also make a Web Request through your code-behind as well to target a remote address and attempt to retrieve content from it as well. However, if you want to handle this through AJAX, you'll want to create a script that will trigger when your page is loaded and then make two separate AJAX calls to populate your Labels :

    <!-- Example jQuery Reference -->
    <script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
    <!-- Your Loading Code -->
    <script type='text/javascript'>
        $(function(){
              // When your page is loaded, populate your values for each of your labels
              $.ajax({ url: "URL", context: document.body}).done(function(data) { $('#<%= LabelA.ClientID %>').html(data); });
              $.ajax({ url: "URL", context: document.body}).done(function(data) { $('#<%= LabelB.ClientID %>').html(data); });
        });
    </script>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 9, 2014 9:58 AM