Home  >  Article  >  Backend Development  >  Example analysis of how ASP.NET implements progress bar effect

Example analysis of how ASP.NET implements progress bar effect

黄舟
黄舟Original
2017-06-04 09:45:371443browse

This article mainly introduces in detail ASP.NET to implement a simple progress bar effect, which has certain reference value. Interested readers Friends, you can refer to it

Let’s take a look at the effect of the progress bar first

After I click the button, it will display the progress page. After the progress is completed, the progress bar disappears, which is actually relatively simple.

We need a progress bar code file ProgressBar.htm (note: there are no head tags)


Then we need a progress bar class ProgressBar.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace ZhuoYueE.Dop.Web.UI
{
  /// 
  ///显示进度条
  /// 
  public class ProgressBar : System.Web.UI.Page
  {
    /// 
    /// 最大值
    /// 
    private int MaxValue
    {
      get
      {
        if (ViewState["MaxValue"] == null)
        {
          return 0;
        }
        else
        {
          return Convert.ToInt32(ViewState["MaxValue"]);
        }
      }
      set
      {
        ViewState["MaxValue"] = value;
      }
    }
    /// 
    /// 当前值
    /// 
    private int ThisValue
    {
      get
      {
        if (ViewState["ThisValue"] == null)
        {
          return 0;
        }
        else
        {
          return Convert.ToInt32(ViewState["ThisValue"]);
        }
      }
      set
      {
        ViewState["ThisValue"] = value;
      }
    }
    /// 
    /// 当前页面
    /// 
    System.Web.UI.Page m_page;
    /// 
    /// 功能描述:构造函数
    /// 作  者:huangzh
    /// 创建日期:2016-05-06 11:54:34
    /// 任务编号:
    /// 
    /// 当前页面
    public ProgressBar(System.Web.UI.Page page)
    {
      m_page = page;
    }

    public void SetMaxValue(int intMaxValue)
    {
      MaxValue = intMaxValue;
    }

    /// 
    /// 功能描述:初始化进度条
    /// 作  者:huangzh
    /// 创建日期:2016-05-06 11:55:26
    /// 任务编号:
    /// 
    public void InitProgress()
    {
      //根据ProgressBar.htm显示进度条界面
      string templateFileName = AppDomain.CurrentDomain.BaseDirectory + "ProgressBar.htm";
      StreamReader reader = new StreamReader(@templateFileName, System.Text.Encoding.GetEncoding("GB2312"));
      string strhtml = reader.ReadToEnd();
      reader.Close();
      m_page.Response.Write(strhtml);
      m_page.Response.Flush();
    }

    /// 
    /// 功能描述:设置标题
    /// 作  者:huangzh
    /// 创建日期:2016-05-06 11:55:36
    /// 任务编号:
    /// 
    /// strTitle
    public void SetTitle(string strTitle)
    {
      string strjsBlock = "";

      m_page.Response.Write(strjsBlock);
      m_page.Response.Flush();
    }

    /// 
    /// 功能描述:设置进度
    /// 作  者:huangzh
    /// 创建日期:2016-05-06 11:55:45
    /// 任务编号:
    /// 
    /// percent
    public void AddProgress(int intpercent)
    {
      ThisValue = ThisValue + intpercent;
      double dblstep = ((double)ThisValue / (double)MaxValue) * 100;

      string strjsBlock = "";

      m_page.Response.Write(strjsBlock);
      m_page.Response.Flush();
    }


    public void DisponseProgress()
    {
      string strjsBlock = "";
      m_page.Response.Write(strjsBlock);
      m_page.Response.Flush();
    }
  }
}

Then it’s time to call the method. The call is very simple. Add code to the button event or somewhere else on the page. How about using

protected void btnImport_Click(object sender, EventArgs e)
    {
      ProgressBar pb = new ProgressBar(this);
      pb.SetMaxValue(110);
      pb.InitProgress();
      pb.SetTitle("这是一个测试数据");
      for (int i = 1; i <= 110; i++)
      {
        pb.AddProgress(1);
        //此处用线程休眠代替实际的操作,如加载数据等
        System.Threading.Thread.Sleep(50);
      }
      pb.DisponseProgress();
    }

in the button event? Isn't it very simple?

The above is the detailed content of Example analysis of how ASP.NET implements progress bar effect. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn