simulating hyperlink.target="_parent" with response.redirect - javascript

how can I simulate hyperlink.target="_parent" with response.redirect in ASP.NET codebehind (C#), I want to redirect to a page (from code behind) but as I'm in a 2-frame window, the whole page should be redirected, i.e. something like hyperlink.target="_parent", I suppose it should be possible via JavaScript, can you give some help please? I want something cross-browser of course
thanks

You can manipulate other frames/windows only on the client-side (through javascript, user interaction etc.)
Response.Redirect is a server-side construction; the server doesn't know anything about your frames and cannot perform the necessary action. The workaround could be to say the page to execute javascript code when it will be returned to the client.
So ClientScript.RegisterStartupScript method (you already mentioned it in your comment) could be the best option here.

Related

Javascript function return in lotusscript agent

I need to make an adjustment in an application developed in Notes, with the classic development without using xpages. The application needs to give the user an alert the moment an action button is clicked. In this button there is a validation in javascript, which when validating successfully submits the form, which in turn executes a lotusscript agent in its webquerysave event. At this point, some processing is performed, and after processing a condition is verified to generate an alert or not. I thought of generating the alert from the confirm function of javascript, but I do not know how to catch the return from the confirm function to know if I keep the agent code or I finish executing and return to the submitted document.
In the application only javascript client side and lotuscript agent are used. I want to know if it is possible from a lotuscript agent to execute a javascript code and return to the lotusscript code of the agent.
It would be very helpful to understand what language you are using to "confirm" the response. In most cases a timeout should be sufficient. Are you getting any kind of response at all?
You say that you are executing the WebQuerySave event. That means that you are submitting your form. If this is classic Domino web development with no AJAXy stuff going on, then the moment you submitted the form the code that is already loaded in your browser is done. The WebQuerySave agent will either directly generate or redirect to a new page, whose code will be loaded into your browser in place of what was already there. The logic in that agent will have to generate new script and set the appropriate field values that tells the script to put up your alert.
*And if there is AJAXy stuff going on, you're going to need to show your code in order for people to have enough understanding of what you're doing in order to help you.
I'm assuming you're trying to check the record against other data as part of the validation before allowing it to be saved. The problem is once a WebQuerySave agent is called the document is posted and you have to direct the user to a new page. So you need to do your validation before the post.
The simplest way is to do an xhttprequest during your javascript validation routine, before posting. You'll need to call a LotusScript agent (or SSJS Rest service or DDS) that returns a value that you can check against before submitting.

ASP.NET call a Javascript function

I'm working on a project that uses IP Payments to process transactions. The project involves a web form written in ASP with Code-Behind written in C#.
IPP offers an iFrame implementation, where you can put an iFrame in your page and display a small IPP page with fields for entering credit card information. The idea behind this is that the credit card info will only be handled by IPP and never by the server running the page, thus there is no requirement to ensure that card data is kept secure.
In order to display the IPP page in the iFrame though, a session needs to be initiated with IPP. The server initiates the session, and passes in a SessionID variable. Upon a successful session initiation, a Secure Session Token is returned to the server. The server then needs to "force" the client's browser to GET or POST the SessionID and the SST (Secure Session Token) to the IPP website. This is where my problem is.
I wrote a Javascript function in the ASPX page that would accept two parameters - the SessionID and SST - and send them to the IPP website. I'm now trying to call this Javascript function from my C# code upon successful initiation of the IPP session. However, I have been completely unable to do so.
I've done a lot of searching, and the one answer I keep coming across is to use either RegisterStartupScript or RegisterClientScriptBlock. The problem is, these seem to insert text directly into the page, rather than calling an existing function. Assuming I inserted my function into the page via one of those functions rather than writing it into the page myself, it still doesn't solve my problem of how to call said function.
Now it is possible that I'm going about this the wrong way, and there's a much better way to get the client's browser to GET/POST the SessionID and SST; if so, please tell me. I'm inexperienced with web programming and am thus learning as I go and making up solutions along the way that are quite likely not ideal.
Thanks in advance.
I think this should work:
Lets say you have something like this in your HTML:
<html>
<head>
<script>
function sendValuesToIPP(sessionId, sst){
//do stuff
}
</script>
</head>
</html>
If you do this in your C# code it should work
ClientScriptManager.RegisterStartupScript(
this.Type,
"some_key_you_want_to_identify_it",
string.Format("sendValuesToIPP('{0}','{1}')", SessionID, SST),
true);
Keep in mind that I'm assuming you have SessionID and SST properties server side, you can get them from wherever you want and just add them to the string that will actually call the function when registered in your ASPX.

Using JavaScript to send String to Servlet, and Results from servlet back to JavaScript

First of all: sorry for my bad grammer. English isn't my native language, but i will try to exlpain my problem as simple as i can.
I'm working on a web-application, where user can enter a link. (Question 1) This link should be send to the server/servlet and will be progressed to other things. (Question 2) After the progression, the servlet will send a json-array (?) back to the javascript-part of my app.
I'm completly new to this kind of stuff, but its very important to me, to find out how this works or better, how i can make this work. Its actually very simple, but i used plenty of weeks and cant figure it out.
The application is using the SAP UI5-libs (Question 3), where i would also like to know, if there is any possible way, to parse JSON with the UI5 libs.
I hope, i could explain my problem good enough, so i can get some help. Thanks to all!
The 'sending' of the string to the server/servlet would happen via ajax in either POST or GET form. That is up to you.
I recommend you use a javascript plugin like jQuery (JQuery Ajax API) because the regular ajax code is a bit messy.
As for the servlet/server communicating back to the client is as simple as writing to the page. In a typical servlet context it would be something like
out.print("This is a message");
where Ajax automatically returns the content of the entire page upon callback.
So in conclusion:
Consider test.jsp your servlet. I wish to send "Hi" from the client (being the browser) via GET to the servlet and I want the servlet to say "Hello" back.
I would open an ajax request of type GET to the url "test.jsp?param=Hi". In the servlet I receive this page request and process it. The servlet discards the parameter because it is not used and outputs "Hello" to the page.
In the client the ajax will have returned "Hello" and I can use this to put it into a var or whatever and all of this happened while not refreshing and not navigating in the original document where I did the javascript.
Another way is using websockets where you basically use sockets in javascript to send and receive any kind of data.
Also please check out this possible duplicate question: How to send a string to a servlet from javascript using xmlhttprequest

How do you make a link perform an action without reloading a page?

The clearest example of this I could think of is the Reddit Upvote/downvote buttons how when you click the button, the value for upvotes is updated, the upvote button lights up, and the page DOES NOT reload, you just stay exactly where you are on the page.
I am trying to make a feature similar to this and I can totally figure out how to do it with reloading, but I want it to not reload so the user experience isn't disrupted.
Is it possible to do this with php? or would I need to use javascript or something?
The action I would need it to perform would be a basic update query in the database.
This would be done with an Ajax call to your php script. Ajax is designed for these asynchronous updates and/or reloads.
Another way you can do this is with HTML5 WebSockets. You could have the client send a trigger to the server when the user clicks the upvote, and then the server could update and push back the data. It would, however, be a bit overfill for this.
If what you want to do is to contact a server to either send it some state or to retrieve some state from the server (or both), then you would use AJAX with javascript in order to contact the server without reloading the page. You can then also use javascript to update the state of your page after the operation. That is generally what the Reddit page you refer to is doing.
Conceptually, you'd set up your page like this:
Put the link on the page.
With javascript install an event handler so you are notified of a click on the link.
When the link is clicked, your event handler will be called.
Prevent the default behavior of the link so the browser doesn't navigate to a new page.
Then, in the event handler, send your data to the server using AJAX. You will obviously need a URL on your server and server process that can accept and process the data for you and return a value if you need to.
If you need the response from the server, then set up a callback function for when the AJAX call completes (this will be some indeterminate time in the future).
Then, if you need to change the current page in any way (like show one more upvote), then you can modify the current page with javascript to show that new state.
Ajax is easier to use with a library (like jQuery) that contains some ajax support code, but you can certainly implement it in plain javascript too.
Here's one example of ajax with plain javscript. You can find many other examples with Google.
This MDN tutorial on AJAX seems pretty helpful too to show you how it works.
You could use JavaScript to do this. Here's a quick sample:
Vote Up
Simple solution in JavaScript:
var el = document.getElementById("upvoteBtn");
el.addEventListener("click", onVoteClick);
function onVoteClick(e) {
e.preventDefault();
// do something
}
Here's a fiddle.
NOTE: I see you'd be updating the database. In that case, you would have to use AJAX in the onVoteClick function (or use XMLHttpRequest) for this. JavaScript is a client-side programming language and will not be able to communicate to the server without the use of AJAX or XMLHttpRequest. Using the jQuery library, you should be able to write AJAX pretty easy.
It's called AJAX.
With AJAX you can send a request in the background.
The easiest way is to use the jquery libary for this.
You can also output some data as JSON back to the script if you want to take some other actions depending on the result from that query.
A good tutorial is this one.
It also explains how this requests (called: XMLHttpRequest) work.
You need to use Javascript's XMLHttpRequest
You can use AJAX...
It allows you to use JavaScript (client side) to call server side functions. Here's a good example.

ASP.NET/JavaScript - Script Registration/Redirection Problem

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 accomplish 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).
I 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 complicated, but you get the general idea of what can be accomplished using the built in framework components.
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 combine 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.

Categories