Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET回发问题_C#_Asp.net - Fatal编程技术网

C# ASP.NET回发问题

C# ASP.NET回发问题,c#,asp.net,C#,Asp.net,我正在学习ASP.NET,我不明白这里发生了什么 代码隐藏文件: protected void Page_Load(object sender, EventArgs e) { Label1.Text = "TextBox1.Text = " + TextBox1.Text + "<br />"; Label1.Text += "TextBox1.Forecolor = " TextBox1.ForeColor.ToString(); } protected void

我正在学习ASP.NET,我不明白这里发生了什么

代码隐藏文件:

protected void Page_Load(object sender, EventArgs e)
{
    Label1.Text = "TextBox1.Text = " + TextBox1.Text + "<br />";
    Label1.Text += "TextBox1.Forecolor = " TextBox1.ForeColor.ToString();
}

protected void Button1_Click(object sender, EventArgs e)
{
    TextBox1.Text = "Some more text";
    TextBox1.ForColor = System.Drawing.Color.Blue;
}
受保护的无效页面加载(对象发送方,事件参数e)
{
Label1.Text=“TextBox1.Text=“+TextBox1.Text+”
; Label1.Text+=“TextBox1.Forecolor=”TextBox1.Forecolor.ToString(); } 受保护的无效按钮1\u单击(对象发送者,事件参数e) { TextBox1.Text=“更多文本”; TextBox1.ForColor=System.Drawing.Color.Blue; }
基本上,所有这些都只是一个标签,告诉你文本框的颜色和文本是什么。当您点击按钮时,它会将颜色更改为蓝色,页面将重新加载

为什么当您第一次按下按钮并重新加载页面时,标签不会更新为正确的信息?您必须再次按下按钮,它才能显示文本框为红色


有人能解释这种行为吗?以及如何更改页面加载方法以解决此问题?

页面加载事件在控件事件之前处理。有关页面生命周期的说明,请参见。要修复此问题,请修改代码,以便页面加载和按钮单击处理程序调用相同的方法来设置标签值。仅当方法不是回发时才执行Page_Load

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
       SetUpLabel();
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    TextBox1.Text = "Some more text";
    TextBox1.ForeColor = System.Drawing.Color.Blue;

    SetUpLabel();
}

private void SetUpLabel()
{
    Label1.Text = "TextBox1.Text = " + TextBox1.Text + "<br />";
    Label1.Text += "TextBox1.Forecolor = " TextBox1.ForeColor.ToString();
}
受保护的无效页面加载(对象发送方,事件参数e)
{
如果(!IsPostBack)
{
SetUpLabel();
}
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
TextBox1.Text=“更多文本”;
TextBox1.ForeColor=System.Drawing.Color.Blue;
SetUpLabel();
}
私有void SetUpLabel()
{
Label1.Text=“TextBox1.Text=“+TextBox1.Text+”
; Label1.Text+=“TextBox1.Forecolor=”TextBox1.Forecolor.ToString(); }
页面加载事件在控件事件之前处理。有关页面生命周期的说明,请参见。要修复此问题,请修改代码,以便页面加载和按钮单击处理程序调用相同的方法来设置标签值。仅当方法不是回发时才执行Page_Load

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
       SetUpLabel();
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    TextBox1.Text = "Some more text";
    TextBox1.ForeColor = System.Drawing.Color.Blue;

    SetUpLabel();
}

private void SetUpLabel()
{
    Label1.Text = "TextBox1.Text = " + TextBox1.Text + "<br />";
    Label1.Text += "TextBox1.Forecolor = " TextBox1.ForeColor.ToString();
}
受保护的无效页面加载(对象发送方,事件参数e)
{
如果(!IsPostBack)
{
SetUpLabel();
}
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
TextBox1.Text=“更多文本”;
TextBox1.ForeColor=System.Drawing.Color.Blue;
SetUpLabel();
}
私有void SetUpLabel()
{
Label1.Text=“TextBox1.Text=“+TextBox1.Text+”
; Label1.Text+=“TextBox1.Forecolor=”TextBox1.Forecolor.ToString(); }
您需要在加载页面中编写代码,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
  if(!IsPostBack) //This condition allows a code to execute once when your page is load first time.
   {
    Label1.Text = "TextBox1.Text = " + TextBox1.Text + "<br />";
    Label1.Text += "TextBox1.Forecolor = " TextBox1.ForeColor.ToString();
   }
}

