Vs2008, C #3.0
The previous article mentioned how to subscribe to event processing functions for the client element. This article describes how to transmit parameters while subscribing.
1. Use function. createdelegate (instance, Function Method) Function pageload () {
This . Username = " Guozhijian " ;
This . Password = " Pass " ;
VaR Delegate = Function. createdelegate ( This , Btncommitclick );
This . Showalert = Function () {
Alert ("A");
}
SYS. UI. domevent. addhandler ($ Get ( " Btncommit " ), " Click " , Delegate );
}
Function btncommitclick (EVT) {
Alert (This. Username );
Alert (This. Password );
This. Showalert ();
}
When function. createdelegate is called, the first parameter uses the this keyword. In the delegate function, this keyword can be used to referenceCodeThe member of the object where the block is located (including attributes and methods ).
2. Use function. createcallback (function method, context) Function pageload () {
VaR Context = {
Username:"Guozhijian",
Password:"Pass"
} ;
VaR callback = Function. createcallback (btncommitkeypress, context );
SYS. UI. domevent. addhandler ($ Get ( " Btncommit " ), " Keypress " , Callback );
}
Function btncommitkeypress (EVT, context) {
Alert (context. username );
Alert (context. Password );
}
When function. createcallback is called, the second parameter is used to pass an object defined in the scope of the subscription event processing method ).