ASP.NET/JavaScript - Script Registration / Redirect Problem

There is a submit button on my page (server button).

Here is my code for the click event:

protected void SubmitButton_Click(object sender, EventArgs e) { db.SaveSomething(); Page.ClientScript.RegisterStartupScript("someScriptWhichReliesOnServerData"); Response.Redirect("SomeOtherPage.aspx"); } 

Now the problem is that I am registering JavaScript with Page.ClientScript.RegisterStartupScript , but this will not have any effect, since the page will not be re-rendered upon postback (where the script will be executed), because instead of this is a Response.Redirect .

The only solution I can think of is to make the page redirected to "aware" that I'm trying to execute some kind of JavaScript, be it QueryString, HttpContext.Current.Items or (gulp) Session.

  • QueryString is not an option as it tries to execute JavaScript.
  • HttpContext.Current.Items is also not an option, because im does Response.Redirect (which loses data at the request level, and I also can not use Server.Transfer, because it doesn’t work well with URL rewriting).
  • Session - of course, but not perfect.

Any other ideas / suggestions?

EDIT for clarification:

Running javascript im is calling the Facebook client API for posting to the user's wall. This must be done on the client side. I move on to script things like "title", "message", "action links", etc. This is basically a bunch of JSON. But the key point here is that this data is created during postback, so I can’t just perform this function on the client on the client side.

So what I'm trying to accomplish is to click the submit button, execute some kind of javascript and do a redirection ( not necessarily in that order, just both have to happen ).

+4
source share
3 answers

I think you are experiencing an unsuccessful clash of two different paradigms here. On the one hand, you have the AJAX style API that you want to use, and on the other hand, you have an ASP.Net back page.

Now, although the two are not mutually exclusive, they can present some problems. I agree with Dan that it is best to lean a bit towards the AJAX approach, and not vice versa.

A good feature of ASP.Net is the ability to turn one static method into your page into a pseudo-web service. You can then use ScriptManager to create client-side proxy classes to call this method for you, but you can use any client-side library you want .

A very simple example:

In your code for you Page

 [WebMethod] public static Person GetPerson(Int32 id, String lastName) { return DataAccess.GetPerson(id, lastName); } 

If you used the ASP.Net AJAX library to handle this for you, you will need to include page methods to create client proxies.

 <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"> </asp:ScriptManager> 

Then you can call it the client side script as follows:

 function CallGetPerson() { var id = $get("txtPersonId").value; var lastName = $get("txtLastName").value; // PageMethods is a class that is part of the ASP.Net AJAX // client-side libraries and will contain your auto-generated // proxy methods for making XHR requests. PageMethods.GetPerson(id, lastName, OnGetPersonComplete); } function OnGetPersonComplete(result) { faceBookApi.DoSomeStuffWithJson(result); window.location = "NewPage.aspx"; } 

Now again this is a contrived example, and what you send to the server can be very complicated, but you get a general idea of ​​what can be done using the built-in infrastructure components.

Hope this helps.

+5
source

If you use Response.Redirect , the java script registered in the previous line will not be executed. I think what you want to do is by clicking the submit button:

  • Save something
  • Javascript execution
  • Redirect to another page

Here you can use:

 protected void SubmitButton_Click(object sender, EventArgs e) { db.SaveSomething(); Page.ClientScript.RegisterStartupScript("someScriptWhichReliesOnServerData"); Page.ClientScript.RegisterStartupScript("window.location.href=XXXXXX"); } 

That is, using javascript to redirect the page instead of Response.Redirect .

+2
source

Failed to start JavaScript on the second page?

In addition, your options are somewhat limited. You can use AJAX to get the necessary data from the server, and then redirect it. This will improve UX since at least you will not have additional page loads to run your intermediate JavaScript.


Another option is to use Server.Transfer(...) , which works similarly to Response.Redirect , but does not send a redirect header for the client. It simply tells the server to "stop executing the current page and start executing a new page." Context.Items will remain in scope between the two classes because you only pass the response to the request and not the entire request context.


You can also combine these 2 solutions. Use Server.Transfer to keep the Context.Items values ​​in scope and then display JS on the second page using any values ​​that you saved from the first page.

0
source

All Articles