/ *
Author: clarion li
Date: OCT.22, 2004 (My Nephew Is Two Months OLD)
Purpose:
Check User Login
Client Call Server's Method (in page or userControl)
* /
/ *
Client Function:
Function CallServerMethodskipClientValidate (MethodName, Methodparams)
{
Document.all ['__ methodname']. Value = methodname;
Document.all ['__ methodparams']. Value = methodparams;
Document.Forms [0] .submit ();
}
Function CallServerMethod (MethodName, Methodparams)
{
IF (TypeOf (Page_ClientValidate)! = 'undefined')
{
IF (! page_clientvalidate ()) return;
}
CallServerMethodskipclientValidate (MethodName, Methodparams);
}
Useage:
Method on Page:
"Alert" is the public method in this page
"Hello" is the method "alert" 'S Parameter
Method on UserControls:
* wuc', 'Hello');">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
"<% = this.uniqueID%>" IS UserControls's UniqueID
"Wuc" is the method name in the userControl
"Hello" is the method "alert" 'S Parameter
* /
Using system;
Using system.Web.ui;
Namespace WebApp4
{
Public Class Basepage: PAGE
{
ServerMethodProxy SMP = NULL;
Protected Override Void OnNit (Eventargs E)
{
IF (! this.checkuser ())
{
THIS.FORCELOGIN ();
}
Base.onit (E);
SMP = New ServerMethodProxy (THIS);
}
Protected Virtual Bool Checkuser ()
{
Return True;
}
protected virtual void forcelogin ()
{
Response.Redirect (", true);
}
Protected Override Void OnLoad (Eventargs E) {
Base.onLoad (e);
IF (ISPostback)
{
Smp.handle ();
}
}
}
Public Class ServerMethodProxy: ipostbackeventhandler
{
Private system.Web.ui.page page;
Private const string constmethodname = "__methodname";
Private const string constmethodparams = "__methodparams";
Private string methodname = null;
Private string methodparameters = NULL;
Public ServerMethodProxy (System.Web.ui.page Page)
{
THIS.PAGE = Page;
THIS.REGISTERCLIENTScript ();
}
Public void handle () PUBLIC VOID HANDE ()
{
MethodName = this.page.Request.form [constmethodname];
IF (MethodName "" == "")
{
Return;
}
MethodParameters = this.page.Request.form [constmethodparams];
Try
{
System.Type T = this.page.gettype ();
While (T.BaseType! = NULL)
{
IF (t.fullname == "system.web.ui.page")
{
System.Reflection.fieldInfo Fi = T.Getfield ("_ RegisteredControlthatrequireraiseEvent", System.Reflection.BindingFlags.nonpublic | System.Reflection.BindingFlags.instance);
Fi.setValue (this.page, this);
Break;
}
T = T.BaseType;
}
}
Catch
{
InvokeServerMethod ();
}
}
Private void registerclientscript ()
{
this.page.registerhiddenfield (constmethodname, "");
this.page.registerhiddenfield (constmethodparams, ");
String script = @ "
Function CallServerMethodskipClientValidate (MethodName, Methodparams)
{
Document.all ['constmethodname']. Value = methodname;
Document.all ['constmethodparams']. Value = methodparams;
Document.Forms [0] .submit ();
}
Function CallServerMethod (MethodName, Methodparams) {
IF (TypeOf (Page_ClientValidate)! = 'undefined')
{
IF (! page_clientvalidate ()) return;
}
CallServerMethodskipclientValidate (MethodName, Methodparams);
}
script>
This.Page.RegisterStartupScript ("CallServerMethod", Script.Replace ("constmethodname", constmethodname) .Replace ("constmethodparams", constmethodparams);
}
Private void invokeservermethod ()
{
System.Reflection.MethodInfo mi = null;
Try
{
INT starpos = methodname.indexof ("*");
IF (Starpos! = -1) // Find Star
{
String [] ss = methodname.split ('*');
Control c = this.page.FindControl (ss [0]); // ss [0] IS UserControl's uniqueid
IF (c == null)
{
Throw New Exception ("Can't Find The Type: SS [0]);
}
Mi = this.getMethodByname (C.GETTYPE (). BaseType, SS [1]); // SS [1] is method Name
Mi.invoke (C, New Object [] {MethodParameters};
}
Else
{
Mi = this.getMethodByname (this.page.gettype (). BaseType, MethodName;
mi.invoke (this.page, new object [] {methodparameters};
}
}
Catch (System.Reflection.TargetInvocationException EX)
{
IF (ex.innerexception! = null&& ex.innerexception is system.threading.threadabortexception)
{
Return;
}
Else
{
Throw EX;
}
}
}
Private system.reflection.MethodInfo getMethodByname (System.Type T, String Name)
{
Return T.getMethod (Name);
}
#Region ipostbackeventhandler members
Public void raisePostBackevent (String Eventargument)
{
this.invokeserverMethod ();
}
#ndregion
}
}
ServerMethodProxy for ASP.NET 2.0
Using system;
Using system.collections.Generic;
Using system.text;
Using system.Web.ui;
Namespace freeborders.plm.webframework {
Public Class ServerMethodProxy
{
Private system.Web.ui.page page;
Private const string constmethodname = "__methodname";
Private const string constmethodparams = "__methodparams";
Public ServerMethodProxy (System.Web.ui.page Page)
{
THIS.PAGE = Page;
THIS.PAGE.LOADCOMPLETE = New EventHandler (OnloadComplete);
THIS.REGISTERCLIENTScript ();
}
Void OnloadComplete (Object Sender, Eventargs E)
{
this.invokeserverMethod ();
}
Private void registerclientscript ()
{
This.page.clientscript.registerhiddenfield (constmethodname, "");
This.page.clientscript.registerhiddenfield (constmethodparams, ");
String script = @ "
Function CallServerMethodskipClientValidate (MethodName, Methodparams)
{
Document.all ['constmethodname']. Value = methodname;
Document.all ['constmethodparams']. Value = methodparams;
Document.Forms [0] .submit ();
}
Function CallServerMethod (MethodName, Methodparams)
{
IF (TypeOf (Page_ClientValidate)! = 'undefined')
{
IF (! page_clientvalidate ('')) Return;
}
CallServerMethodskipclientValidate (MethodName, Methodparams);
}
script>
this.page.ClientScript.RegisterClientScriptBlock (this.GetType (), "CallServerMethod", script.Replace ( "constmethodName", constMethodName) .Replace ( "constmethodParams", constMethodParams));
}
Private void invokeservermethod ()
{
String methodname = this.page.Request.form [constmethodname];
IF (String.Insnullorempty (MethodName))
{
Return;
}
String methodparameters = this.page.Request.form [constmethodparams]; system.reflection.methodInfo mi = null;
Try
{
INT UserControluniqueIndlength = methodname.lastIndexof (":");
IF (UserControluniqueIndlength! = -1) // Find User Control Unique ID
{
String UserControlunique = methodname.substring (0, UserControlunique);
String ServerMethodName = methodname.substring (userControluniqueIndlength 1);
Control c = this.page.FindControl (UserControlunique);
IF (c == null)
{
Throw New Exception ("Can't Find The Type:" UserControlunique);
}
Mi = this.getMethodByname (c.gettype (). BaseType, ServerMethodName;
Mi.invoke (C, New Object [] {MethodParameters};
}
Else
{
Mi = this.getMethodByname (this.page.gettype (). BaseType, MethodName;
mi.invoke (this.page, new object [] {methodparameters};
}
}
Catch (System.Reflection.TargetInvocationException EX)
{
EX.innerexception is system.threading.threadabortexception
{
Return;
}
Else
{
Throw EX;
}
}
}
Private system.reflection.MethodInfo getMethodByname (System.Type T, String Name)
{
Return T.getMethod (Name);
}
}
}