在开发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 ();
00165 for ( int i =0; i < parameters . count ; i ++)
00166 {
00167 if ( parameters [ i ]. element == elementtype . element )
00168 {
00169 param ="' " + parameters[i].name +" =' + " + parent +"document.all('
"+parameters[i].value + " ').value" ;
00170 }
00171 else if ( parameters [ i ]. element == elementtype . select )
00172 {
00173 param ="' " + parameters[i].name +" =' + " + parent +"__getseletedbutton(" + parent +
"document.all(' "+parameters[i].value + " '))" ;
00174 }
00175 if ( i ==0)
00176 {
00177 sb . append ("+' "+system.web.httputility.urlencode(" ?") +" '+" + param );
00178 }
00179 else
00180 {
00181 sb . append ("+' "+system.web.httputility.urlencode(" &") +" '+" + param );
00182 }
00183 }
00184 return sb . tostring ();
00185 }
00186
00187
00188
00189 }
00190 #region subclass
00191 public enum elementtype
00192 {
00193 none ,
00194 element ,
00195 select
--------------------------------------------------
00196 }
00197 /// <summary>
00198 /// 参数描述类
00199 /// </summary>
00200 public class parameter
00201 {
00202 /// <summary>
00203 /// 构造参数对象
00204 /// </summary>
00205 public parameter ()
00206 {
00207 }
00208 /// <summary>
00209 /// 构造指定名称和值的参数对象
00210 /// </summary>
00211 /// <param name="name">参数名称</param>
00212 /// <param name="value">参数值</param>
00213 public parameter ( string name , string value )
00214 {
00215 name = name ;
00216 value = value ;
00217 }
00218 /// <summary>
00219 /// 构造指定名称和值的参数对象
00220 /// </summary>
00221 /// <param name="name">参数名称</param>
00222 /// <param name="value">参数值</param>
00223 /// <param name="iselement">值是否元素名称</param>
00224 public parameter ( string name , string value , elementtype element )
00225 {
00226 name = name ;
00227 value = value ;
00228 element = element ;
00229 }
00230
00231 private string mname ;
00232 /// <summary>
00233 /// 获取或设置参数名称
00234 /// </summary>
00235 public string name
00236 {
00237 get
00238 {
00239 return mname ;
00240 }
00241 set
00242 {
00243 mname = value ;
00244 }
00245 }
00246 private string mvalue ;
00247 /// <summary>
00248 /// 获取或设置参数值
00249 /// </summary>
00250 public string value
00251 {
00252 get
00253 {
00254 return mvalue ;
00255 }
00256 set
00257 {
00258 mvalue = value ;
----------------------------------------------
00259 }
00260 }
00261 private elementtype melement = elementtype . none ;
00262 /// <summary>
00263 /// 获取或设置参数值是否参数名称
00264 /// </summary>
00265 public elementtype element
00266 {
00267 get
00268 {
00269 return melement ;
00270 }
00271 set
00272 {
00273 melement = value ;
00274 }
00275 }
00276 }
00277 public class parametercollection : system . collections . collectionbase
00278 {
00279 public parameter this [ int index ]
00280 {
00281 get
00282 {
00283 return ( ( parameter ) list [ index ] );
00284 }
00285 set
00286 {
00287 list [ index ] = value ;
00288 }
00289 }
00290
00291 public int add ( parameter value )
00292 {
00293 return ( list . add ( value ) );
00294 }
00295
00296 public int indexof ( parameter value )
00297 {
00298 return ( list . indexof ( value ) );
00299 }
00300
00301 public void insert ( int index , parameter value )
00302 {
00303 list . insert ( index , value );
00304 }
00305
00306 public void remove ( parameter value )
00307 {
00308
00309 list . remove ( value );
00310 }
00311
00312 public bool contains ( parameter value )
00313 {
00314 // if value is not of type int16, this will return false.
00315 return ( list . contains ( value ) );
00316 }
00317
00318 }
00319 /// <summary>
----------------------------------------------------
00320 /// 返回值接收元素描述类
00321 /// </summary>
00322 public class returnelement
00323 {
00324 /// <summary>
00325 /// 构造对象
00326 /// </summary>
00327 /// <param name="id">接收值的元素id</param>
00328 /// <param name="key">对应值的键值</param>
00329 public returnelement ( string id , string key )
00330 {
00331 id = id ;
00332 key = key ;
00333 }
00334 private string mid ;
00335 /// <summary>
00336 /// 获取或设置元素id
00337 /// </summary>
00338 public string id
00339 {
00340 get
00341 {
00342 return mid ;
00343 }
00344 set
00345 {
00346 mid = value ;
00347 }
00348 }
00349 private string mkey ;
00350 /// <summary>
00351 /// 获取或设置对应值的键值
00352 /// </summary>
00353 public string key
00354 {
00355 get
00356 {
00357 return mkey ;
00358 }
00359 set
00360 {
00361 mkey = value ;
00362 }
00363 }
00364 /// <summary>
00365 /// 获取操作脚本
00366 /// </summary>
00367 /// <returns>string</returns>
00368 public string getscript ( string parent )
00369 {
00370 return parent +"document.all(' "+id +" ').value=" + parent +"__analysestring(' "+key +"
',getvalue);" ;
00371 }
00372 }
00373 public class returnelementcollections : system . collections . collectionbase
00374 {
00375 public returnelement this [ int index ]
00376 {
00377 get
00378 {
00379 return ( ( returnelement ) list [ index ] );
00380 }
---------------------------------------------------
00381 set
00382 {
00383 list [ index ] = value ;
00384 }
00385 }
00386
00387 public int add ( returnelement value )
00388 {
00389 return ( list . add ( value ) );
00390 }
00391
00392 public int indexof ( returnelement value )
00393 {
00394 return ( list . indexof ( value ) );
00395 }
00396
00397 public void insert ( int index , returnelement value )
00398 {
00399 list . insert ( index , value );
00400 }
00401
00402 public void remove ( returnelement value )
00403 {
00404
00405 list . remove ( value );
00406 }
00407
00408 public bool contains ( returnelement value )
00409 {
00410 // if value is not of type int16, this will return false.
00411 return ( list . contains ( value ) );
00412 }
00413 }
00414 #endregion
00415 }