1

Say I have a simple form with only two text boxes and a button. The text boxes have AutoPostBack set to false. Say put some code inside of the TextChanged event of those to write to the response stream saying which TextChanged event was fired after a button is clicked and the form is submitted to the web server. Is there a way to control the firing order of cached events?

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

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

        }

        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            Response.Write("Text box 1 event fire");
        }

        protected void TextBox2_TextChanged(object sender, EventArgs e)
        {
            Response.Write("Text box 2 event fired");
        }
        //dummy button click handler here
    }
}

With that code and nothing else on the form it doesn't matter which text box is changed first, they fire in the same order. Is the firing order of cached events determined by its place in the html?

4

1 回答 1

2

根据微软的说法,你不应该依赖特定的事件顺序。但一般情况下,事件将按页面标记内的控件位置顺序触发。

所以如果你的布局是

[TextBox1]

[TextBox2]

[Button1]

事件将按顺序触发:

Page_Load

TextBox1_Changed

TextBox2_Changed

Button1_Click

您可以尝试通过重新排列页面上的控件来更改事件的顺序。

于 2013-06-04T00:33:38.673 回答