Javascript input.click() does not cause a Response.Redirect in asp.net

A

ahaupt

Hi all,

I have to use some javascript to "click" a search button on my aspx
page.

The server side btnSearch_Click executes prefectly, but there's a
Response.Redirect within the method, that does not get executed.

Any idea why it would execute all the code apart from the
Response.Redirect?

Kind regards,
Andre
 
P

Patrice

Not seen this behavior. As it triggers an internal excpetion perhaps
something with catching exceptions ? Seeing the minimal amount of code that
shows this behavior may help...

What if you just redirect without any other code ? If it stills doesn"t work
what if you just print out something to make sure the event handler is
called ?
 
A

ahaupt

Hi Eliyahu,

Thanks for the swift reply.

Yeah it gets executed when you click on the button with the mouse, but
not when hitting the enter key.

(I'm pretty sure there's nothing wrong with the javascript. I'll post
my code below Partice's post)

Best,
Andre
 
A

ahaupt

Hi Patrice,

Thanks for the post.

I agree. It should work.

I'm pretty sure I'm missing something somewhere, but can't seem to find
it.

Here's the code as requested.

This is the javascript:
if(window.event.keyCode == 13)
{

var inputs = document.getElementsByTagName("input");

//I could've done a shorter version of this
//But we use a 3rd party cms (sitecore), which handles our layouts
//The cms causes webcontrol ID's to be rewritten to something
//like "uc_search0:btnSearch"
//So I have to search the id for the value "btnSearch"
for(var i = 0; i < inputs.length; i++)
{

if(inputs.item(i).id.toString().indexOf("btnSearch") > -1)
{

inputs.item(i).click();
break;

}

}

}
//----------------------------------------------------------------------------------------------

And the server side code:
private void btnSearch_Click(object sender, System.EventArgs e)
{

string redirectURL = "http://www.google.com";

//...
//Some basic if statements, which might alter the URL according to
//the search criteria on the form
//...

Response.Redirect(redirectURL);

}
//----------------------------------------------------------------------------------------------

There's nothing funny in the Page_Load either.

I don't know if this behaviour might be caused by the cms. The cms uses
native aspx, so it shouldn't really be the culprit.

I'll do a quick test in plain aspx to see if it is in fact our cms.
Hope its not otherwise I've been wasting your time!

Best,
Andre
 
A

ahaupt

Eh, I just did a test in plain aspx and it worked perfectly.

I'll have a fiddle with it for another while, and let you guys know how
it turned out.

Thanks,
Andre
 
E

Eliyahu Goldin

Andre,

Does it get executed when you click the button with the mouse?

Eliyahu
 
B

Bruce Barker

your client code needs to cancel the event. the default behavior of a return
is a form submit, this will still happen, after your code does its own
submit.

-- bruce (sqlwork.com)
 
A

ahaupt

Hi Bruce,

Excellent! It works! Thanks for that.

For all those who want to know how to actually cancel the event, here
it is:

window.event.returnValue = false;

Many thanks,
Andre
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top