惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
Spread Privacy
Spread Privacy
I
InfoQ
V
V2EX
S
Schneier on Security
小众软件
小众软件
C
CERT Recently Published Vulnerability Notes
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
L
Lohrmann on Cybersecurity
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Attack and Defense Labs
Attack and Defense Labs
云风的 BLOG
云风的 BLOG
The Hacker News
The Hacker News
S
SegmentFault 最新的问题
C
Cybersecurity and Infrastructure Security Agency CISA
NISL@THU
NISL@THU
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
Latest news
Latest news
S
Secure Thoughts
Project Zero
Project Zero
MongoDB | Blog
MongoDB | Blog
I
Intezer
Security Latest
Security Latest
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
N
Netflix TechBlog - Medium
V2EX - 技术
V2EX - 技术
量子位
T
Threatpost
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
T
Tor Project blog
A
Arctic Wolf
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
Check Point Blog
博客园 - Franky
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
L
LINUX DO - 热门话题

博客园 - 阿斌

[导入]シャンハイ(上海) [导入]日语流行口语极短句888个(42) [导入]功能日语句型精解2 [导入]日语流行口语极短句888个(41) [导入]日语流行口语极短句888个(40) [导入]功能日语句型精解1 [导入]日语流行口语极短句888个(39) [导入]日语流行口语极短句888个(38) [导入]加班 [导入]Community Server 1.0 Source Release [导入]CommunityServer 1.1 源码及汉化文件 [导入]CnForums2.x体验 [导入]CommunityServer架构分析ppt [导入]CommunityServer实例分析——注册新用户(1) [导入]CommunityServer系统性能优化 [导入]CommunityServer 2.0 RTM 发布 [导入]将asp.net1.1的应用程序升级到asp.net2.0的一点心得 [导入]CommunityServer2.0 Beta1发布 [导入]Community Server 2.0中如何调试项目?我告诉你! [导入]关于Community Server开发日内容的安排 [导入]Community Server2.0专注细节专题Doc下载(2006-3-9更新) [导入]Community Server2.0专注细节一 邮件提醒按钮实现(上) [导入]Community Server 2.0 (SDK) 发布了 [导入]CommunityServer 2.0中Files 与 Reader 项目的授权机制
[导入]写一个给按钮、链接等控件增加快捷键的控件
阿斌 · 2008-01-04 · via 博客园 - 阿斌

肯定有这样子的需求:我们希望能用快捷键代替鼠标点击做一些事情,例如一个典型的应用就是论坛上常用的Ctrl + Enter 快捷发帖子。就以Ctrl+Enter快捷发帖子为例,实质上呢,就是通过js脚本,捕获系统的onkeyup事件,判断event.ctrlKey是否为true并且event.keyCode为13,如果满足这个条件,那么就调用按钮对象的click()方法,等同于用鼠标去点击按钮。写个简单的示例代码:

上面的脚本可以方便的给指定的按钮加上快捷键。如果现在我们希望应用到我们的服务器端控件当中去,例如Button、Linkbutton、Hyperlink等,因为控件的ID各不相同,而且所对应的快捷键也各不相同,那么我们就需要写一个控件来给他们添加快捷键了。

想想这个控件需要哪些属性?
TargetControlID:string:既然是给其他控件绑定的,那么所要绑定的目标控件ID是少不了了,根据这个控件ID,我们才能确定一个控件,才能知道它输出时的客户端ID,才能根据客户端ID来在脚本里面确定这个对象。
CtrlKey:bool:判断是否用到Ctrl组合键
ShiftKey:bool:判断是否用到Shift组合键
AltKey:bool:判断是否用到Alt组合键
KeyCode:int:和DHTML里面的event.keyCode对应的,例如Enter的keyCode是13。(注:其实这个不是很友好,因为用的时候还要找一下键盘各个按键和keyCode的对应关系,如果结合一个快捷键设置的控件就比较完美了)
Text:string:可能顺便需要一点文字说明什么的

相对来说,这是一个比较简单的用户自定义控件应用,创建一个名字为HotKey的类,继承自System.Web.UI.Controls。根据目标控件ID查找控件对象:this.cachedTargetControl = this.NamingContainer.FindControl(this.TargetControlID);
根据上面属性,我们就可以生成相应的客户端脚本了,然后在重写控件的OnPreRender事件中使用RegisterClientScriptBlock方法输出脚本。

代码相对比较简单:

最后,调用的时候就比较方便了:
和普通的自定义用户控件一样:
<asp:Button Runat="server" id="PostButton" CssClass="txt3"></asp:Button>
<cs:Hotkey runat="server" TargetControlID="PostButton" KeyCode="13" CtrlKey="True" Text="(Ctrl + Enter)"/>
完整的应用请参考CCS1.1(http://www.communityserver.cn/ )的代码。


文章来源:http://blog.joycode.com/dotey/archive/2005/10/29/66007.aspx