OnFocus event missing from ASP.Net 2.0?

You'd think it'd be simple to do something like this..

<asp:RadioButton ID="rbOne" runat="server" Text="Button One" GroupName="grpOne">       </asp:RadioButton>
<asp:TextBox ID="txtOne" runat="server" OnFocus="rbOne.Checked = true"></asp:TextBox>

But VS2005 is giving me all manner of grief saying the "OnFocus" attribute doesn't exist with textbox...or dropdownlist, or any of those other goodies!


This worked fine when I was using classic ASP!  When a user selects the item from a dropdownlist, or types in a textbox, the corresponding radio button becomes checked...then on submit, see which radio button was selected and perform the appropriate action.

Not sure if there is a way to get the same functionality doing things differently, but any alternative options would be appreciated **grin**

Oh...not sure if you have to do anything in the code behind...with Classic ASP it was all done in Javascript, client-side...but if code behind is necessary, I am more familiar with C# than VB.Net...but can try muddle my way through a translation.

Thanks in advance
Who is Participating?
Carl TawnSystems and Integration DeveloperCommented:
If you add the OnFocus event via the code-behind then you can factor in the ClientID of the control. Something like:

    txtOne.Attributes.Add("onfocus", rbOne.ClientID + ".checked=true;");
Carl TawnSystems and Integration DeveloperCommented:
It should work fine. You'll get a warning from the compiler because IntelliSense won't recognise it as being an attribute of the server control. But it is valid for client-side javascript.
ShaymusBane2Author Commented:
I'm getting a javascript error

"document.frmMain.rbOne is null or not an object"

I'm using .master pages, and I have my frmMain located there...

When I do a view source, the ID of txtOne is altered beyond recognition too...ctrl1_id_txtOne or some such...
ShaymusBane2Author Commented:
Nice!  That's what I was lookin' for **grin**

SLIGHT variation used...

txtOne.Attributes["OnFocus"] = rbOne.ClientID + ".Checked=true;";

Works perfectly though...AND it's consistent, regardless what page I'm on, or what the form name is...

Thanks for the prompt and complete response.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.