.CS中的JavaScript

来源:互联网 发布:武汉黑马程序员 编辑:程序博客网 时间:2024/05/19 19:30
using System;
using System.Web;
using System.Web.UI;
using System.Text;
using System.IO;
using System.Net;
using System.Collections.Generic;
using System.Configuration;
using System.Xml;

/// <summary>
/// Cs里JAVASCRIPT控制
/// </summary>
/// Author: 姜辉
/// History: 
///      2007-02-08   姜辉 [创建]


namespace UpFile
{
    
public class Utility : Page
    
{
        
/// <summary>
        
/// 弹出提示
        
/// </summary>
        
/// <param name="message"></param>

        protected void MsgBox(string message)
        
{
            
// Get a ClientScriptManager reference from the Page class.
            ClientScriptManager cs = this.Page.ClientScript;

            
// Define the name and type of the client scripts on the page.
            String csname = "PopupScript";
            Type cstype 
= this.GetType();

            
// Check to see if the startup script is already registered.
            if (!cs.IsStartupScriptRegistered(cstype, csname))
            
{
                String cstext 
= "alert('" + Server.HtmlEncode(message) + "');";
                cs.RegisterStartupScript(cstype, csname, cstext, 
true);
            }

        }


        
/// <summary>
        
/// 弹出提示并转向url
        
/// </summary>
        
/// <param name="message"></param>
        
/// <param name="url"></param>

        protected void MsgBox(string message, string url)
        
{
            
// Get a ClientScriptManager reference from the Page class.
            ClientScriptManager cs = this.Page.ClientScript;

            
// Define the name and type of the client scripts on the page.
            String csname = "PopupScript";
            Type cstype 
= this.GetType();

            
// Check to see if the startup script is already registered.
            if (!cs.IsStartupScriptRegistered(cstype, csname1))
            
{
                String cstext 
= "alert('" + Server.HtmlEncode(message) + "');window.location='" + url + "';";
                cs.RegisterStartupScript(cstype, csname, cstext, 
true);
            }

        }

        
/// <summary>
        
/// 最大化打开窗口
        
/// </summary>
        
/// <param name="pageName"></param>

        public static void COpenMax(string pageName)
        
{
            
//Get a ClientScriptManager reference from the Page class
            ClientScriptManager cs = this.Page.ClientScript;

            
//Define the name and type of the client scripts on the page
            String csname = "open";
            Type cstype 
= this.GetType();

            
//builder the script text
            StringBuilder cstext = new StringBuilder();
            cstext.Append(
"<script language='javascript'>");
            cstext.Append(
"{window.open('" + pageName + "','aa','width='+screen.width+' height='+screen.height+' top=0 left=0 toolbar=no menubar=no resizable=yes status=yes');}");
            cstext.Append(
"</script>");

            
// Check to see if the startup script is already registered.
            if (!cs.IsStartupScriptRegistered(cstype, csname))
            
{
                cs.RegisterStartupScript(cstype, csname, cstext.ToString(), 
true);
            }

        }

        
/// <summary>
        
/// 全屏窗口
        
/// </summary>
        
/// <param name="pageName"></param>

        public static void COpenFullScreen(string pageName)
        
{
            
//Get a ClientScriptManager reference from the Page class
            ClientScriptManager cs = this.Page.ClientScript;

            
//Define the name and Type of the client scripts on the page
            String csname = "open";
            Type cstype 
= this.GetType();

            
//builder the script text
            StringBuilder cstext = new StringBuilder();
            cstext.Append(
"<script language='javascript'>");
            cstext.Append(
"{window.open('" + pageName + "','aa','fullscreen');}");
            cstext.Append(
"</script>");
            
// Check to see if the startup script is already registered
            if (!cs.IsClientScriptBlockRegistered(cstype, csname))
            
{
                cs.RegisterStartupScript(cstype, csname, cstext.ToString(), 
true);
            }

        }

        
/// <summary>
        
/// 打开最大化模式窗口
        
/// </summary>
        
/// <param name="pageName"></param>

