Asp.net自定義控件之加載層
本文旨在給大家開發(fā)自定義控件(結合js)一個思路,一個簡單的示例,可能在實際項目中并不會這樣做。
先來看看效果:
1.在靜態(tài)頁面里開發(fā)好想要的效果
jQuery.extend({ openloading: function (options) { var defaults = { msg: '數據提交中...', img: 'loading.gif' }; var opts = $.extend(defaults, options); $("body").append("<div class='l_overlay' style='position:fixed;top:0;right:0;bottom:0;left:0;z-index:998;width:100%;height:100%;padding:0 20px 0 0;background-color:gray;display:none;'></div><div class='l_showbox' style='position:fixed;top:0;left:50%;z-index:1001;opacity:0;filter:alpha(opacity=0);margin-left:-80px;border:1px solid gray;font-size:12px;font-weight:bold;'><div class='loadingWord' style='width:122px;height:38px;line-height:38px;border:2px solid #D6E7F2;background:#fff;'><img style='margin:10px 8px 10px 8px;float:left;display:inline;' src='"+opts.img+"'>數據提交中...</div></div>"); var h = $(document).height(); $(".l_overlay").css({ "height": h, 'display': 'block', 'opacity': '0.4' }); $(".l_showbox").stop(true).animate({ 'margin-top': (h / 2 - 58) + 'px', 'opacity': '1' }, 200); }, closeloading: function () { $(".l_showbox").stop(true).animate({ 'margin-top': '250px', 'opacity': '0' }, 400); $(".l_overlay").css({ 'display': 'none', 'opacity': '0' }); $(".l_overlay").remove(); $(".l_showbox").remove(); } });
2.vs新建類庫,新建類繼承于WebControl
添加屬性:
[Description("獲取和設置觸發(fā)器ID"), DefaultValue(""), Browsable(true), Category("雜項")]
public string TargetID { get; set; }
重寫OnPreRender方法。方法中注冊js腳本,該腳本指示ID為TargetID的控件點擊時顯示加載層
protected override void OnPreRender(EventArgs e) { if (Page != null && !string.IsNullOrEmpty(TargetID)) { TargetID = GetClientID(TargetID); Page.ClientScript.RegisterClientScriptResource(typeof(Loading), "BoControl.Scripts.Jquery.js"); this.Page.ClientScript.RegisterStartupScript(typeof(string), "BoControl_" + this.ClientID, "$(\"#" + TargetID + "\").on(\"click\",function(){$.openloading({msg:\"" + Text + "\", img: \"" +Page.ClientScript.GetWebResourceUrl(this.GetType(), "BoControl.Images.loading.gif")+ "\"});});", true); } base.OnPreRender(e); }
OnPreRender方法中
Page.ClientScript.RegisterClientScriptResource(typeof(Loading), "BoControl.Scripts.Jquery.js");注冊JQuery
Page.ClientScript.GetWebResourceUrl(this.GetType(), "BoControl.Images.loading.gif");是獲取Web資源文件路徑,如果你不想把圖片文件嵌入dll請改為真實路徑(如:Images/Loading.gif),相反你需要像下面一樣指明圖片文件和JQuery文件,并且圖片屬性-生成操作為:嵌入的資源
[assembly: WebResource("BoControl.Images.loading.gif", "image/gif")]//這里你還需注冊JQuery
namespace BoControl
{
你還需要寫Open方法和Close方法,方便后臺代碼中調用。
如:
/// <summary> /// 打開加載動畫 /// UpdatePanel注冊 /// </summary> /// <param name="panel">UpdatePanel對象</param> public void Open(UpdatePanel panel) { if (Page != null) { ScriptManager.RegisterStartupScript(panel, panel.GetType(), "openloading", "$.openloading({msg:\"" + Text + "\", img: \"" + Page.ClientScript.GetWebResourceUrl(this.GetType(), "BoControl.Images.loading.gif"); + "\"});", true); } }
總的來說自定義控件的開發(fā)不算復雜,以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
ASP.NET Core 2.0中Razor頁面禁用防偽令牌驗證
在這篇短文中,我將向您介紹如何ASP.NET Core2.0 Razor頁面中禁用防偽令牌驗證,對此有興趣的朋友參考學習下吧。2018-01-01ASP.NET Core中修改配置文件后自動加載新配置的方法詳解
這篇文章主要給大家介紹了關于ASP.NET Core中修改配置文件后自動加載新配置的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用ASP.NET Core具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2020-08-08asp.net System.Guid ToString五種格式
這篇文章主要介紹了asp.net System.Guid ToString五種格式,需要的朋友可以參考下2017-02-02.Net中如何操作IIS的虛擬目錄原理分析及實現(xiàn)方案
編程控制IIS實際上很簡單,和ASP一樣,.Net中需要使用ADSI來操作IIS,但是此時我們不再需要GetObject這個東東了,因為Net為我們提供了更加強大功能的新東東2012-12-12