Javascript在Asp.Net中的应用汇总
protected void NextPageParameters (
string strPageTo,string [] strKeys,object [] obValues,boo l bNewWindow,int nWidth,int nHeight )
{
if( strPageTo == null || strPageTo.Length == 0)
throw new ArgumentNullException ("PageBase "," 必须提供将要跳转到的页面的名称! ") ;
if( (strKeys == null && obValues != null) || (obValues == null && strKeys != null)
||( strKeys != null && obValues != null && strKeys.Length !=obValues.Length ))
throw new ArgumentNullException ("PageBase "," 参数名和参数值个数不一致或者为空! ") ;
string[ ] strPageToArray = strPageTo.Split ('?') ;
string url = strPageToArray [0] ;
// 得到原先存在的参数
string strOldParas = strPageToArray.Length > 1 ? strPageToArray [ 1] : string.Empty ;
short shtParamCount = 0 ;
//bool bAsk = false ;
System.Text.StringBuilder strBuilder = new System.Text.StringBuilder ( url ) ;
if( strKeys != null && obValues != null && obValues.Length > 0 )
{
strBuilder.Append ( '?' ) ;
// bAsk = true ;
for( int i = 0 ; i < strKeys.Length ; i ++ )
{
if( strKeys [i ] != null)
{
if( shtParamCount ++ > 0 )
strBuilder.Append ( "&" ) ;//&//&
strBuilder.Append ( strKeys [i ] ) ;
strBuilder.Append ( '=' ) ;
strBuilder.Append ( obValues [i ] == null ? "" : obValues [i ].ToString () ) ;
}
}
//shtParamCount = 0 ;
}
if( strOldParas != null && !strOldParas.Equals (String.Empty ) )
{
//if( !bAsk )
if( shtParamCount == 0 )strBuilder.Append ( '?' ) ;
string[ ,] strParametrs = Util.Str.GetCodeNameArray (strOldParas ,'&','=') ;
for( int i = 0 ; i < strParametrs.Length/2 ; i ++)
{
if( shtParamCount ++ > 0 )strBuilder.Append ( "&" ) ;//&//&
strBuilder.Append ( strParametrs [i,0] ) ;
strBuilder.Append ( '=' ) ;
strBuilder.Append ( strParametrs [i,1] == null ? "" : strParametrs [i,1] ) ;
}
}
url = strBuilder.ToString () ;
//navgation
if( bNewWindow )
{
this.Response.Write ( "<script language=JavaScript> "
+"function ShowDialog ( url,iWidth,iHeight ) { "
+"var iTop =( window.screen.height-iHeight )/2;var iLeft =(window.screen.width-iWidth )/2; "
+"var DialogPath ='scrollbars=yes,toolbar =no,resizable =yes,
status = no,menubar = no,titlebar =no,location =yes,Width ='+iWidth +' ,Height='+iHeight
+',Top ='+iTop+',Left ='+iLeft;"+"window.open (url,'',DialogPath ); } ");
string strB = " ShowDialog (' "+(url == "" ? strPageTo : url )+"',"+nWidth+","+nHeight +");</script> ";
Response.Write ( strB ) ;
}
else
Response.Redirect ( url == "" ? strPageTo : url ) ;
}
. aspx 文件中的用 javscript 脚本为控件添加事件
<head >
…
…
<script language="javascript ">
function displayDiv1 ()
{
document .getElementById ( "Div1").style .display ="";
}
</script>
</head>
<body >
<form id="Form1" method="post" runat ="server">
<input type ="button" id="btn2" runat ="server" value ="displayDiv " onclick ="displayDiv1( ) ">
<br >
<div id="Div1" style ="DISPLAY: none; BACKGROUND -COLOR: #009999" runat ="server">
一个 DIV 图层
</div>
<br >
</form>
</body>
aspx.cs 文件中实现上面同样的效果,后台执行 javascript 脚本
A :测试一个带 alert() 的脚本文件,在 .cs 中执行
//Page_Load () 中添加:
JavascriptRegistEnvent () ; // 在页面显示之前向页面中写入脚本
// 定义 javascriptRegistEvent ()
public Void JavascriptRegistEvent (object sender,EventArgs e)
{
Response.Write ( "run CustomEvent ()...");
Response.Write ( "Regisit dispalyJavascript ....");
// 测试可用
StringBuilder builder=new StringBuilder ( );
builder.Append ( "<script langugage ='javascript '> ");
builder.Append ( "function displayDiv () ");
builder.Append ( "{ ");
builder.Append ( "alert('abc '); ");
builder.Append ( "document .getElementById ('Div1').style.display =''; ");
builder.Append ( "} ");
builder.Append ( "</script> ");
Response.Write ( builder.ToString ());
if( !IsStartupScriptRegistered ("Startup"))
{
this.RegisterStartupScript ( "Startup",builder.ToString ());
this.bnt1.Attributes.Add( "Onclick ", "displayDiv ()");
}
}
// 效果如前台脚本一致吗?似乎有问题!