Solved

Button Click after Page Load?

Posted on 2011-09-12
3
414 Views
Last Modified: 2012-05-12
Dear experts,

It's kind of a general question because I'm an ASP.NET noob (I come from Winforms).

I've got a page with a datagrid and a button. And this datagrid is databound to a collection. When the button is clicked I add an object to the collection in the click event. But the click event is handled after the load event of the page. So the added object is not immediatly visible in my databound datagrid.

To solve this I ask in the page load if the button was the control that caused the post back. If so, I add the object to the collection and rebind the collection to the datagrid.

It seems to me that this is not the way to go. Because I'll have to ask for every button on the page if it caused the post back if I want stuff to display immediatly on the page.

So help an ASP.NET noob out guys. How are webdevelopers supposed to implement the aformentioned setup?
0
Comment
Question by:Labelsoft
3 Comments
 
LVL 18

Accepted Solution

by:
Cluskitt earned 250 total points
ID: 36522576
If you add it on click event, then you should see it updated. That is, there's no need to check for things on page load. Most of the controls aren't in the form by then anyway (they get inserted in later events).
What you need to do is, on the click event Sub, add the object to the collection and then databind the datagrid again. Once you do that, it will be refreshed.
0
 
LVL 5

Assisted Solution

by:zvytas
zvytas earned 250 total points
ID: 36522584
You should use IsPostback property to check that page was posted back. Your code should look something like this:

public void Page_Load(...)
{
  if (!IsPostBack)
  {
     // bind your grid
  }
}

protected void Button1_Click(...)
{
  // add new item to the grid and rebind it
}

This is how such a problem is solved in the ASP.NET world.
}
0
 
LVL 3

Author Closing Comment

by:Labelsoft
ID: 36522631
Thanks for the replies guys.

I understand now that changing the underlying collection does not trigger the datagrid to reload itself, but setting it's DataSource property again does.

This is strange to me see, because collections like BindingList in Winforms broadcast the event CollectionChanged which will cause databound controls to repaint themselves.

Anways, I applied all the suggestions you made and now it works. Awesome.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question