简体   繁体   中英

updating labels asp.net c#

I have a <asp:Label ID="ceckSlide"/> in my aspx page and would like to update this label constantly form a class in my model so that the label keeps changing based on a for loop. I would like to know if this is possible, if so how would I go about doing this,

As can be seen from the pseudo-code, I am calling the add services method in the aspx page, and would like to update the table in accordance with the for loop in createServices class.

class CreateServices

private int number = 0
priavte int numbercount = 0;

public void addservices()
{
   numbercount = services.count;

   for(int i = 0;i<service.count;i++)
   {
      ///add services 
      number = i+1;
   }
}

public int GetNumber()
{
    int number= number;
    return slide;
}
public int GetSlideCount()
{

    return numberCount;
}

defefaul.aspx

protected void Confirm_Click(object sender, EventArgs e)
{
   var ser =  new CreateServices();
   var addSer = ser.addservices()
}

// label get updated in this method in the aspx file 
protected void GetSlideNumber()
{

    var ser =  new CreateServices();
    int number= ser .GetSlideNumber();
    int numberCount = ser.GetSlideCount() + 1;

    ceckSlide.Text = slide.ToString() +" Of "+slideCount;
 }

The easiest way for you is to put your label in an UpdatePanel (AJAX) and place a client-side timer (javascript) in your page that sends an update request every few seconds (or whatever you wish). On the server-side, simply update your Model asynchronously and the function that updates the label value should read from the Model directly. Hope that helps.

You might want to take a look at the PokeIn Library - ASP.NET WebSocket & Comet Ajax Library (Reverse Ajax - Server Push) .

From the documentation webpage :

... PokeIn generates dynamic JavaScript codes from a .NET class to let you call them like you are calling a method from same side.

I think the issue is that you don't quite understand how the HTTP protocol works. HTTP uses a Request/Response model, where the client (typically a web browser) makes a request and then the server responds.

The typical architecture used when implementing the auto-updating feature you are describing is exactly what @dotNET has described - the client-side page has some bit of javascript which automatically fires off a request at set intervals. The server responds with the updated state. You can make this interval as small as you'd like, though shorter intervals will sometimes cause problems for users with either slow machines or slow internet connections.

While a simple web application can be developed without much knowledge of the Request/Response cycle, more advanced development (like what you are describing) practically requires that you have a thorough understanding of the web's architecture. If you don't understand the HTTP protocol, I recommend that you take some time to learn it - the small investment will pay huge dividends in how effective and effecient your code will be. REST is a good place to start because its recommendations adhere most closely to the HTTP specification.

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