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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 白白

Avril lavigne - Girlfriend(mv) 圣射手双修心得 圣射手技能 Event事件详解 用Javascript实现Agent(实现右键菜单)(2) - 白白 - 博客园 用Javascript实现Agent(网页精灵)(1) 利用vml制作统计图全攻略----饼图的制作(四) 利用vml制作统计图全攻略----饼图的制作 (三) 利用vml制作统计图全攻略----饼图的制作 (二) 利用vml制作统计图全攻略----饼图的制作(一) (Tip)教你通过windows的正版验证 GML、SVG、VML的比较 AJAX七宗罪(转) AJAX(转) HTML中字符实体集 - 白白 ASCII码表 关于potentially dangerous Request.Cookies value的解决 最佳实践:string对象(string.empty与""的区别到底是什么呢?) C#来创建和读取XML文档 - 白白
ASP.NET 2.0 - Enter Key - Default Submit Button - 白白
白白 · 2006-10-30 · via 博客园 - 白白

Hi,

One of the most annoying things in developing web pages is handling the "Enter key" for form submission. Enter key has been the favourite way users like to submit forms. Though we provide Buttons to click on, the easiest and intuitive way is that, I can enter some text, make some changes and then hit "Enter" to accomplish my submission.

"Enter" Key is handled in a little tricky way by uplevel browsers like Internet Explorer, when it comes to ASP.NET.

  • If there is a single Textbox and single button, then it becomes straight forward, the button is submitted. However, the event code doesnt get executed, though the page postsback.
  • If there are two or more, buttons, then it takes up the first button as the default button. However, it still doesnt execute the event handler but just refreshes the page.

You can supress the Enter key event using Javascript. But this would result in other undesirable effects like, any Enter key in the form i.e. within Text Area or basically where large text is entered, would be disabled.

The earlier work around was to associate a javascript function to each Button to verify the that the relevant button is submitted upon Enter key.

ASP.NET 2.0 introduces a wonderful work around for this. By simply specifying the "defaultbutton" property to the ID of the <asp:Button>, whose event you want to fire, your job is done.

The defaultbutton property can be specified at the Form level in the form tag as well as at panel level in the <asp:panel> definition tag. The form level setting is overridden when specified at the panel level, for those controls that are inside the panel.

Also, the Event Handler for the specified button, fires thereby simulating a true submit button functionality.

The following sample code contains a form and 4 panels with each of them containing different buttons. It can be noticed that for each panel, there is a default button specified which would trigger the corresponding button's event handler when "Enter" Key is pressed upon a text changed event.

<form id="form1" runat="server" defaultbutton="btn1">

<div>

<asp:TextBox ID="txt" runat="server"></asp:TextBox>

<asp:Button ID="Button5" runat="server" Text="Cancel" OnClick="Button5_Click" />

<asp:Button ID="btn1" runat="server" Text="Submit" OnClick="btn1_Click" />

<asp:Panel ID="pnl1" runat="server" defaultbutton="Button1">

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

<asp:Button ID="Button1" runat="server" Text="Button1" OnClick="Button1_Click" />

</asp:Panel>

<asp:Panel ID="Panel1" runat="server" defaultbutton="Button2">

<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>

<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>

<asp:Button ID="Button2" runat="server" Text="Button2" OnClick="Button2_Click" />

</asp:Panel>


<asp:Panel ID="Panel2" runat="server" defaultbutton="Button3">


<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>

<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>

<asp:Button ID="Button3" runat="server" Text="Button3" OnClick="Button3_Click" />

</asp:Panel>

<asp:Panel ID="Panel3" runat="server" defaultbutton="Button4">

<asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>

<asp:TextBox ID="TextBox8" runat="server"></asp:TextBox>

<asp:Button ID="Button4" runat="server" Text="Button4" OnClick="Button4_Click" />

</asp:Panel>

</div>

</form>

The corresponding, sample events for the button clicks are


protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(Button1.Text);
}


protected void Button2_Click(object sender, EventArgs e)
{
Response.Write(Button2.Text);
}


protected void Button3_Click(object sender, EventArgs e)
{
Response.Write(Button3.Text);
}


protected void Button4_Click(object sender, EventArgs e)
{
Response.Write(Button4.Text);
}


protected void btn1_Click(object sender, EventArgs e)
{
Response.Write(btn1.Text);
}


protected void Button5_Click(object sender, EventArgs e)
{
Response.Write(Button5.Text);
}

Once we execute the above functionality, we can notice, the corresponding Buttons' text are displayed when the Enter key is pressed from within a panel and at the form level, it fires the btn1 Button's event.

Thanks.