0

澄清:我是智利人,所以我的英语不是很完美,很抱歉拼写错误。

嗨,我正在使用 c# 中的图像。

我尝试在第一次打开页面时放置一个示例图像,我为此使用了 post back,但是当我按下一个按钮时,执行 post back 部分中的代码(这是正确的),然后执行 Button代码,但随后,它再次传递给 Page_Load 方法,并执行“不回发”部分,我不知道为什么。

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        //Is post back
    }
    else // Is not post back
    {
        //Make things only when the page is open for the first time
    }
}
4

1 回答 1

1

我通常只在 PageLoad 上使用 (!IsPostBack) 来进行初始数据加载或验证(如用户设置)。

protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
     if (userIsAdmin)
     {
       button1.Enabled = true;
     }
  }
}

您可以参考 PostBack 的解释链接https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.page.ispostback?view=netframework-4.8

于 2019-10-22T23:28:29.663 回答