Base Web Page

xiaoxiao2021-03-06  102

/ *

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:

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

"<% = 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 = @ "