[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Response.Redirect redirecting to same page when using anchors

Posted on 2005-05-17
17
Medium Priority
?
2,071 Views
Last Modified: 2008-03-06
Hi Experts!

I'm not very familiar with ASP.NET but I'm figuring things out as I go...

This is probably easiest to explain if I list the flow...

1) source.aspx calls Response.Redirect("destination.aspx");
2) Button click in destination.aspx calls Response.Redirect("source.aspx");
3) Redirected back to original page ok.

Now here's the problem:

1) source.aspx calls Response.Redirect("destination.aspx#summary");
2) Button click in destination.aspx calls Response.Redirect("source.aspx");
3) Actually redirected back to destination.aspx
4) Another button click in destination.aspx again calls Response.Redirect("source.aspx");
5) Now I'm finally redirected back to source.aspx.

Does anyone know why this happens and how I force it to return to source.aspx on the first redirect?  I tried Response.Clear() to no avail.   hmmm.

Thanks in advance!
-Aaron

0
Comment
Question by:AaronReams
  • 8
  • 7
  • 2
17 Comments
 
LVL 9

Expert Comment

by:hismightiness
ID: 14021462
Are there any existing or cached variables in source.aspx that would route you back to destination.aspx on the first button click?
0
 
LVL 8

Author Comment

by:AaronReams
ID: 14021493
I'm not sure I understand your question...  Can you explain?  Thx -Aaron
0
 
LVL 9

Expert Comment

by:hismightiness
ID: 14021677
Well, without having any code snippets, one should assume that the reason you are using a redirect in the background versus a hyperlink is to check for certain conditions, then redirect.  In order to meet those conditions, you might also be saving a variable for return in either cache, viewstate, cookie, etc.  If this variable is being unextectedly saved, it may explain why it appears that the first time the page is refreshed instead of redirected.  If you were redirected again when reaching the page you were supposed to reach, then you would see the result you are describing.  However, placing a single button on two pages and redirecting between the two via the click event works here just fine.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:hismightiness
ID: 14021687
If you want more detailed answers to your questions, it would help everyone involved if you were to include code snippets of how the redirect is reached in the codebehind of each page.
0
 
LVL 8

Author Comment

by:AaronReams
ID: 14021764
Hmmm... regarding the background redirect vs. hyperlink...

Basically I have an account summary page with several different edit buttons.  Whenever the user clicks on any of the edit buttons it redirects to the account edit page.  The user can edit his information on that page and click finish, which redirects back to the summary page.   This all works fine except for when I use #anchor in the redirect url to the edit page.  When I use that, I have to click Finish twice to return to the summary page.

I can post sample code if need be... I was just wondering if there was something small I was overlooking when using Response.Redirect with a URL that contains an #anchor reference.   Should the scenario I mentioned typically just work correctly?

Thanks your mightiness!  ;-)

Cheers,
Aaron
0
 
LVL 9

Expert Comment

by:hismightiness
ID: 14021969
If I remember correctly, in ASP the anchor in the URL did not work at all.  I have not tried or seen any docmentation on the use of it in ASP.Net apps.  That, being said there are alternative ways to do "anchor" to a position in the page.  I use a querystring here to determine whether or not to go to an anchor point (which can be sent via Redirect).

http://www.mattkruse.com/javascript/anchorposition/
0
 
LVL 26

Assisted Solution

by:Rejojohny
Rejojohny earned 400 total points
ID: 14024978
hismightiness,
>>If this variable is being unextectedly saved, it may explain why it appears that the first time the page is refreshed instead of redirected.
what do u mean?? if there is any data been flushed to the browser and a redirect statement is then executed, he will get an error and not the original page .. or the redirected page ...

Now the question is what do u want to redirect using a bookmark "#" .. if it is to focus a particular area .. u cold use javascript for that .. Formname.controlname.focus(); to focus on a particular control within the page ..
0
 
LVL 9

Expert Comment

by:hismightiness
ID: 14026453
Rejojohny, we do not know enough details about this persons project to just assume that there are not external elements like shared variables or classes saving values that may be part of a redirect condition - therefore I brough it up.  Surely, an expert of your level can understand that.

