admin管理员组文章数量:1417070
My page has a submit button on it (server-side button).
Here's 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, i register the JavaScript using Page.ClientScript.RegisterStartupScript
, but this will have no effect as the page is not being re-rendered on postback (which is where the script WOULD be executed), because instead a Response.Redirect
happens.
The only solution i can think is to make the page i redirect to "aware" that im trying to execute some JavaScript, be it QueryString, HttpContext.Current.Items, or (gulp) Session.
- QueryString - not an option, as it's JavaScript im trying to execute.
- HttpContext.Current.Items - also not an option because im doing a Response.Redirect (which loses the request-level data, and i also cannot use Server.Transfer because this doesn't play nice with URL Rewriting).
- Session - of course, but not ideal.
Any other ideas/suggestions?
EDIT for Clarification:
The JavaScript im executing is a call to a Facebook client-side API to publish to the user's wall. It has to be done client-side. I pass to the script things like "title", "message", "action links", etc. Basically a bunch of JSON. But the key here is that this data is created on postback, so i cant just execute this function on client-side click.
So what im trying to acplish is on submit button click, execute some javascript and do a redirect (does not have to be in that order, just both need to happen).
My page has a submit button on it (server-side button).
Here's 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, i register the JavaScript using Page.ClientScript.RegisterStartupScript
, but this will have no effect as the page is not being re-rendered on postback (which is where the script WOULD be executed), because instead a Response.Redirect
happens.
The only solution i can think is to make the page i redirect to "aware" that im trying to execute some JavaScript, be it QueryString, HttpContext.Current.Items, or (gulp) Session.
- QueryString - not an option, as it's JavaScript im trying to execute.
- HttpContext.Current.Items - also not an option because im doing a Response.Redirect (which loses the request-level data, and i also cannot use Server.Transfer because this doesn't play nice with URL Rewriting).
- Session - of course, but not ideal.
Any other ideas/suggestions?
EDIT for Clarification:
The JavaScript im executing is a call to a Facebook client-side API to publish to the user's wall. It has to be done client-side. I pass to the script things like "title", "message", "action links", etc. Basically a bunch of JSON. But the key here is that this data is created on postback, so i cant just execute this function on client-side click.
So what im trying to acplish is on submit button click, execute some javascript and do a redirect (does not have to be in that order, just both need to happen).
Share Improve this question edited Aug 25, 2010 at 2:37 RPM1984 asked Aug 25, 2010 at 2:08 RPM1984RPM1984 73.2k62 gold badges240 silver badges363 bronze badges 7- It would be nice if you could describe "what" you were trying to acplish as there might be a better solution to the problem. There are many ways to solve the problem, but also many that may be unsuitable to your particular situation. – Josh Commented Aug 25, 2010 at 2:15
- @Josh - question expanded. Any better? – RPM1984 Commented Aug 25, 2010 at 2:19
- Is cross-page postback not an option? – Jim Schubert Commented Aug 25, 2010 at 2:33
- @Jim If you've ever worked with ASP.NET Forms, you'll understand that cross-page postbacks are not a simple solution for any situation in ASP Forms :-/ – Dan Herbert Commented Aug 25, 2010 at 2:40
- @Dan I do use Web Forms daily, but I've never had this issue or similar constraints. Also, I've never used cross-page postback, but I was wondering why that wasn't listed as a possibility. Whenever my javascript has relied on some server-side data, I've used AJAX.NET Pro or page methods. I doubt either would be useful in this situation since user information is involved. – Jim Schubert Commented Aug 25, 2010 at 2:49
3 Answers
Reset to default 5I think what you are experiencing is the unfortunate clashing of two different paradigms here. On the one side you have an AJAX style API you want to take advantage of, and on the other side you have the ASP.Net page postback model.
Now, while these two are not mutually exclusive, it can present some challenges. I agree with Dan that your best bet is to bend a little more towards the AJAX approach instead of the other way around.
A nice feature in ASP.Net is the ability to turn a single static method in your page into a pseudo web service. You can then use the ScriptManager to generate client-side proxy classes to call that method for you, but you can use whatever client side library you want.
A very simple example:
In your codebehind for you Page
[WebMethod]
public static Person GetPerson(Int32 id, String lastName)
{
return DataAccess.GetPerson(id, lastName);
}
If you were using the ASP.Net AJAX library to handle this for you, then you would need to enable page methods to generate the client-side proxies.
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
Then you could call that from client-side script like so:
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 are posting to the server may be very plicated, but you get the general idea of what can be acplished using the built in framework ponents.
I hope this helps.
If you use Response.Redirect
, the java script you registered at the previous line will not executed. I think what you want to do after clicking the submit button is:
- Save something
- Execute javascript
- 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
.
Could you run the JavaScript on the second page?
Other than that, your options are somewhat limited. You could use AJAX to get the data you need from the server and then redirect. That would improve the UX since at the very least you wouldn't have extra page loads to run your intermediary JavaScript.
Another option would be to use Server.Transfer(...)
, which works similarly to Response.Redirect
, but it doesn't send a redirect header to the client. It simply tells the server "stop running the current page and start executing a new page". Context.Items
will remain in scope between the 2 classes because you're only transferring the responsibility of responding to the request, not the entire context of the request.
You could also bine these 2 solutions. Use Server.Transfer
to keep Context.Items
values in scope and then render the JS on the second page using whatever values you kept from the first page.
本文标签: ASPNETJavaScriptScript RegistrationRedirection ProblemStack Overflow
版权声明:本文标题:ASP.NETJavaScript - Script RegistrationRedirection Problem - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745260312a2650326.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论