Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Redirect from OnClick of a button 1

Status
Not open for further replies.

wingpeople

Programmer
Oct 18, 2002
24
US
I must be having a syntax problem. I want to redirect when the user clicks a button. I have a variable, MyRedirect, which has been assigned a value -- for this purpose, let's say:

MyRedirect = "MyOtherPage.asp?ID=135

I include this line to define my button:

<button type=&quot;button&quot; name=&quot;btnChange&quot; onClick=&quot;<%Response.Redirect(MyRedirect)%>&quot;>Change</button>

Problem is, as soon as this page loads, it redirects to MyOtherPage and never displays the original page or even gives the user a chance to click btnChange.

I've also tried this syntax:

<button type=&quot;button&quot; name=&quot;btnChange&quot; onClick=&quot;Response.Redirect(MyRedirect)&quot;>Change</button>

Now I get to see my page & click the button, but get an error 'Response is undefined'.

Help!

And one more thing (since I'm very new at this) -- if I DO get this to work using Response.Redirect, will this even work on browsers other than IE? I'm still not clear on the client/server model.
 
<form method=&quot;post&quot; action=&quot;MyOtherPage.asp&quot;>
<input type=&quot;hidden&quot; name=&quot;id&quot; value=&quot;135&quot;>
<input type=&quot;button&quot; name=&quot;btnChange&quot; value=&quot;Change&quot;>
</form>

this will do exactly the same i think
 
try this
<button type=&quot;button&quot; onClick=&quot;document.location='<%=MyRedirect%>'&quot;>Change</button>
A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
To onpnt: document.location worked great.
So, does this mean I should avoid using Response.Redirect if I want this to work properly on non-IE browsers?
 
in this case it does not matter what browser it is (and any other case dealing with ASP) due to ASP being read before the html and or any client side scripting is.
By placing the location you want to redirect to in the variable and then simple writing that variable into the onClick event is the only way you will be able to have these two (client and server side) scripts interact.

document.location will work both NN and IE

example of what I mean.
if you did this
<input type=&quot;button&quot; onClick=&quot;<%response.redirect &quot;page.asp&quot;%>&quot;>
in this case sense the ASP code was read on loading the page the value of the onClick is equal to nothing or &quot;&quot;
hence no action is performed And even more so the page may be redirected as the code tells it to do.

but when you do this
<%
dim location
location = &quot;page.asp&quot;
%>
<input type=&quot;button onClick=&quot;document.location='<%=location%>'&quot;>
now when the page is loaded the ASP code is read and populates the onClick value with page.asp and any other place that you call the variable to be written
so the actual output to the browser is
<input type=&quot;button onClick=&quot;document.location=page.asp&quot;>

hope that helps out
and thanks for the star [thumbsup2]
A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top