开发者

asp.net: How to get a button to affect the page contents

开发者 https://www.devze.com 2022-12-09 15:05 出处:网络
In Page_Load I populate an asp:Table with a grid of images. I have a button that when pressed I would like it to repopulate the page with different images.

In Page_Load I populate an asp:Table with a grid of images. I have a button that when pressed I would like it to repopulate the page with different images.

However it appears that when 开发者_运维问答the button is pressed Page_Load is called again, followed by the script specified by the button. I thought that I could simply set a variable in the button script which is checked during Page_Load, but this will not work.

What is the most asp.netish way to approach this? Should I be populating my table somewhere other than in Page_Load, or should my button be doing something different?


Your button event gets called after page load. As such, you should put your button code in there.

I'm not terribly sure why you'd try to stuff all of your event code into Page_Load, but it's best to keep it separated.

GOOD

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);
}

BAD

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);
    }
  }
}


As rick said, it's all a matter of understanding the postback.

page_load gets fired every time the page is refreshed. however in many cases, you only want certain things to happen on the first time a page is loaded. in your case, you want the default images to load. by putting all 'one time' events for a page load in a

if (!Page.IsPostback )
{
}

it will only fire on the first time the page is loaded. this is what you want to load your first set of images.

Then in your button click event (which triggers a postback), the code within the if statement will not execute again. and you can load your second set of images in your button's event handler.


That button you're using should call a method in your code behind,so you can know that the button is was clicked, ex:

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"/>

Actually I understand what your problem is now, seems like you just have values being set in your page load with no condition check in you page load, so every time you have a postback it refreshes the page to original state, the reason for that is because everytime you trigger a refresh(postback) on the page, the pageload method is invoked, so you need to set original setting in your page load,but have them in the condition, as if(!Page.Postback) which gets triggered the first time you visit this page. Which means this is where your defaults go and if(Page.Postback) is where your always true things should go. ex:

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
   }
}


A PostBack happend when the page is reload. The first page load, Page.IsPostBack has value false. When an event happend, Page.IsPostBack has value true. So doing the thing like this will definitely works

void Page_Load()
{
if (!Page.IsPostBack)
{
//do your first time binding data
}


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

EDIT: Your state change events are not going to fire correctly if you re-bind controls(ie:DropDownList) data on every postback.

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号