简体   繁体   English

C#方法的作用

[英]C# what the methods do

 protected void Page_Load(object sender, EventArgs e)
 {       
    Panel1.Visible = True;
    Panel2.Visible = false;
    LoadQuestion(); // auto choose question from database and add into Panel1
    LoadQuestion1();  // auto choose question from database and add into Panel2                         
  }

When I start my program, my form automatically loads questions into my textbox and radio button list. 当我启动程序时,我的表单会自动将问题加载到我的文本框和单选按钮列表中。 I click link button2 to make my Panel1 visible = false and Panel2 visible = true to continue answering question. 我单击链接按钮2使我的Panel1 visible = false ,Panel2 visible = true继续回答问题。 But after I clicked the link button 2 or 1, it will go back to the Page_Load() method and causes my questions keep on changing. 但是在我点击链接按钮2或1后,它将返回到Page_Load()方法并导致我的问题不断变化。

You should check if it's a postback. 您应该检查它是否是回发。 You want to execute this only on first load. 您只想在第一次加载时执行此操作。

protected void Page_Load(object sender, EventArgs e)
{       
   if(!IsPostBack) {
      Panel1.Visible = True;
      Panel2.Visible = false;
      LoadQuestion(); // auto choose question from database and add into Panel1
      LoadQuestion1();  // auto choose question from database and add into Panel2 
   }                      
}

Source 资源

Try: 尝试:

 protected void Page_Load(object sender, EventArgs e)
 {       
    if (!IsPostBack)
    {
        Panel1.Visible = True;
        Panel2.Visible = false;
        LoadQuestion(); // auto choose question from database and add into Panel1
        LoadQuestion1();  // auto choose question from database and add into Panel2   
    }
 }

This is because the Load event happens every time the server processes a request to your page. 这是因为每次服务器处理对页面的请求时都会发生Load事件。

There are two kinds of request: initial page load (when you go to the URL) and postback (when you click a button). 有两种请求:初始页面加载(当您转到URL时)和回发(当您单击按钮时)。 What you do in your Page_Load method is kinda initialization, so it should be done only initially, but not during the postback. 你在Page_Load方法中所做的是有点初始化,所以它应该只在最初完成,而不是在回发期间完成。

protected void Page_Load(object sender, EventArgs e)
{       
    if( !IsPostBack )
    {
        // The code here is called only once - during the initial load of the page
    }

    // While the code here is called every time the server processes a request
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#中的“扩展方法”是什么意思? - What do you mean by “extension methods” in C#? 与 C# 中的方法相关的“调用者”是什么意思? - What do people mean by "caller" in relevance to methods in C#? C#中的匿名方法是什么? - What are anonymous methods in C#? 尝试做方法 C# - Try to do methods C# 单元测试模拟控制器,C#我需要模拟HTTPContext吗? 我要嘲笑什么方法? - Unit Test Mock Controller, C# Do I need to Mock HTTPContext? What methods do I mock? C#中的方法使用什么日志系统? - What logging system to use for methods in C#? 这两种C#方法之间有什么区别 - What is difference between this two C# methods 有没有什么Ruby Mixins可以做什么C#pseudo Mixins(接口+扩展方法)不能? - Is there anything Ruby Mixins can do what C# pseudo Mixins (Interface + Extension Methods) can't? C#如何知道要基于与datagridview一起使用的接口的类需要重新实现哪些方法? - C# How do I know what methods need to be reimplemented for a class based on a interface that is to be used with a datagridview? 你喜欢用什么成语(如果有的话)将“this”参数命名为C#中的扩展方法,为什么? - What idiom (if any) do you prefer for naming the “this” parameter to extension methods in C#, and why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM