代码之家  ›  专栏  ›  技术社区  ›  David Sykes

ASP.NET:如何获取按钮以影响页面内容

  •  1
  • David Sykes  · 技术社区  · 15 年前

    在页面加载中,我用图像网格填充一个asp:table。我有一个按钮,当按下时,我希望它用不同的图像重新填充页面。

    但是,当按下按钮时,会再次调用页面加载,然后是按钮指定的脚本。我想我可以简单地在按钮脚本中设置一个变量,这个变量在页面加载期间被检查,但这不起作用。

    用什么样的ASP.NET方式来处理这个问题?我应该在页面加载之外的其他地方填充我的表,还是我的按钮应该做一些不同的事情?

    5 回复  |  直到 15 年前
        1
  •  4
  •   JustLoren    15 年前

    页面加载后将调用按钮事件。因此,您应该将按钮代码放在那里。

    我不太清楚你为什么要把所有的事件代码都塞进页面加载中,但最好是分开。

    protected void Page_Load(object sender, EventArgs e)
    {
      MethodThatDynamicallyCreatesControls();
    }
    
    protected void MyImage_Click(object sender, EventArgs e)
    {
      MyImage.Property = newValue;
      MyImage2.Property = newValue2;
      PopulateTables(newValues);
    }
    

    坏的

    protected void PageLoad(object sender, EventArgs e)
    {
      if (Page.IsPostBack)
      {
        //Check to see if "MyButton" is in the Request 
        // if it is, it's the button that was clicked
        if (Request["MyButton"])
        {
          MyImage.Property = newValue;
          MyImage2.Property = newValue;
          PopulateTables(newValues);
        }
      }
    }
    
        2
  •  3
  •   David Sykes    15 年前

    正如里克所说,这一切都是为了理解回邮。

    每次刷新页面时都会激发页面加载。但是在许多情况下,您只希望在第一次加载页面时发生某些事情。在您的情况下,您希望加载默认图像。通过将页面加载的所有“一次性”事件放入

    if (!Page.IsPostback )
    {
    }
    

    它只在第一次加载页面时启动。这是您要加载第一组图像的内容。

    然后在您的button click事件(触发回发)中,if语句中的代码将不再执行。您可以在按钮的事件处理程序中加载第二组图像。

        3
  •  2
  •   TStamper    15 年前

    您使用的按钮应该调用代码隐藏中的一个方法,这样您就可以知道按钮是被单击的,例如:

    protected void Important_ButtonClicked(Object sender, EventArgs e)
    {
        //do what I want to do
    }
    
    <asp:Button id="Button1"
           Text="MakeChanges"
           OnClick="Important_ButtonClicked" 
           runat="server"/>
    

    事实上,我理解你现在的问题是什么,似乎你只是在页面加载中设置了一些值。 无条件检查 在你的页面加载 ,所以每次有一个回发时,它都会将页面刷新到原始状态,原因是每次在页面上触发刷新(回发)时,都会调用pageLoad方法,因此需要在页面加载中设置原始设置,但要使它们处于这种状态,例如 if(!Page.Postback) 当你第一次访问这个页面时就会触发它。这意味着这就是你默认的地方 if(Page.Postback) 是你永远真实的事情应该去的地方。前任:

    protected void Page_Load()
    {
       // this is where I want things to always happen whenever the page is loaded
       //for example, no matter what happens I want a certain image in the background
       if(!Page.Postback)
       {
         //set my values for the first and only time
       }
       else //hint Page.Postback
       {  
           //you can play with the page here to make necessary changes
           //but Button click method will still be invoke so thats where button click 
           //changes should be made
       }
    }
    
        4
  •  1
  •   tuanvt    15 年前

    重新加载页面时发生回发。第一页加载page.ispostback的值为false。当事件发生时,page.ispostback的值为true。 所以这样做肯定会奏效的

    void Page_Load()
    {
    if (!Page.IsPostBack)
    {
    //do your first time binding data
    }
    
        5
  •  0
  •   rick schott    15 年前

    How to: Create Event Handlers in ASP.NET Web Pages

    编辑: 如果在每个回发上重新绑定控件(即DropDownList)数据,则状态更改事件将不会正确触发。

    void Page_Load()
    {
        if (!IsPostBack)
        {
            //load your data
        }
    }