Solved

Button Click after Page Load?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to dynamically set the form action using jQuery.

867 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

22 Experts available now in Live!

Get 1:1 Help Now