在開發應用程序過程中編寫Script是件很煩人的事情其實我們可以把常用的Script裝成相應的NET組件在服務端調用這樣可以大大簡化Script的編寫還提高的Script的重用
以下是常用的打開模式窗體並獲取返回值的類調用實際上可以給設置多個參數傳入和不同控件獲取不同返回值的結果.
定義打開窗體並設置返回值腳本類
HFSoftWebScriptsShowModalDialog dialog=new HFSoftWebScriptsShowModalDialog(
XXXUnitsConfigWebBoot+Appunit/WindowOpenDialogaspx
FrmFailureLogSearchPageURI());
dialogHeight=;
dialogWidth=;
dialogReturnElementsAdd(new HFSoftWebScriptsReturnElement(thistxtReturnClientIDReturnString));
HFSoftWebScriptsReGISterScriptRegiOnClick(cmdSearchdialog);
打開窗體設置返回值的代碼
HFSoftWebScriptsReturnValues returnValue=new HFSoftWebScriptsReturnValues();
returnValueAdd(ReturnStringfilter);
HFSoftWebScriptsRegisterScriptRegiPageClient(thisreturnreturnValue);
打開窗體類的代碼(其實很多Script都可以通過這樣的方式來封裝).
namespace HFSoft Web Scripts
{
/// <summary>
/// 創建打開窗體腳本類
/// 注意:返回值參數據接收的數據格式必須
/// key=value|key=value|
/// </summary>
public class ShowModalDialog : IExecuteScript
{
private const string _Dialog =windowshowModalDialog({} dialogHeight : { } px ;
dialogWidth : { } px ; edge : Raised ; center : Yes ; help : No ; resizable : No ; status : No ; scroll :
Yes ;); ;
#region IExecuteScript 成員
private bool mParent = false ;
/// <summary>
/// 是否需要加載父指向
/// </summary>
public bool Parent
{
get
{
// TODO: 添加 SetElementsValueParent getter 實現
return mParent ;
}
set
{
// TODO: 添加 SetElementsValueParent setter 實現
mParent = value ;
}
}
private string GetParent ()
{
if ( Parent )
return parent ;
return ;
}
/// <summary>
/// 構造對象
/// </summary>
/// <param name=pageContainer>容器頁</param>
/// <param name=openPage>具本打開的頁面</param>
public ShowModalDialog ( string pageContainer string openPage )
{
PageContainer = pageContainer ;
OpenPage = openPage ;
}
public const string PageUrlTag =pageurl ;
/// <summary>
/// 生成腳本
/// </summary>
/// <returns>string</returns>
public string Execute ()
{
// TODO: 添加 ShowModalDialogExecute 實現
string url = + PageContainer + ? pageurl =+ + GetPage ();
url = string Format ( _Dialog url Height Width );
url = GetParent () + url ;
if ( ReturnElements Count >)
{
url = var getvalue= + url +;if(getvalue != null){ ;
foreach ( ReturnElement item in ReturnElements )
{
url += item GetScript ( GetParent ());
}
url +=} ;
return url +; ;
}
return url +; ;
}
#endregion
private string mPageContainer ;
/// <summary>
/// 獲取或設置容器頁(包含路徑)
/// </summary>
public string PageContainer
{
get
{
return mPageContainer ;
}
set
{
mPageContainer = value ;
}
}
private string mOpenPage ;
/// <summary>
/// 打開的頁(包含路徑)
/// </summary>
public string OpenPage
{
get
{
return mOpenPage ;
}
set
{
mOpenPage = value ;
}
}
private int mHeight =;
/// <summary>
/// 獲取或設置打開窗體的高度
/// </summary>
public int Height
{
get
{
return mHeight ;
}
set
{
mHeight = value ;
}
}
private int mWidth =;
/// <summary>
/// 獲取或設置打開窗體的寬度
/// </summary>
public int Width
{
get
{
return mWidth ;
}
set
{
mWidth = value ;
}
}
private ReturnElementCollections mReturnElements = new ReturnElementCollections ();
/// <summary>
/// 獲取返回值元素集
/// </summary>
public ReturnElementCollections ReturnElements
{
get
{
return mReturnElements ;
}
}
private ParameterCollection mParameters = new ParameterCollection ();
/// <summary>
/// 獲取打開頁面的參數集
/// </summary>
public ParameterCollection Parameters
{
get
{
return mParameters ;
}
}
private string GetPage ()
{
if ( Parameters Count ==)
return +OpenPage+ ;
System Text StringBuilder sb = new System Text StringBuilder ();
sb Append ( +OpenPage+ );
string param = ;
string parent = GetParent ();
for ( int i =; i < Parameters Count ; i ++)
{
if ( Parameters [ i ] Element == ElementType Element )
{
param = + Parameters[i]Name + = + + parent +documentall(
+Parameters[i]Value + )value ;
}
else if ( Parameters [ i ] Element == ElementType Select )
{
param = + Parameters[i]Name + = + + parent +__getSeletedButton( + parent +
documentall( +Parameters[i]Value + )) ;
}
if ( i ==)
{
sb Append (+ +SystemWebHttpUtilityUrlEncode( ?) + + + param );
}
else
{
sb Append (+ +SystemWebHttpUtilityUrlEncode( &) + + + param );
}
}
return sb ToString ();
}
}
#region subClass
public enum ElementType
{
None
Element
Select
}
/// <summary>
/// 參數描述類
/// </summary>
public class Parameter
{
/// <summary>
/// 構造參數對象
/// </summary>
public Parameter ()
{
}
/// <summary>
/// 構造指定名稱和值的參數對象
/// </summary>
/// <param name=name>參數名稱</param>
/// <param name=value>參數值</param>
public Parameter ( string name string value )
{
Name = name ;
Value = value ;
}
/// <summary>
/// 構造指定名稱和值的參數對象
/// </summary>
/// <param name=name>參數名稱</param>
/// <param name=value>參數值</param>
/// <param name=iselement>值是否元素名稱</param>
public Parameter ( string name string value ElementType element )
{
Name = name ;
Value = value ;
Element = element ;
}
private string mName ;
/// <summary>
/// 獲取或設置參數名稱
/// </summary>
public string Name
{
get
{
return mName ;
}
set
{
mName = value ;
}
}
private string mValue ;
/// <summary>
/// 獲取或設置參數值
/// </summary>
public string Value
{
get
{
return mValue ;
}
set
{
mValue = value ;
}
}
private ElementType mElement = ElementType None ;
/// <summary>
/// 獲取或設置參數值是否參數名稱
/// </summary>
public ElementType Element
{
get
{
return mElement ;
}
set
{
mElement = value ;
}
}
}
public class ParameterCollection : System Collections CollectionBase
{
public Parameter this [ int index ]
{
get
{
return ( ( Parameter ) List [ index ] );
}
set
{
List [ index ] = value ;
}
}
public int Add ( Parameter value )
{
return ( List Add ( value ) );
}
public int IndexOf ( Parameter value )
{
return ( List IndexOf ( value ) );
}
public void Insert ( int index Parameter value )
{
List Insert ( index value );
}
public void Remove ( Parameter value )
{
List Remove ( value );
}
public bool Contains ( Parameter value )
{
// If value is not of type Int this will return false
return ( List Contains ( value ) );
}
}
/// <summary>
/// 返回值接收元素描述類
/// </summary>
public class ReturnElement
{
/// <summary>
/// 構造對象
/// </summary>
/// <param name=id>接收值的元素ID</param>
/// <param name=key>對應值的鍵值</param>
public ReturnElement ( string id string key )
{
ID = id ;
Key = key ;
}
private string mID ;
/// <summary>
/// 獲取或設置元素ID
/// </summary>
public string ID
{
get
{
return mID ;
}
set
{
mID = value ;
}
}
private string mKey ;
/// <summary>
/// 獲取或設置對應值的鍵值
/// </summary>
public string Key
{
get
{
return mKey ;
}
set
{
mKey = value ;
}
}
/// <summary>
/// 獲取操作腳本
/// </summary>
/// <returns>string</returns>
public string GetScript ( string parent )
{
return parent +documentall( +ID + )value= + parent +__AnalyseString( +Key +
getvalue); ;
}
}
public class ReturnElementCollections : System Collections CollectionBase
{
public ReturnElement this [ int index ]
{
get
{
return ( ( ReturnElement ) List [ index ] );
}
set
{
List [ index ] = value ;
}
}
public int Add ( ReturnElement value )
{
return ( List Add ( value ) );
}
public int IndexOf ( ReturnElement value )
{
return ( List IndexOf ( value ) );
}
public void Insert ( int index ReturnElement value )
{
List Insert ( index value );
}
public void Remove ( ReturnElement value )
{
List Remove ( value );
}
public bool Contains ( ReturnElement value )
{
// If value is not of type Int this will return false
return ( List Contains ( value ) );
}
}
#endregion
}
From:http://tw.wingwit.com/Article/program/net/201311/12329.html