Return values ​​from code in asp.net ajax endrequest

Basically, I have an asp.net ajax enabled usercontrol, and now I would just like to return a simple string value that will be used in some js. I run after executing ajax request.

I worked with the endrequest method, which I already configured to handle exceptions, which works fine. But is there no way to simply return a string value?

At first I thought I could do this through Response.Write (); but args.get_response () doesn't like it when you do it automatically - can someone point me in the right direction?

Thanks!

+7
source share
2 answers

You can use the ScriptManager.RegisterDataItem method with Sys.WebForms.PageRequestManager. Consider the following test page, which, when you click the button, registers the data element using the script manager, and on the client side it processes the endrequest and receives this data element.

<%@ Page Language="C#" AutoEventWireup="true" Inherits="WebApplication1._Default" %> <script runat="server"> protected void btTest_Click(object sender, EventArgs e) { ScriptManager1.RegisterDataItem(btTest, "Your value to pass to the client"); } </script> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="upTest" runat="server"> <ContentTemplate> <asp:Button ID="btTest" runat="server" Text="Click Me" OnClick="btTest_Click" /> </ContentTemplate> </asp:UpdatePanel> </div> </form> </body> <script language="javascript" type="text/javascript"> Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler); function endRequestHandler(sender, args) { var dataItems = args.get_dataItems()['<%= btTest.ClientID %>']; if (dataItems != null) alert(dataItems); } </script> </html> 
+11
source

I may not understand your problem, but what user control related to these additional js calls? Are you just rendering js from a user control?

Anyway, IMO. You must use ASP.NET Web Handler (* .ashx) or HttpHandler to return a string value and call the handler with this extra js.

-one
source

All Articles