Writing Scripts during the development of ASP.NET applications is a very annoying thing. In fact, we can install commonly used Scripts into corresponding .NET components and call them on the server. This can greatly simplify the writing of Scripts and improve the reuse of Scripts. .The
following is a commonly used class call to open a modal form and obtain the return value. In fact, you can set multiple parameters and obtain different return values for different controls.
Define a script class that opens the form and sets the return value.
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);
code to open the form and set the return value
HFSoft.Web.Scripts.ReturnValues returnValue=new HFSoft.Web.Scripts.ReturnValues();
returnValue.Add("ReturnString",filter);
HFSoft.Web.Scripts.RegisterScript.RegiPageClient(this, "return", returnValue);
Open the code of the form class (in fact, many Scripts can be encapsulated in this way).
00006 namespace HFSoft . Web . Scripts
00007 {
00008 /// <summary>
00009 /// Create an open form script class
00010 /// Note: The data format received by the return value parameter data must be
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 member
00017 private bool mParent = false ;
00018 / // <summary>
00019 /// Do you need to load the parent pointer
00020 /// </summary>
00021 public bool Parent
00022 {
00023 get
00024 {
00025 // TODO: Add SetElementsValue.Parent getter to implement
00026 return mParent ;
00027 }
00028 set
00029 {
00030 // TODO: Add SetElementsValue.Parent setter to implement
00031 mParent = value ;
00032 }
00033 }
00034 private string GetParent ()
00035 {
00036 if ( Parent )
00037 return "parent." ;
00038 return "" ;
00039 }
00040 /// <summary>
00041 /// Construct object
00042 /// </summary>
00043 /// <param name="pageContainer">Container page</param>
00044 /// <param name="openPage" >Open page</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 /// Generate script
00053 /// </summary>
00054 /// <returns>string</returns>
00055 public string Execute ()
00056 {
00057 // TODO: Add ShowModalDialog.Execute implementation
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 /// Get or set the container page (including path)
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 /// Open page (including path)
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 /// Get or set the height of the open form
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 /// Get or set the width of the open form
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 /// Get the return value element set
00138 /// </summary>
00139 public ReturnElementCollections ReturnElements
00140 {
00 141 get
00142 {
00143 return mReturnElements ;
00144 }
00145 }
00146 private ParameterCollection mParameters = new ParameterCollection ();
00147 /// <summary>
00148 /// Get the parameter set of the open page
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
0 0192 {
00193 None,
00194 Element,
00195 Select
-------------------------------------------------- ----------------------------------
00196 }
00197 /// <summary>
00198 /// Parameter description class
00199 /// </summary>
00200 public class Parameter
00201 {
00202 /// <summary>
00203 /// Construct parameter object
00204 /// </ summary>
00205 public Parameter ()
00206 {
00207 }
00208 /// <summary>
00209 /// Construct a parameter object with the specified name and value
00210 /// </summary>
00211 /// <param name="name"> Parameter name</param>
00212 /// <param name="value">Parameter value</param>
00213 public Parameter ( string name , string value )
00214 {
00215 Name = name ;
00216 Value = value ;
00217 }
00218 / // <summary>
00219 /// Construct a parameter object with specified name and value
00220 /// </summary>
00221 /// <param name="name">Parameter name</param>
00222 /// <param name ="value">Parameter value</param>
00223 /// <param name="iselement">Is the value the element name</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 /// Get or set the parameter name
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 /// Get or set the parameter value
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 /// Get or set whether the parameter value is the parameter name
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 {
0 0281 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 /// Return value receiving element description class
00321 /// </summary>
00322 public class ReturnElement
00323 {
00324 /// <summary>
00325 /// Construct object
00326 /// </summary>
00327 /// < param name="id">Element ID that receives the value</param>
00328 /// <param name="key">Key value of the corresponding value</param>
00329 public ReturnElement ( string id , string key )
00330 {
00331 ID = id ;
00332 Key = key ;
00333 }
00334 private string mID ;
00335 /// <summary>
00336 /// Get or set the element 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 /// Get or set the key value of the corresponding value
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 /// Get operation script
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 )
00 393 {
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 }