[翻]ASP.NET自定义Loading控件

Customizable 'Loading' Control for Web Applications with Designer Support

英文源文档 <http://www.codeproject.com/KB/webforms/LoadingWebCustomControl.aspx>

翻译: 白水 <敏捷学院>

 

代码下载: LoadingControlEx.zip

 

介绍

使用Page_load页面加载时显示进度,是一个不错的做法。这使用的用户在与网页交互时的感觉很好。传统做法是使用每一次一行行的写不少JavaScript代码。对于JavaScript的初学者来说也是一个不小的难题。此外,写传统的JavaScript难免会有语法错误问题。因此,将所有功能封装成自定义的控件,这将为我们节省不少时间。

这是一个完全可定制的控件,只需要从工具栏多拽即可使用。它把包含设计时的支持,工具箱和属性支持。还可以根据具体情况扩展和限制功能。

代码使用

使用方法就是从工具箱上多拽即可。可以在设计时或运行时改变它的属性设置。默认情况下不需要写任何代码。设计时用使用标签表示,这样使得WebForms设计人员可以看得见。

 // Just drag and drop
 <CustomControl:Loading ID="Loading1" runat="server" />



 

事件处理:

// Custom Controls Constructor
// Binding of event handlers for processing
this.Init += new EventHandler(Loading_Init);
this.Load += new EventHandler(Loading_Load);

当一个页面的生命周期开始时,“Loading" 控件在Init事件中处理,并显示给用户。

// JavaScript injected to use in order to show/hide the control
private string _UtilityScripts = "<SCRIPT LANGUAGE="'JavaScript'">
if(document.getElementById){var isLoaded = true;}
function ShowObject(obj){if (isLoaded){obj.style.display = 'block';}}
function HideObject(obj){if (isLoaded){obj.style.display = 'none';}}
</SCRIPT>";

private void Loading_Init(object sender, EventArgs e)

    //....
    WebControl tempWC = sender as WebControl; 
    
    //Loading control is displayed to the user while page loads
    tempWC.Page.Response.Write(_UtilityScripts + loadingDiv);
    tempWC.Page.Response.Flush();
     
    //....
}

字体和其他的属性可以在后台的代码设置。当页面加载完成后,控件在Load事件将隐藏的JavaScirpt注入。

// JavaScript injected to hide the control
private string _HideDiv = "<SCRIPT LANGUAGE="'JavaScript'">
var divLoadObject = document.getElementById(\"{0}\");
HideObject(divLoadObject);</script>";
 
private void Loading_Load(object sender, EventArgs e)
{
     //....
     WebControl tempWC = sender as WebControl;
     tempWC.Page.Response.Write(string.Format(_HideDiv, "loadingScreen"));
     //....
}

Ajax enable 开关是对ajax的支持。因此Ajax的网站也可以使用,没有任何的麻烦。

// Example codes embedded
// For making the control AJAX enabled
 ScriptManager currPageScriptManager =
        ScriptManager.GetCurrent(Page) as ScriptManager;
 if (currPageScriptManager != null)
        _IsAsyncPostBack = currPageScriptManager.IsInAsyncPostBack;

// Controls property '_IsAsyncPostBack' is used to handle the Async scenarios

 自定义支持 提供了各种风格和特性的支持。控件使用默认或用户设置的属性加载然后在显示到页面上。有了这些就可以正常使用了。但是,为了刚好的用户友好和易于使用,我们添加了一个 designer类,包含了几个方法和属性。

1. 设计时支持: 在设计视图中的界面支持.

2. 设计时属性支持

3. 工具箱支持

 

 

控件提供的属性支持:

  • LoadingText - 获取或设置控件显示的文本
  • LoadingImagePath - 获取或设置控件上要显示的图片的路径
  • HorizontalAlignment - 获取或设置控件在页面的水平对齐方式
  • VerticalAlignment - 获取或设置控件在页面上的垂直对齐方式
  • TextVAlignment - 获取或设置文本在控件中的垂直对齐方式
  • TextLocation - 获取或设置文本相对图片的位置

 

兴趣点

可以了解控件的设计时支持和属性的开发方法.

posted @ 2011-07-22 17:17  敏捷学院  阅读(554)  评论(0编辑  收藏  举报