views:

31

answers:

2

I am trying to add a client side event to a dropdownlist and I need to access the currently selected Text. I have tried:

ddl_tech.Attributes.Add("onclick", "document.getElementById('" + chk_techreview.ClientID + "').disabled = this.options[this.selectedIndex].text.Equals(' UNASSIGNED');");

and

ddl_tech.Attributes.Add("onclick", "document.getElementById('" + chk_techreview.ClientID + "').disabled = this.text.Equals(' UNASSIGNED');");

Both of which give me runtime errors when the event is fired.

Whats the correct way to access this text property client side?

I tried this but it does not enable the checkbox...

ddl_tech.Attributes.Add("onchange", "document.getElementById('" + chk_techreview.ClientID + "').disabled = this.options[this.selectedIndex].text == ' UNASSIGNED';");

ANSWER:

Well, along with having to use == rather than .Equals, when you set a checkbox.enabled = false on the server side it raps the checkbox in tags and sets it to disabled=true; therefore you must set BOTH the checkbox.disabled = false and checkbox.parentElement.disabled = false; on the client side to enable the checkbox!

The solution:

ddl_tech.Attributes.Add("onchange", "document.getElementById('" + chk_techreview.ClientID + "').parentElement.disabled = (this.options[this.selectedIndex].text == 'UNASSIGNED'); document.getElementById('" + chk_techreview.ClientID + "').disabled = (this.options[this.selectedIndex].text == 'UNASSIGNED');");

Thanks for the help!

+1  A: 

I believe it should be:

ddl_tech.Attributes.Add("onclick", "var s = document.getElementById('" + chk_techreview.ClientID + "'); s.disabled = (s.selectedIndex == -1 || s.options[s.selectedIndex].text == ' UNASSIGNED ');");
Torbjörn Hansson
+3  A: 

There is no .Equals() on a string in JavaScript, instead use the === operator, like this:

ddl_tech.Attributes.Add("onchange", "document.getElementById('chk_techreview').disabled = ( this.options[this.selectedIndex].text === 'UNASSIGNED');");

You can give it a try here, I also changed the event to onchange since that's probably more of what you're after. Also, depending on your option it may just be 'UNASSIGNED' rather than ' UNASSIGNED'.

Nick Craver
ya i think the onchange should work better. It is ' Unassigned' though, it's a cheap way to make it apear at the top of a sorted list. Not the best way to get it at the top, but it works everywhere (SQL, asp etc.) Thanks!
kralco626
Tried it but it does not seem to work. I posted what i tried in my question...
kralco626
@kralco626 - You're missing a set of parenthesis, also check `' UNASSIGNED'` vs `'UNASSIGNED'` (without the space).
Nick Craver
@Nick - I tried onchange and onclick. I also tried just saying ".disabled = false;" and this did not work. So either the envent is not firing or something. It's not the evaulation of the text value...
kralco626
@Nick - I tried this and it didn't work: ddl_tech.Attributes.Add("onclick", "document.getElementById('" + chk_techreview.ClientID + "').disabled = false;");
kralco626
@kralco626 - When you view the page, what's actually getting rendered, what does the `<select>` tag look like?
Nick Craver
I'm working on it. When I run it locally it just crashes when i go to view source. I have to run it up on the server, it's going to take a few minutes.
kralco626
@Nick - don't have the source yet but, i did some testing and if instead of enabling a checkbox I enable a dropdownlist it works. It seems as though this may not be the correct way to enable a checkbox? Or something because if I just change "chk_techreview" to a disabled dropdownlist in the grdiview it become enabled! ???
kralco626
@Nick - figured it out. When you set Enabled = false sever side for a checkbox it raps it in <span> tags and sets it's disabled property to true also on the client side. You must set disabled = true for both the checkbox and it's parent.
kralco626