ASP.Net Client-callback not working from ASCX control
I need to call ASP.Net server side code from the client. Because I'm in an ascx user control I can't use [webmethod] + PageMethods as this only works on ASPX pages.
T开发者_如何学Pythonhe next option that I thought would work was manually creating a client-callback. Using this method the client will successfully call the server code but the client will not get the call back. This is my code for setting up the client side script blocks:
protected void Page_Load(object sender, EventArgs e)
{
String strJSCallbackPrefix = this.ClientID;
ClientScriptManager cm = Page.ClientScript;
String cbReference = cm.GetCallbackEventReference(this, "arg", strJSCallbackPrefix + "ReceiveServerData", "context");
String callbackScript = "function " + strJSCallbackPrefix + "CallServer(arg, context){" + cbReference + "; }";
cm.RegisterClientScriptBlock(this.GetType(), strJSCallbackPrefix + "CallServer", callbackScript, true);
String strReceiveServerData = "function " + strJSCallbackPrefix + "ReceiveServerData(arg, context){document.getElementById('btnCancel').value='thisandthat';}";
cm.RegisterClientScriptBlock(this.GetType(), strJSCallbackPrefix + "ReceiveServerData", strReceiveServerData, true);
Button2.OnClientClick = strJSCallbackPrefix + "CallServer('test message',1); " + "return false;";
}
public string GetCallbackResult()
{
return returnValue;
}
public void RaiseCallbackEvent(string eventArgument)
{
returnValue = "11223" + eventArgument;
}
Does anyone have any ideas why the client call back is not getting fired?
You could just add the PageMethod into the containing aspx page. It doesn't really matter where it is since you can't interact with the contents of the page/usercontrol from the PageMethod anyway.
Just beware, this might become a maintenance nightmare if the usercontrol is used in lots of pages.
精彩评论