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

推荐订阅源

爱范儿
爱范儿
宝玉的分享
宝玉的分享
博客园 - 司徒正美
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 三生石上(FineUI控件)
IT之家
IT之家
博客园 - Franky
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
酷 壳 – CoolShell
酷 壳 – CoolShell
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Blog — PlanetScale
Blog — PlanetScale
Recent Announcements
Recent Announcements
Stack Overflow Blog
Stack Overflow Blog
T
Threatpost
aimingoo的专栏
aimingoo的专栏
博客园_首页
Know Your Adversary
Know Your Adversary
T
Threat Research - Cisco Blogs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
Security Latest
Security Latest
T
Tailwind CSS Blog
The Hacker News
The Hacker News
P
Palo Alto Networks Blog
Latest news
Latest news
G
GRAHAM CLULEY
C
CERT Recently Published Vulnerability Notes
Engineering at Meta
Engineering at Meta
A
Arctic Wolf
D
Darknet – Hacking Tools, Hacker News & Cyber Security
I
InfoQ
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 叶小钗
雷峰网
雷峰网
MongoDB | Blog
MongoDB | Blog
P
Proofpoint News Feed
S
SegmentFault 最新的问题
C
Cybersecurity and Infrastructure Security Agency CISA
Last Week in AI
Last Week in AI
罗磊的独立博客
博客园 - 聂微东
Simon Willison's Weblog
Simon Willison's Weblog
Scott Helme
Scott Helme
T
Tenable Blog
O
OpenAI News
The Cloudflare Blog
大猫的无限游戏
大猫的无限游戏
L
LINUX DO - 热门话题
美团技术团队

博客园 - Adair

位运算 Dojo Style Guide Dojo Javascript 编程规范(转) dojo1.7 加载器 ArcGIS Server GP服务使用常见错误总结 JavaScript prototype原型链介绍 网址收藏 新一轮互联网创投潮再次掀起 学习一下别人的翻译 创意空间 富翁的西瓜 8个实用保健偏方 给你健康好身体 冬季巧食生姜可提高免疫力 wz_tooltip使用备忘 如何通过饮食来提高免疫力 AjaxToolKit学习笔记 之 AjaxToolKit.dll For ASP.NET2.0 下载 AjaxToolKit学习笔记 之 Accordion AjaxToolKit学习笔记 之 TextBoxWatermark AjaxToolKit学习笔记 之 TabContainer
How to set the DefaultButton in a Page Based on ASP.NET Master Page
Adair · 2010-11-02 · via 博客园 - Adair

Challenges:

Asp.NETAs I mentioned in my previous post, I fixed the issue of hitting enter key on an ASP.NET page with a single textbox and submit button. But now new issue occurred: I could not use the Enter key on other button web controls. Whenever I hit the Enter key in the web form, the onClick event of the search button fires not the other submit buttons. I know I am using Master page and the search button is included in every page based on this master page. And the search button is in the first button web control position.

Trouble Shootings:

OK, the first thing came to my mind to resolve the issue above was to use the  new DefaultButton properties in ASP.NET 2.0 form or panel controls. But since I am using Masterpage, when I put defaultButton=”btnSubmit” in master page or put the following code in my Page_load event

Page.Form.DefaultButton = “btnSubmit”

I got the following error:

Server Error in ‘/MyApplication’ Application.
The DefaultButton of ‘form1′ must be the ID of a control of type IButtonControl.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The DefaultButton of ‘form1′ must be the ID of a control of type IButtonControl.

If you look close and think again, you will understand the problem here. Since the page is based on a Master Page, the HTML name and id attributes have been prefixed with the names of their naming container. Like, if you use ContentPlaceHolder1 in your master page, then your button will be “ctl00_ContentPlaceHolder1_TextBox1″.

Solutions:

How to resolve this problem? The trick here is to use the controls UniqueID property that returns its long client name. So, you can use the following code in your page based on Master page:

Page.Form.DefaultButton = btnSubmit.UniqueID

This way, when you press Enter, the form will post back to the server and the code in the event handler for btnSubmit.Click will fire.

The same method can be applied to defaultFocus property, of course you need to use ClientID for the textbox you need to auto-focus on.

Remember:

1) The code sample above should be put in the page which is based on Master page, not the master page itself.

2), The sample code above is in VB.net, for C# you can use the following

this.form.DefaultButton = this.yourbutton.UniqueID