在下面的代码中,我想实现的是:
(1) 第一次打开网页,TextBox中的文本为"Good Morning."
(2) 单击Button,形成Postback,然后更改TextBox的文本为"Good Afternoon"可是不管怎样,TextBox的文本总是 "Good Morning.", 请各位帮我看看是什么原因。(我不想捕捉Button的Onclick事件)    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Button button1 = new Button();
            button1.Text = "Click";
            button1.ID = "button1";                        TextBox textBox1 = new TextBox();
            textBox1.Text = "Good Morning.";
            textBox1.ID = "textBox1";                        if (IsPostBack)
            {
                textBox1.Text = "Good Afternoon.";
            }            Form.Controls.Add(button1);
            Form.Controls.Add(textBox1);
        }
    }

解决方案 »

  1.   


     public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Button button1 = new Button();
                button1.Text = "Click";
                button1.ID = "button1";                        TextBox textBox1 = new TextBox();
                textBox1.ID = "textBox1";                        if (IsPostBack)
                {
                    textBox1.Text = "Good Afternoon.";
                }else{
                       textBox1.Text = "Good Morning.";
                }            Form.Controls.Add(button1);
                Form.Controls.Add(textBox1);
            }
        }
      

  2.   

    晕死,将变量声明放在函数里,那是局部变量
    把函数的内容全删掉,再在前台代码里拖控件进去,ButtonClick事件里写代码
    前台代码:
    <asp:TextBox ID="txt" Text="good morning" runat="server"/>
    <asp:Button ID="btn" Text="Test" OnClick="btn_Click" runat="server"/>后台代码:
    void btn_Click(object sender, EventArgs e)
    {
     txt.Text="good afternoon";
    }
      

  3.   


    我已经说了,我不想捕捉Button的OnClick事件。我要动态添加组件到页面中。