Solved

Button Click after Page Load?

Posted on 2011-09-12
3
416 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to count occurrences of each item in an array.

752 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