views:

29

answers:

1

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.

The 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?

A: 

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.

geoff
As mentioned above, I have limited access to the aspx page because this is in a content managed environment. That's why I'm trying to keep everything neatly tucked up in a usercontrol.
Max