Butten Event not fired ASP.NET 2.0 VS 2008

Discussion in 'ASP .Net Web Controls' started by Michael Ahrens, Mar 27, 2009.

  1. 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)
    {

    }
    }
    }
    Michael Ahrens, Mar 27, 2009
    #1
    1. Advertising

  2. 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:
    .

    ==================================================
    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.
    Allen Chen [MSFT], Mar 30, 2009
    #2
    1. Advertising

  3. Thanks Allen! You saved my day!

    Regards
    Michael

    "Allen Chen [MSFT]" <> wrote in message
    news:...
    > 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:
    > .
    >
    > ==================================================
    > 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.
    >
    >
    >
    Michael Ahrens, Apr 2, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jorge
    Replies:
    2
    Views:
    1,244
    jorge
    May 26, 2004
  2. jorge
    Replies:
    1
    Views:
    398
    Teemu Keiski
    May 28, 2004
  3. a serr

    Inherit from Table, net Catching child butten event

    a serr, Apr 22, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    134
    a serr
    Apr 22, 2005
  4. jorge
    Replies:
    0
    Views:
    307
    jorge
    May 25, 2004
  5. jorge
    Replies:
    0
    Views:
    238
    jorge
    May 25, 2004
Loading...

Share This Page