Solved

Button Click after Page Load?

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

623 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