What Am I Doing Wrong?

J

Joey

I am attempting to hook the onChange event for a dropdown list with
javascript so that I can do some stuff and then initiate a postback
with my code. I have not yet learned about how to do postbacks with
javascript code...I have always relied on the dropdown list's
AutoPostBack=true property to do it for me.

I am basically trying to intercept the process and put my own code
in...I want to handle the onchange event clientside and then continue
on with the postback after (from my code). To do this, I changed the
dropdown list's AutoPostBack property to false, added an onChange
attribute to it, and then linked in the code below. And now, when I
select something, the alert box shows, but the page doesn't post back.
After I acknowledge the alert box, it just throws an "object expected"
javascript error.

code:

function CheckAndThenPostBack()
{

switch(document.all['ddlMyList'][document.all['ddlMyList'].selectedIndex].value)
{
case '1':
window.alert('1');
break;
case '2':
window.alert('2');
break;
case '3':
window.alert('3');
break;
default:
break;
}
__doPostBack('ddlMyList','')
}

I copied the "__doPostBack('ddlMyList','')" line straight from the page
source, after it had been displayed earlier, when the dropdown list had
AutoPostBack=true.

What am I doing wrong?
 
J

Joey

I think this error is being caused becuase the "__doPostBack()"
function is out of scope in my code file.

If this is true, how do I fix this?
 
D

Dan

Hey man

The post back property just makes it auto post back to the server, in other
words submit the form...i think it actually does this by generating the
javascript for it but dont quote me on that

Anyway all you need to do after your javascript for the on change is use
javascript to submit the form (as you know aspx puts it all in one form tag
so submit that :) )

So now it will do your client side change and then immediately post to the
server which will handle it like any normal postback i believe.

The only thing i am not sure of is if aspx pages add other info to the
postback stream which is picked up in the backgroud, but give that a go and
see how you get on. Let me know if it works.
 
J

Joey

I got it. I copied in a __doPostBack code block from a page source and
took out the eventtarget and eventargument stuff (that caused an error
for some reason). I just left the submit part, like Eliyahu said. It
worked great.

THANKS!
 
D

Dan

Yep, which posted the form as usual from javascript. Not sure your
understanding the differences between client side and server side tho, read
some of your other posts and they are all due to this same lack of
knowledge.

Just remember, everything server side happens on the server itself, and
anything in javascript is executed client side. So when using javascript you
can't refer to any server controls directly, only once they have been
rendered so via their client ids not their control ids etc Hope that makes
sense
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top