buttons cannot be triggered at the same time, so when you clicked on Button2, only Button2_Click is called, Button1_Click is not called

解决方案 »

  1.   

    因为你按了button2 而没有按过 button1 当然str="abc" 一次都没有执行过自然就出错了。
    你可以public void Button2_Click(Object src,EventArgs e)
    {
      Button1.PerformClick();
      TextBox3.Text=str;
    }刚才我才意识到一个问题,本来我在考虑答案应该是:似乎不是only Button2_Click is called ,如果是only Button2_Click is called 则会出错因为使用了未赋值的变量“str”可当我进行实验时才发现,非局部变量就算未赋值也可以使用真是一天一进步~
      

  2.   

    你先点击一下button1,在点击一下button2,看还为空吗?
      

  3.   

    public void Button2_Click(Object src,EventArgs e)
    {
      Button1_Click(null,null);
      TextBox3.Text=str;
    }
      

  4.   

    我明白大家的意思 都怪我写的不好应该吧
    button1_click改成page_load 但是问题还是没解决啊
    因为我的代码与页面是一起写的 也就是说
    <script>
    string str;
    protected void Page_Load(Object sender, EventArgs e) 
        {str="abc";}
     public void Button2_Click(Object src,EventArgs e)
     {TextBox3.Text=str;
     }
    </script>
    不知道为什么TextBox3.Text还是显示不到“abc” 显示空。
    请问这样写str是不是全局变量啊?
      

  5.   

    if the code is like what you said here, it should worklook at your code again, make sure
    str="abc"; 
    is not in 
    if (!IsPostBack)
    {
    //...
    }
      

  6.   

    public static string str;
    这样就保证可以的,给分
      

  7.   

    肯定是你的Page_Load()没有执行
      

  8.   

    网页我不太懂,但是在FORM中,我做了一下实验.string src;//定义全局变量;
    /*
    在Form加载时给src赋值,不知道你为什么要单独再定义一个方法,就算是单独定义一个方法也是可以啊.但是复杂了点,不是吗?
    */
    private void Form1_Load(object sender, System.EventArgs e)
    {
    this.src="abc";
    }
    /*
    点击Button时就会按你要求的显示,试一试吧!
    */
    private void button1_Click(object sender, System.EventArgs e)
    {
    this.textBox1.Text=this.src;
    }
    执行时是完全没有问题的.