        public static void COpenModalDialog(string pageName)
        
{
            
//Get a ClientScriptManager reference from the Page class
            ClientScriptManager cs = this.Page.ClientScript;

            
//Define the name and Type of the client script on the page
            String csname = "showModalDialog";
            Type cstype 
= this.GetType();

            
//builder the script text
            StringBuilder cstext = new StringBuilder();
            cstext.Append(
"<script language='javascript'>");
            cstext.Append(
"{showModalDialog('" + pageName + "','aa','dialogWidth:'+screen.width+'; dialogHeight:'+screen.height+'; dialogTop:0; dialogLeft:0; center:yes; help:no; resizable:yes; status=yes');}");
            cstext.Append(
"</script>");

            
//Check to see if the startup script is already registered
            if (!cs.IsClientScriptBlockRegistered(cstype, csname))
            
{
                cs.RegisterStartupScript(cstype, csname, cstext.ToString(), 
true);
            }

        }

        
/// <summary>
        
/// 打开规定大小的模式窗口
        
/// </summary>
        
/// <param name="MyPage"></param>
        
/// <param name="pageName"></param>
        
/// <param name="width"></param>
        
/// <param name="height"></param>

        public static void COpenModalDialogSelf(string pageName, int width, int height)
        
{
            
//Get a ClientScriptManager reference from the Page class
            ClientScriptManager cs = this.Page.ClientScript;

            
//Define the name and Type of the client script on the page
            String csname = "showModalDialog";
            Type cstype 
= this.GetType();

            
//builder the script
            StringBuilder cstext = new StringBuilder();
            cstext.Append(
"<script language='javascript'>");
            cstext.Append(
"{showModalDialog('" + pageName + "','aa','dialogWidth:'" + width.ToString() + "'; dialogHeight:'" + height.ToString() + "'; dialogTop:0; dialogLeft:0; center:yes; help:no; resizable:yes; status=yes');}");
            cstext.Append(
"</script>");

            
//Check to see if the startup script is already registered
            if (!cs.IsClientScriptBlockRegistered(cstype, csname))
            
{
                cs.RegisterStartupScript(cstype, csname, cstext.ToString(), 
true);
            }

        }

        
/// <summary>
        
/// 关闭窗口
        
/// </summary>

        public static void CClose()
        
{
            
//Get a ClientScriptManager reference from the Page class
            ClientScriptManager cs = this.Page.ClientScript;

            
//Define the name and Type of the client script on the page
            String csname = "close";
            Type cstype 
= this.GetType();

            
// builder the script text
            StringBuilder cstext = new StringBuilder();
            cstext.Append(
"<script language='javascript'>");
            cstext.Append(
"{window.opener='anyone';window.close();}");
            cstext.Append(
"</script>");

            
//Check to see if the startup script is already registered
            if (!cs.IsClientScriptBlockRegistered(cstype, csname))
            
{
                cs.RegisterStartupScript(cstype, csname, cstext.ToString(), 
true);
            }


        }

        
/// <summary>
        
/// 下载文件
        
/// </summary>
        
/// <param name="DstrFileName"></param>
        
/// <param name="YstrUrl"></param>
        
/// <returns></returns>

        public static bool DownLoadFile(string DstrFileName, string YstrUrl)
        
{
            
//打开上次下载的文件或新建文件
            long lStartPos = 0;
            System.IO.FileStream fs;
            
if (System.IO.File.Exists(DstrFileName))
            
{
                fs 
= System.IO.File.OpenWrite(DstrFileName);
                lStartPos 
= fs.Length;
                fs.Seek(lStartPos, System.IO.SeekOrigin.Current);   
//移动文件流中的当前指针
            }

            
else
            
{
                fs 
= new System.IO.FileStream(DstrFileName, System.IO.FileMode.Create);
                lStartPos 
= 0;
            }

            
//打开网络连接
            try
            
{
                System.Net.HttpWebRequest request 
= (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(YstrUrl);
                
if (lStartPos > 0)
                    request.AddRange((
int)lStartPos);    //设置Range值
                
//向服务器请求,获得服务器回应数据流
                System.IO.Stream ns = request.GetResponse().GetResponseStream();
                
byte[] nbytes = new byte[512];
                
int nReadSize = 0;
                nReadSize 
= ns.Read(nbytes, 0512);
                
while (nReadSize > 0)
                
{
                    fs.Write(nbytes, 
0, nReadSize);
                    nReadSize 
= ns.Read(nbytes, 0512);
                }

                fs.Close();
                ns.Close();
                
//("下载完成");
                return true;
            }

            
catch
            
{
                fs.Close();
                
return false;
            }

        }

    }

}


 
原创粉丝点击