获得专注控制的ID [英] Get id of focussed control

查看:56
本文介绍了获得专注控制的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp.net应用程序。我想在有焦点的文本框中写一些字符串。



如何在运行时知道哪个控件在asp.net(C#)中有焦点。

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.

推荐答案

您无法在ASP.NET(C#)中找到它,因为它只能在服务器端运行。您可以在客户端找到通过为您添加onfocus javascript事件处理程序,如下所示:



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:

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





然后你还需要包含一些javascript代码:





Then you also need to include some javascript code:

<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>





当文本框获得焦点时,将调用javascript函数doItOnFocus并且这个将是对应于asp:textbox元素的HTML输入元素。



还有问题吗?然后给我留言。



问候,

Manfred



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


这篇关于获得专注控制的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