Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 673
  • Last Modified:

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
  • 2
  • 2
1 Solution
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...
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;");
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.


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now