下載例子和相關源碼
在開發ASP.NET應用程式過程中編寫Script是件很煩人的事情,其實我們可以把常用的Script裝成相應的.NET組件在服務端調用,這樣可以大大簡化Script的編寫還提高的Script的重用.
以下是常用的開啟模式表單並擷取傳回值的類調用,實際上可以給設定多個參數傳入和不同控制項擷取不同傳回值的結果.
定義開啟表單並設定傳回值指令碼類.
HFSoft.Web.Scripts.ShowModalDialog dialog=new HFSoft.Web.Scripts.ShowModalDialog(
XXX.Units.Config.WebBoot+"Appunit/WindowOpenDialog.aspx",
FrmFailureLogSearch.PageURI());
dialog.Height=400;
dialog.Width=600;
dialog.ReturnElements.Add(new HFSoft.Web.Scripts.ReturnElement(this.txtReturn.ClientID,"ReturnString"));
HFSoft.Web.Scripts.RegisterScript.RegiOnClick(cmdSearch,dialog);
開啟表單設定傳回值的代碼
HFSoft.Web.Scripts.ReturnValues returnValue=new HFSoft.Web.Scripts.ReturnValues();
returnValue.Add("ReturnString",filter);
HFSoft.Web.Scripts.RegisterScript.RegiPageClient(this,"return",returnValue);
開啟表單類的代碼(其實很多Script都可以通過這樣的方式來封裝).
00006 namespace HFSoft . Web . Scripts
00007
{
00008
/// <summary>
00009
/// 建立開啟表單指令碼類
00010
/// 注意:傳回值參資料接收的資料格式必須
00011
/// key=value|key1=value1|.....
00012
/// </summary>
00013
public class ShowModalDialog : IExecuteScript
00014
{
00015
private const string _Dialog ="window.showModalDialog({0},' ',' dialogHeight : { 1 } px ;
dialogWidth : { 2 } px ; edge : Raised ; center : Yes ; help : No ; resizable : No ; status : No ; scroll :
Yes ;');" ;
00016
#region IExecuteScript 成員
00017
private bool mParent = false ;
00018
/// <summary>
00019
/// 是否需要載入父指向
00020
/// </summary>
00021
public bool Parent
00022
{
00023
get
00024
{
00025
// TODO: 添加 SetElementsValue.Parent getter 實現
00026
return mParent ;
00027
}
00028
set
00029
{
00030
// TODO: 添加 SetElementsValue.Parent setter 實現
00031
mParent = value ;
00032
}
00033
}
00034
private string GetParent ()
00035
{
00036
if ( Parent )
00037
return "parent." ;
00038
return "" ;
00039
}
00040
/// <summary>
00041
/// 構造對象
00042
/// </summary>
00043
/// <param name="pageContainer">容器頁</param>
00044
/// <param name="openPage">具本開啟的頁面</param>
00045
public ShowModalDialog ( string pageContainer , string openPage )
00046
{
00047
PageContainer = pageContainer ;
00048
OpenPage = openPage ;
00049
}
00050
public const string PageUrlTag ="pageurl" ;
00051
/// <summary>
00052
/// 產生指令碼
00053
/// </summary>
00054
/// <returns>string</returns>
00055
public string Execute ()
00056
{
00057
// TODO: 添加 ShowModalDialog.Execute 實現
00058
string url ="' " + PageContainer +" ? pageurl ='+" + GetPage ();
00059
url = string . Format ( _Dialog , url , Height , Width );
00060
url = GetParent () + url ;
00061
if ( ReturnElements . Count >0)
00062
{
00063
url = "var getvalue=" + url +";if(getvalue != null){" ;
00064
foreach ( ReturnElement item in ReturnElements )
00065
{
00066
url += item . GetScript ( GetParent ());
00067
}
00068
url +="}" ;
00069
return url +";" ;
00070
}
00071
return url +";" ;
00072
}
00073
00074
#endregion
00075
private string mPageContainer ;
00076
/// <summary>
00077
/// 擷取或設定容器頁(包含路徑)
00078
/// </summary>
00079
public string PageContainer
00080
{
00081
get
00082
{
00083
return mPageContainer ;
00084
}
00085
set
00086
{
00087
mPageContainer = value ;
00088
}
00089
}
00090
private string mOpenPage ;
00091
/// <summary>
00092
/// 開啟的頁(包含路徑)
00093
/// </summary>
00094
public string OpenPage
00095
{
00096
get
00097
{
00098
return mOpenPage ;
00099
}
00100
set
00101
{
00102
mOpenPage = value ;
00103
}
00104
}
00105
private int mHeight =400;
00106
/// <summary>
00107
/// 擷取或設定開啟表單的高度
00108
/// </summary>
00109
public int Height
00110
{
00111
get
00112
{
00113
return mHeight ;
00114
}
00115
set
00116
{
00117
mHeight = value ;
00118
}
00119
}
00120
private int mWidth =400;
00121
/// <summary>
00122
/// 擷取或設定開啟表單的寬度
00123
/// </summary>
00124
public int Width
00125
{
00126
get
00127
{
00128
return mWidth ;
00129
}
00130
set
00131
{
00132
mWidth = value ;
00133
}
00134
}
00135
private ReturnElementCollections mReturnElements = new ReturnElementCollections ();
00136
/// <summary>
00137
/// 擷取傳回值元素集
00138
/// </summary>
00139
public ReturnElementCollections ReturnElements
00140
{
00141
get
00142
{
00143
return mReturnElements ;
00144
}
00145
}
00146
private ParameterCollection mParameters = new ParameterCollection ();
00147
/// <summary>
00148
/// 擷取開啟頁面的參數集
00149
/// </summary>
00150
public ParameterCollection Parameters
00151
{
00152
get
00153
{
00154
return mParameters ;
00155
}
00156
}
00157
private string GetPage ()
00158
{
00159
if ( Parameters . Count ==0)
00160
return "' "+OpenPage+" '" ;
00161
System . Text . StringBuilder sb = new System . Text . StringBuilder ();
00162
sb . Append ("' "+OpenPage+" '" );
00163
string param ="" ;
00164
string parent = GetParent ();