Solved

Button Click after Page Load?

Posted on 2011-09-12
3
412 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Read about why website design really matters in today's demanding market.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now