This is a mirror of official site: http://jasper-net.blogspot.com/

Automatic postback does not fire for TextBox control

| Monday, November 1, 2010
I recently come across an interesting request which read like this:

A simple webapplication with one Textbox and one label. The Textbox has Autopostback=”true” and has an event listener attached to the TextChanged event. In the TextChanged event the label’s text is set to the text in the textbox. A javascript function “formats” the value of the textbox on the keyUp event.   The ”problem” The postback is not fired for a textbox if a javascript function sets the value of the textbox in the keyDown or keyUp event. This only occurs in IE (IE 8, IE 7, FF 3.6.8, Opera 10.6 and Chrome 5 has been tested). No javascript errors or any kind of error message, the postback is just not being fired.

And here’s a sample page to reproduce the problem:

<%@ Page Language="vb" AutoEventWireup="false" ValidateRequest="false" EnableEventValidation="false" EnableViewStateMac="false" %>

<!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" language="javascript">
       function FormatTextBox(obj) {
           //This is just a dummy function that adds $ as the first character.
           var value = obj.value

           if (value.length > 0 && value.charAt(0) != '$')
               value = '$' + value;

           obj.value = value; // This line causes the autopostback not to fire. This only happens if the value of the input
                              // is set in the keyup or keydown event and only in IE.
       }
      </script>

      <script language="vbscript" runat="server">
            'Just to have something happening during the autopostback.
            Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
                   Label1.Text = TextBox1.Text
            End Sub
      </script>

</head>
<body>


Read more: Never doubt thy debugger

Posted via email from .NET Info

0 comments: