Butten Event not fired ASP.NET 2.0 VS 2008

M

Michael Ahrens

Hi All

I have a problem with a webform with 2 Textboxes and a Button. TextBox1 has
a text changed event with autopostback true. Textbox2 has also a text
changed event with autopostback false. When the focus is set to TextBox1 and
i hit enter i get only the pageload event. When i set the focus on TextBox2
i get the pageload event and the button clicked event.

why don't i get the button clicked event, when the focus is set to
TextBox1?? It should also fire the button clicked event, because this button
is the default button of the page...

How to solve this problem??

thanks for your help

code below

ASPX:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="WebApplication2._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server" defaultbutton="Button1"
defaultfocus="TextBox1">
<div>

<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"
ontextchanged="TextBox1_TextChanged"></asp:TextBox>
<br />

<asp:TextBox ID="TextBox2" runat="server"
ontextchanged="TextBox2_TextChanged"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button1_Click" />

</div>
</form>
</body>
</html>

CodeBehind:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{

}

protected void TextBox1_TextChanged(object sender, EventArgs e)
{

}

protected void TextBox2_TextChanged(object sender, EventArgs e)
{

}
}
}
 
A

Allen Chen [MSFT]

Hi Michael,

The cause of this behavior is, if we set AutoPostBack to true the TextBox
will render the following HTML:

<input name="TextBox1" type="text" value="sdfa"
onchange="javascript:setTimeout('__doPostBack(\'TextBox1\',\'\')', 0)"
onkeypress="if (WebForm_TextBoxKeyHandler(event) == false) return false;"
id="TextBox1" />

We can see it hooks the onchange event to post back data. Since it happens
before the click of the button the browser will post data immediately
instead of waiting for the button click. At server side, ASP.NET therefore
cannot know the button is clicked and trigger the server side click event.

If you want the button to behave normally and still want to have some kind
of auto postback support for TextBox you can consider writing JavaScript on
your own instead of using the default rendered JavaScript code. Below is a
sample that can achieve the requirement.

aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="RequiredFieldValidator_Internal_lvl_300.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script type="text/javascript">
function TestChange(obj){

setTimeout("Check('" + obj.id + "')", 10);
}

function Check(id) {

if (window.pendingelement != null) {
if (window.pendingelement.type != "submit") {
__doPostBack(id, '');
}

}
else {
setTimeout("Check('" + id + "')", 10);
}

}
function Test() {
var elem = document.getElementById('form1').elements;
for (var i = 0; i < elem.length; i++) {
if (elem.tagName == "INPUT") {
elem.onfocus = Set;
}
}
}

function Set()
{

window.pendingelement = this;

}
</script>

</head>
<body onload="Test()" onfocus="Set()">
<form id="form1" runat="server" defaultbutton="Button1"
defaultfocus="TextBox1">
<div>

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

<asp:TextBox ID="TextBox2" runat="server"
ontextchanged="TextBox2_TextChanged"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button1_Click" />

</div>
</form>

</body>
</html>

aspx.cs:

public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.TextBox1.Attributes.Add("onchange", "TestChange(this)");

}

protected void Button1_Click(object sender, EventArgs e)
{

}

protected void TextBox1_TextChanged(object sender, EventArgs e)
{

}

protected void TextBox2_TextChanged(object sender, EventArgs e)
{

}

}
Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Michael Ahrens

Thanks Allen! You saved my day!

Regards
Michael

Allen Chen said:
Hi Michael,

The cause of this behavior is, if we set AutoPostBack to true the TextBox
will render the following HTML:

<input name="TextBox1" type="text" value="sdfa"
onchange="javascript:setTimeout('__doPostBack(\'TextBox1\',\'\')', 0)"
onkeypress="if (WebForm_TextBoxKeyHandler(event) == false) return false;"
id="TextBox1" />

We can see it hooks the onchange event to post back data. Since it happens
before the click of the button the browser will post data immediately
instead of waiting for the button click. At server side, ASP.NET therefore
cannot know the button is clicked and trigger the server side click event.

If you want the button to behave normally and still want to have some kind
of auto postback support for TextBox you can consider writing JavaScript
on
your own instead of using the default rendered JavaScript code. Below is a
sample that can achieve the requirement.

aspx:

<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="WebForm1.aspx.cs"
Inherits="RequiredFieldValidator_Internal_lvl_300.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script type="text/javascript">
function TestChange(obj){

setTimeout("Check('" + obj.id + "')", 10);
}

function Check(id) {

if (window.pendingelement != null) {
if (window.pendingelement.type != "submit") {
__doPostBack(id, '');
}

}
else {
setTimeout("Check('" + id + "')", 10);
}

}
function Test() {
var elem = document.getElementById('form1').elements;
for (var i = 0; i < elem.length; i++) {
if (elem.tagName == "INPUT") {
elem.onfocus = Set;
}
}
}

function Set()
{

window.pendingelement = this;

}
</script>

</head>
<body onload="Test()" onfocus="Set()">
<form id="form1" runat="server" defaultbutton="Button1"
defaultfocus="TextBox1">
<div>

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

<asp:TextBox ID="TextBox2" runat="server"
ontextchanged="TextBox2_TextChanged"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button1_Click" />

</div>
</form>

</body>
</html>

aspx.cs:

public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.TextBox1.Attributes.Add("onchange", "TestChange(this)");

}

protected void Button1_Click(object sender, EventArgs e)
{

}

protected void TextBox1_TextChanged(object sender, EventArgs e)
{

}

protected void TextBox2_TextChanged(object sender, EventArgs e)
{

}

}
Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support
Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top