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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select box selected value on ajax postback

Status
Not open for further replies.

86atc250r

Programmer
Jan 17, 2008
2
US
Got a strange problem I'm hoping someone can help with.

What we have is a normal Select box populated with several items.

I have a javascript setTimeout that when triggered grabs a value off the page, passes it to a webservice - the webservice returns a value, if they match, the timeout is reset and it repeats.

If the value is different, it fires an ajax postback at the server (with the value of the selected item in the select box) and populates a div with the data returned and a new value which will be again compared to data returned by the webservice on the next setTimeout.

My problem is, in Firefox if I have the select box opened and am hovering over one of the options when it is determined a postback is required (from the webservice's returned data) - the value I'm hovering over is posted back, not the actual selected value.

So - is there a way to detect if a select box is "open" in javascript? If I could tell if the box is currently open, I could cancel either the call setTimeout or the postback itself.
 
I don't believe there is a property of the select that you can reference to see if it is open or not.

You could set a flag somewhere using the onclick method perhaps... but that's a bit clunky.

How are you extracting the value from the select?

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

Webflo
 
Well, for the webservice call, what I did was to set a variable = to the selected value in JS using the onChange event to make sure that only the onChange event would set the selected value property, like this (in the select tag, of course):

onChange="setValue(this.options[this.selectedIndex].value)"

That did correct one issue by setting it up that way that I was having with the webservice call doing the same thing. The larger problem (and the one explained in my previous post) is that I'm using asp.net's ajax framework to do the postback once the comparison is made of the value in the page and what the webservice returns.

I'm manually calling asp.net's __doPostBack function with my own custom JS after the comparison:

__doPostBack('divToBeUpdated', '');

Below is the automatically generated function by asp.net's ajax framework

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['myForm'];
if (!theForm) {
theForm = document.myForm;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
//]]>
</script>

----------

Update, as I was posting this and in looking over the __doPostBack function I've figured out that I can use the same variable I originally set to correct the webservice issue to also correct this issue.

I did have to change a little bit of the page's logic but it seems to work as expected now.

Basically, instead of setting my dropdown to autopostback="true" in asp.net, I set it to false, then I manually pass the __doPostBack function in the onChange event, with my selected value in the eventArgument argument.

Then, in the server side code, instead of pulling my dropdown list's selectedValue property, I get the eventArguement using this statement: Request.Params.Get("__EVENTARGUMENT")

Hopefully this will help some poor googler in the future :)
 
here is for checking that the SELECT is in use.

you can use the [tt]onfocus[/tt], [tt]onblur[/tt] and [tt]onchange[/tt] to set a variable.
the [tt]onfocus[/tt] says that you entered the SELECT and the [tt]onblur[/tt] and [tt]onchange[/tt] says that you left the SELECT.

JavaScript:

Code:
var sel_in_use = false;

HTML:

Code:
<select onfocus="sel_in_use = true;" onblur="sel_in_use = false;" onchange="sel_in_use = false;">
<option>One</option>
<option>Two</option>
<option>Three</option>
</select>

if it halps any..


- Lowet

[gray]Why can't all browsers parse pages the same way? It should be the Web designer who decides how to display the content, not the browser![/gray]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top