OnFocus event missing from ASP.Net 2.0?

Posted on 2006-04-07
Last Modified: 2010-05-18
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
Question by:ShaymusBane2
    LVL 52

    Expert Comment

    by:Carl Tawn
    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.

    Author Comment

    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...
    LVL 52

    Accepted Solution

    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;");

    Author Comment

    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

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now