WebContorl示例--页面跳转Button

RedictButton 代码
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Text;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Reflection;
using System.Drawing.Design;

// 加载js资源
[assembly: WebResource( " WebUIControl.Js.RedictURL.js " " application/x-javascript " )]
namespace  WebUIControl
{
    [DefaultProperty(
" Text " )]
    [ToolboxData(
" <{0}:RedictButton runat=server></{0}:RedictButton> " )]
    
public   class  RedictButton : Button
    {
        [Bindable(
true )]
        [Category(
" Appearance " )]
        [DefaultValue(
"" )]
        [Localizable(
true )]
        //使用UrlEditor编辑属性
        [Editor("System.Web.UI.Design.UrlEditor, System.Design", typeof(UITypeEditor))]
        
public   string  RedictUrl
        {
            
get
            {
                String s 
=  (String)ViewState[ " RedictUrl " ];
                
return  ((s  ==   null ?  String.Empty : s);
            }
            
set
            {
                ViewState[ " RedictUrl " =  value;
            }
        }
        //在OnInit中,向页面中写入js资源
        
protected   override   void  OnInit(EventArgs e)
        {
            
base .OnInit(e);
            
if  ( ! Page.ClientScript.IsClientScriptIncludeRegistered(Page.GetType(),  " RedictURL " ))
            {
                
// 向页面中写入js资源
                 string  webUrl  =  Page.ClientScript.GetWebResourceUrl(GetType(),  " WebUIControl.Js.RedictURL.js " );
                Page.ClientScript.RegisterClientScriptInclude(Page.GetType(), 
" RedictURL " , webUrl);
            }

        }

        
///   <summary>
        
///  如果客户端禁用js,则会忽略客户端处理,提交服务端处理跳转。
        
///   </summary>
        
///   <param name="e"></param>
         protected   override   void  OnClick(EventArgs e)
        {
            
base .OnClick(e);
            Page.Response.Redirect(
RedictUrl);
        }

 

        /// <summary>
        
/// 客户端Attributes不应该在OnInit中设置,最好在OnLoad或OnPreRender中设置
        
/// </summary>
        
/// <param name="e"></param>

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

       //获取全路径

            string path = HttpUtility.UrlPathEncode(this.ResolveClientUrl(this.RedictUrl));

       //写入客户端事件中的js跳转处理

            this.Attributes.Add("onclick", " return PL_RedictButton_RedictURL('" + path + "')");
        }

        
///   <summary>
        
///  取得虚拟根目录全路径//废弃
        
///   </summary>
        
///   <returns></returns>
         public   static   string  GetRootURI()
        {
            
string  AppPath  =   "" ;
            HttpContext HttpCurrent 
=  HttpContext.Current;
            
if  (HttpCurrent  !=   null )
            {
                HttpRequest Request 
=  HttpCurrent.Request;

                
string  UrlAuthority  =  Request.Url.GetLeftPart(UriPartial.Authority);
                
if  (Request.ApplicationPath  ==   null   ||  Request.ApplicationPath  ==   " / " )
                    
// 直接安装在Web站点  
                    AppPath  =  UrlAuthority;
                
else
                    
// 安装在虚拟子目录下  
                    AppPath  =  UrlAuthority  +  Request.ApplicationPath;
            }
            
return  AppPath;
        }
    }
}

 

 

// 作为嵌入的资源
function  RedictURL (url)
{
    window.location.href 
=  url;
  //返回false,则不会被提交服务端处理;否则,提交回客户端处理。
     return   false ;
}

 

 生成的页面source中

 

< input  type ="submit"  name ="RedictButton1"  value ="test"  onclick =" return RedictURL('http://localhost:15332/NewFolder1/WebForm1.aspx');"  id ="RedictButton1"   />

 

 

input type="submit' 加上onclick的用处:

当点击按钮后,会先执行RedictURL()脚本里面的代码,如果return false;,则不提交;否则提交到服务端处理

 

 

你可能感兴趣的:(button)