【问题标题】:Postback using Button control使用 Button 控件回发
【发布时间】:2015-10-26 19:54:50
【问题描述】:

我是初学者是 ASP.NET。我有一个小疑问。我写了一个简单的代码来打印文本框中选中的单选按钮的文本。我没有使用单选按钮的自动回发属性。代码如下

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

namespace web
{
    public partial class SAMPLE : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
        }
        protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (RadioButton1.Checked)
                 TextBox1.Text = RadioButton1.Text;
        }
        protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (RadioButton2.Checked)
                 TextBox1.Text = RadioButton2.Text;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }        
    }
}

检查单选按钮,单选按钮中的文本不会打印在文本框中,而是在点击按钮后打印。 我的疑问是在回发期间(点击按钮)只有按钮控件事件处理程序中的内容被执行,但是其他事件处理程序中的语句是如何被执行的?

【问题讨论】:

  • 发布你的aspx代码,可能你已经交换了事件

标签: c# asp.net


【解决方案1】:

原因是单选按钮更改事件是Cached Event。这些事件保存在 View State 中,以便在 Postback 事件发生时进行处理。所以在这种情况下,当您单击按钮时,会发生回发,并执行存储在 View State 中的单选按钮更改事件的缓存事件。

TextBox 控件的TextChanged 事件、DropDownList 控件的SelectedIndexChanged 事件也是缓存事件的示例。

通过将控件的AutoPostBack属性设置为true,可以将缓存的事件转换为Postback Events

【讨论】:

  • 我猜 OP 想说的是,即使没有 AutoPostBack 属性附加到单选按钮,它仍然会在单击按钮时执行。当没有附加AutoPostBack 属性时,CheckedChanged 事件怎么会在回发时执行。
  • @SuprabhatBiswal - 嘿,我想我不太清楚,已经更新了我的答案。因此,当按钮实际执行回发时,单选按钮的缓存事件将被执行。它发生在文本框、下拉列表、单选按钮等。
  • 但是,如果我们在上述情况下为 ex 的任何控件设置自动回发单选按钮,并且当我检查单选按钮时,它正在执行所有事件处理程序........那么是什么使用为其他控件定义单独的事件处理程序..?
  • @METALHEAD - 嘿,不,当您设置单选按钮列表的AutoPostBack 属性并更改单选按钮时,只会触发单选按钮的选定索引更改事件(当然还有其他页面事件),但不会执行按钮单击事件。检查页面生命周期。
猜你喜欢
  • 2016-12-27
  • 1970-01-01
  • 2011-01-19
  • 1970-01-01
  • 2018-03-06
  • 1970-01-01
  • 1970-01-01
  • 2011-04-05
  • 1970-01-01
相关资源
最近更新 更多