3

My scenario:

After client login, my user will be redirected to the index.aspx page. Inside this page I will put one div with a gridview inside. This gridview will be showing data from one table.

My question is: How to refresh this gridview always that one record is saved on this table?

Ps.: I´ve seen a lot of examples using "server push" tecnology, comet, ajax, etc. Don´t know the best way to do that and can´t find a really simple example.

user1270384
  • 711
  • 7
  • 24
  • 53
Fabio
  • 103
  • 11

5 Answers5

2

When a new record is added to the table, the real challenge is communicating those changes to the client in real-time without polling in intervals or requiring some sort of user interaction.

You have a couple of options:

  1. Your best bet is to use a WebSocket, which enables bidirectional communication between the client and server. This is the solution I would pick.

    Here are some examples using WebSockets:


    There are a few good libraries around too that will take care of most of the leg work. A couple to check out are WebSync and PokeIn. Both products offer decent documentation and community editions that you can download for free.

    Here are some tutorials to check out:

  2. Use AJAX to poll for changes every X number of seconds. If changes are detected reload the page, otherwise do nothing.

Community
  • 1
  • 1
James Johnson
  • 45,496
  • 8
  • 73
  • 110
  • **Use AJAX to poll for changes every X number of seconds. If changes are detected reload the page, otherwise do nothing.** Can you tell please how should I calculate the changes made ? – Pankaj Apr 17 '12 at 17:36
  • @PankajGarg: What do you mean calculate the changes made? Make an AJAX call to a function that passes in the number of records displayed and returns a boolean. The method will return a row count, and if `recordsDisplayed != existingRecords`, rebind the grid with an asynchronous callback. – James Johnson Apr 17 '12 at 17:43
  • @abcdefghi: The question asks **when add new line in a specific table** and does not mention updates. However, there are a number of ways to handle updates with the given approach too. – James Johnson Apr 17 '12 at 17:48
  • It's important to note however that I'm advocating the use of `WebSockets` for this, not AJAX polling. – James Johnson Apr 17 '12 at 17:51
1

You can use setinterval javascript method. I act as a timer and use submit form to refresh the page. other method could be asp.net ajax toolkit timer control here

Adil
  • 146,340
  • 25
  • 209
  • 204
0

put your grid inside an update panel and set "Update Mode" to always

ZERO
  • 133
  • 3
  • Can you please tell, How should I refresh this gridview ? – Pankaj Apr 17 '12 at 17:15
  • When it is inside an update panel and update mode is always, every change to the grid will be capture and refresh. You don't need to do anything else I think. – ZERO Apr 17 '12 at 17:20
  • So, Can i do it without Update Panel? – Pankaj Apr 17 '12 at 17:21
  • You can refresh the page can load your grid at pageload. UpdatePanel is better since it looks smooth and easy to implement. Try it u'll like it. – ZERO Apr 17 '12 at 17:29
0

You can use Update Panel which uses AJAX under the hood. Refer to this link for a short and simple demo.

rageit
  • 3,513
  • 1
  • 26
  • 38
0

You can also use plain old html

<META HTTP-EQUIV="REFRESH"
CONTENT="15;URL=http://www.I18nGuy.com/index.html">
David Robbins
  • 9,996
  • 7
  • 51
  • 82