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

推荐订阅源

P
Proofpoint News Feed
V
V2EX
博客园_首页
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recent Announcements
Recent Announcements
博客园 - 司徒正美
Microsoft Security Blog
Microsoft Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Latest news
Latest news
Vercel News
Vercel News
The Register - Security
The Register - Security
T
The Exploit Database - CXSecurity.com
S
Schneier on Security
N
Netflix TechBlog - Medium
WordPress大学
WordPress大学
小众软件
小众软件
L
Lohrmann on Cybersecurity
GbyAI
GbyAI
P
Privacy & Cybersecurity Law Blog
T
Tor Project blog
AWS News Blog
AWS News Blog
美团技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
K
Kaspersky official blog
B
Blog RSS Feed
G
Google Developers Blog
量子位
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
Scott Helme
Scott Helme
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
I
Intezer
雷峰网
雷峰网
Martin Fowler
Martin Fowler
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Blog — PlanetScale
Blog — PlanetScale
IT之家
IT之家
F
Full Disclosure
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 【当耐特】
The Hacker News
The Hacker News
U
Unit 42
S
SegmentFault 最新的问题
I
InfoQ
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
宝玉的分享
宝玉的分享
罗磊的独立博客
Spread Privacy
Spread Privacy
C
CERT Recently Published Vulnerability Notes

博客园 - 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