Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6747
  • Last Modified:

Update asp.net label dynamically with jquery ajax

Hello experts,

I have an asp.net page that on the Page_Load method takes some value from the database and then assigns it to an asp.net Label.

.aspx
....
<body>
...
<asp:Label ID="ResultLabel1" runat="server"></asp:Label>
...
</body>

.aspx.cs
....
protected void Page_Load(object sender, EventArgs e)
{
...
resultText = ...
ResultLabel1.Text = resultText;
...
}

This value is being dynamically changed on the background, but unfortunately the only way right now to update this value is to manually reload the whole page.

I would like to implement ajax request using jquery in order to update that value on the page dynamically (let's say once in a minute) without manual reloading the whole page.

I figured out that with jquery I can use $.get to get the same page and somehow parse the returned html. I'm new to jquery and javascript itself, so unfortunately I cannot get any further...

Any ideas?
Thanks!
0
snarinsky
Asked:
snarinsky
  • 3
1 Solution
 
Albert Van HalenAnalyst developerCommented:
To get the client element from a ASP.NET server control use can use it's ClientID property.
In order to change the text of your label you can use :

$("#<%= ResultLabel1.ClientID %>").text("New text");
0
 
Albert Van HalenAnalyst developerCommented:
NB This is jQuery so Clientside scripting
0
 
snarinskyAuthor Commented:
Ok thanks, but how can get the value of this label from the returned page?

My idea (I'm not sure if it's right) was to do something like the following:

       $.get(
            "<current page>",                 // Somehow I need to figure out how to get current url??
            function(html)
            {
                $("#<%= ResultLabel1.ClientID %>").text("<updated value from the returned page>");      //Somehow I need to parse html of the returned page and get the new value of the ClientID label to reassign it here????
            }
0
 
Albert Van HalenAnalyst developerCommented:
So, you want to process a complete page just in order to return a specific value which is displayed in a label.
Perhaps it's better to create a handler in ASP.Net (a file with the extension ashx).
This handler will function like a classic asp page, and will return just whatever you want (ie a specific value from the database), without all the overhead of a aspx page without to extract a certain value from the page which is rendered.
Keep in mind that a handler is a standalone page and will rely on the input you are sending to it in order to process things.

Usefull link http://www.aspcode.net/Creating-an-ASHX-handler-in-ASPNET.aspx, or just google on "asp.net ashx file"
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now