That javascript will only work on form controls (as long as they are visible), not other HTML elements.  I had to do something very similar with a Tech Support application recently and that link above was a life saver.
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 14026745
>>there are not external elements like shared variables or classes saving values
but what has these got to do with a redirect not happening ..
do u mean to say the redirect may be based on specific conditions??If so, wouldn't the same condition still exist when he removes the anchor statement from the redirect ...
(the user can edit his information on that page and click finish, which redirects back to the summary page.   This all works fine except for when I use #anchor in the redirect url to the edit page.  )
so shouldn't the redirect fail here too ...
>> Surely, an expert of your level can understand that.
 am sorry, but am not able to understand what you are trying to explain ..

anyway Aaron, it would ne nice if you could explain the purpose of the whole excercise .. maybe one of us can suggest an alternative ..
0
 
LVL 8

Author Comment

by:AaronReams
ID: 14029504
Thanks for both of your responses.

Here are more details on my current project.

The user can view their account information all condensed onto one page that I call a summary page.  The summary page has edit buttons near each different piece of information (details, billing, shipping, etc).

I have one other long page where they can edit the actual information.   I call this my edit page.  The edit page has a bunch of textboxes and several finish buttons all of which redirect back to the summary page (after I store the info in my db of course).  Currently I don't perform any validation on the data.  I just store it in the DB and redirect back to the summary page.

So, on the summary page if you click the edit button near the details section it does a Response.Redirect("edit.aspx");  That takes you to the edit page where I can edit some details and hit any of the finish buttons.  In the finish button handler I save info to the DB and call Response.Redirect("summary.aspx").  That works correctly (I'm taken back to summary page).

However, on the summary page if I click the edit button near say billing I do a Response.Redirect("edit.aspx#billing");  That takes me to the edit page correctly scrolled to the billing section. But, when I click the finish button and call Response.Redirect("summary.aspx"); I actually end up back at "edit.aspx" (without the #billing).  If I hit Finish again then I'm taken back to "summary.aspx".

So, is there a better way to do this?  Do I need to use Javascript?

Thanks for all the help!!

Aaron




0
 
LVL 9

Accepted Solution

by:
hismightiness earned 1600 total points
ID: 14029836
My link above to the Matt Kruse site is a way of doing this via JavaScript, but if you had form fields, it is much easier.  You could do like rejojohny mentioned.  You can dynamically do this through the use of a querystring - which WILL work.  You would want the querystring value to either match the form field name (better), or have a coded way to tell what the value means.

So, your first page's button would direct you to the edit page:  edit.aspx?type=billing

In your edit page, you might have a field called billing.  In your codebehind, you would do something like this (in your page_load or called from the page_load):

            Dim strField As String = Request.Params("type")
            If strField <> "" then
                        Dim strFocusScript As String = "<script language=""javascript"" type=""text/javascript"">" & _
                            "document.getElementById('" & strField & "').focus();" & _
                            "</script>"
                        If Not Page.IsStartupScriptRegistered("FocusField") Then
                            Page.RegisterStartupScript("FocusField", strFocusScript)
                        End If
            End If

There are better JS ways that are more browser friendly, but this is quick and dirty...
0
 
LVL 8

Author Comment

by:AaronReams
ID: 14030085
Sweet!  That looks like what I need, so I'll give it a shot and get back to you later today.  Many thanx! -A.R.
0
 
LVL 8

Author Comment

by:AaronReams
ID: 14118211
Thanks for the answer.  It worked but the only problem is that it doesn't scroll the control with focus to the top of the viewable browser area.  Do you have suggestions on how to get the control with focus to align with the top of the browser window?

Cheers,
Aaron
0
 
LVL 9

Expert Comment

by:hismightiness
ID: 14120732
If it is not aligning with the top of the window, the problem more likely is that there is not enough of the page below the control to continue scrolling the page.  A solution would be to add extra line breaks at the bottom of the page.
0
 
LVL 8

Author Comment

by:AaronReams
ID: 14124722
There is definitely enough room at the bottom of the page because this was all scrolling to the correct position when I used #anchors.  And it is scrolling to the correct control, but it scrolls the minimum amount to show the control (i.e. bottom of the page).  The desirable thing would be to have the control at the top of page.  If you can think of something offhand that'd be great, but if not thanks for the help and I'll post another question to EE later.  Cheers mate -Aaron
0
 
LVL 9

Expert Comment

by:hismightiness
ID: 14124796
Oh, I see... You are just focusing now...  Well, the way I got around this was through the use of the Tutorial example here:

http://www.mattkruse.com/javascript/anchorposition/

The exact position that the page is scrolled to is dependant on the brower you are using, but for the most part you will see the behavior you have described unless some additional JavaScript is used to enhance the focus() function (like in the link above).
0
 
LVL 8

Author Comment

by:AaronReams
ID: 14124899
Great, many thanks! -A.R
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month17 days, 21 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question