Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an asp.net application. I want to write some string in the textbox which has focus.

How to know which control has the focus in asp.net(C#) at runtime.
Posted

1 solution

You can't find out in ASP.NET(C#) as you put it because that only runs on the server side. You can find out on the client side though by adding an onfocus javascript event handler for you textbox controls like this:

C#
<asp:textbox ... onfocus="doItOnFocus(this);" ... >


Then you also need to include some javascript code:

C#
<script type="text/javascript">
    function doItOnFocus(element)
    {
        var stringToInsert = "whatever"; // do your stuff here to get some string
        var textBoxID = element.id;
        element.value = stringToInsert;
    }
</script>


When the textbox gets the focus the javascript function doItOnFocus will be called and this will be the HTML input element that corresponds to the asp:textbox element.

Still have questions? Then leave me a comment.

Regards,
Manfred
 
Share this answer
 
v4
Comments
Shining Legend 24-Jan-11 0:20am    
Thanks for your answer but I want to know which textbox has focus when the page is loaded that too from another software.
Manfred Rudolf Bihy 24-Jan-11 7:20am    
What do you mean by saying "that too from another software"? Please elaborate.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900