protected void Button1_Click(object sender, EventArgs e)
{
    TextBox1.Text = "Some more text";
    TextBox1.ForColor = System.Drawing.Color.Blue;

    Label1.Text = "TextBox1.Text = " + TextBox1.Text + "<br />";
    Label1.Text += "TextBox1.Forecolor = " TextBox1.ForeColor.ToString();

}
受保护的无效页面加载(对象发送方,事件参数e)
{
if(!IsPostBack)//此条件允许在首次加载页面时执行一次代码。
{
Label1.Text=“TextBox1.Text=“+TextBox1.Text+”
; Label1.Text+=“TextBox1.Forecolor=”TextBox1.Forecolor.ToString(); } } 受保护的无效按钮1\u单击(对象发送者,事件参数e) { TextBox1.Text=“更多文本”; TextBox1.ForColor=System.Drawing.Color.Blue; Label1.Text=“TextBox1.Text=“+TextBox1.Text+”
; Label1.Text+=“TextBox1.Forecolor=”TextBox1.Forecolor.ToString(); }
您需要在加载页面中编写代码,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
  if(!IsPostBack) //This condition allows a code to execute once when your page is load first time.
   {
    Label1.Text = "TextBox1.Text = " + TextBox1.Text + "<br />";
    Label1.Text += "TextBox1.Forecolor = " TextBox1.ForeColor.ToString();
   }
}

protected void Button1_Click(object sender, EventArgs e)
{
    TextBox1.Text = "Some more text";
    TextBox1.ForColor = System.Drawing.Color.Blue;

    Label1.Text = "TextBox1.Text = " + TextBox1.Text + "<br />";
    Label1.Text += "TextBox1.Forecolor = " TextBox1.ForeColor.ToString();

}
受保护的无效页面加载(对象发送方,事件参数e)
{
if(!IsPostBack)//此条件允许在首次加载页面时执行一次代码。
{
Label1.Text=“TextBox1.Text=“+TextBox1.Text+”
; Label1.Text+=“TextBox1.Forecolor=”TextBox1.Forecolor.ToString(); } } 受保护的无效按钮1\u单击(对象发送者,事件参数e) { TextBox1.Text=“更多文本”; TextBox1.ForColor=System.Drawing.Color.Blue; Label1.Text=“TextBox1.Text=“+TextBox1.Text+”
; Label1.Text+=“TextBox1.Forecolor=”TextBox1.Forecolor.ToString(); }
试试看,只要试试这个代码,这个代码总是有用的,不需要额外的代码

bool IsClick=false;
protected void Page_Load(object sender, EventArgs e)
{    
    Label1.Text = "TextBox1.Text = " + TextBox1.Text + "<br />";
    Label1.Text += "TextBox1.Forecolor = " TextBox1.ForeColor.ToString();
    if(IsClick)
    {
    TextBox1.Text = "Some more text";
    TextBox1.ForeColor = System.Drawing.Color.Blue;
    }
}


protected void Button1_Click(object sender, EventArgs e)
{
  IsClick=true;
}
bool IsClick=false;
受保护的无效页面加载(对象发送方、事件参数e)
{    
Label1.Text=“TextBox1.Text=“+TextBox1.Text+”
; Label1.Text+=“TextBox1.Forecolor=”TextBox1.Forecolor.ToString(); 如果(IsClick) { TextBox1.Text=“更多文本”; TextBox1.ForeColor=System.Drawing.Color.Blue; } } 受保护的无效按钮1\u单击(对象发送者,事件参数e) { IsClick=true; }

这是我的想法

试试看,只要试试这个代码,这个代码一直都是我的工作,不需要额外的代码

bool IsClick=false;
protected void Page_Load(object sender, EventArgs e)
{    
    Label1.Text = "TextBox1.Text = " + TextBox1.Text + "<br />";
    Label1.Text += "TextBox1.Forecolor = " TextBox1.ForeColor.ToString();
    if(IsClick)
    {
    TextBox1.Text = "Some more text";
    TextBox1.ForeColor = System.Drawing.Color.Blue;
    }
}


protected void Button1_Click(object sender, EventArgs e)
{
  IsClick=true;
}
bool IsClick=false;
受保护的无效页面加载(对象发送方、事件参数e)
{    
Label1.Text=“TextBox1.Text=“+TextBox1.Text+”
; Label1.Text+=“TextBox1.Forecolor=”TextBox1.Forecolor.ToString(); 如果(IsClick) { TextBox1.Text=“更多文本”; TextBox1.ForeColor=System.Drawing.Color.Blue; } } 受保护的无效按钮1\u单击(对象发送者,事件参数e) { IsClick=true; }

这是我的想法

看一看我脑海中的想法,但我想在继续之前了解一下这里发生了什么。所有页面加载代码都需要包装在if(!page.IsPostback)块中,否则它会在每次回发时执行。对,但在这种情况下这很好,因为我们正在根据文本框中的代码更改这些标签。问题是,正如@tvanfosson提到的,他们加载的顺序不允许标签在第一次按下按钮时显示文本已变为蓝色。请看一下我脑海中出现的相同想法,但在我继续之前,我想了解这里发生了什么。所有页面加载代码都需要包装在if(!page.IsPostback)块中,否则它会在每次回发时执行。对,但在这种情况下这很好,因为我们正在根据文本框中的代码更改这些标签。问题是,正如@tvanfosson所提到的,它们加载的顺序不允许标签读取文本已更改为b