Calling a non static method in Server Side by a Client Side Function
I can get an object from the server side by using static receive callbackresult methods from server side.
But I want to run a non-static method in my page which populates an ajax accordion by calling a client side function.
The object I am calling from server side is a complex object which I can't use in client side if I get it by callbackresults.
Is there any other solution that I can run a non static method in an aspx file by a client side control ?
Codes I am using so far ...
function ReceiveServerData(arg, context) {
//Message.innerText = "Date from server: " + arg;
}
#region ICallbackEventHandler Members
public void RaiseCallbackEvent(String eventArgument)
{
// Processes a callback event on the server using the event
// argument from the client.
Insert(); // this is running, but doesnt work !
//printAlternativesFromAirport(eventArgument);
}
public string GetCallbackResult()
{
// Returns the results of a callback event to the client.
return null;开发者_运维知识库
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
ClientScriptManager cm = Page.ClientScript;
String cbReference = cm.GetCallbackEventReference(this, "arg",
"ReceiveServerData", "");
String callbackScript = "function CallServer(arg, context) {" +
cbReference + "; }";
cm.RegisterClientScriptBlock(this.GetType(),
"CallServer", callbackScript, true);
}
yes, you need to create a handler that will create the entire context needed for the page, which will run a full page life cycle ect, and is more recommended when you want to retrieve something like a user control or something big.
public void ProcessRequest(HttpContext context)
{
context.Response.Write(RenderView("~/_controltemplates/15/myDir/Templates/myUC.ascx"));
}
public static string RenderView(string path)
{
try
{
Page pageHolder = new Page();
UserControl viewControl = (UserControl)pageHolder.LoadControl(path);
pageHolder.Controls.Add(viewControl);
StringWriter result = new StringWriter();
Log.Application.Debug(LOGPREFIX + "RenderView before Execute");
HttpContext.Current.Server.Execute(pageHolder, result, true);
return result.ToString();
}
catch (Exception ex)
{
Log.Application.ErrorException(LOGPREFIX, ex);
throw;
}
}
but i think that it is not what you need, instead i would advice you to make an entity (class) to handle that insert function that will not need any httpContext and run it from a simple handler.
another solution you might need, since maybe you do need all the postback info but do not want to make a full postback is to use AjaxPanel or even clear the Response and send "OK" instead.
精彩评论