0

i have created a page in asp.net. it is accessing the data from database. what i want is whenever i change the data in the database, the changes should be automatically reflected on the page without the need of user to press refresh button.can someone tell me how to do it. can it be done using java script.

user1852933
  • 117
  • 1
  • 3
  • 11

3 Answers3

1

You can use web sockets to send data to the connected clients or you can use AJAX, jQuery ajax to pool the server for database changes or you can use asp.net ajax timer control. Using asp.net ajax timer would be more straight forward then others.

Using asp.net Timer

Html

<asp:ScriptManager ID="ScriptManager1" runat="server" />
    <asp:Timer runat="server" id="UpdateTimer" interval="5000" ontick="UpdateTimer_Tick" />
    <asp:UpdatePanel runat="server" id="TimedPanel" updatemode="Conditional">
        <Triggers>
            <asp:AsyncPostBackTrigger controlid="UpdateTimer" eventname="Tick" />
        </Triggers>
        <ContentTemplate>
            <asp:Label runat="server" id="DateStampLabel" />
        </ContentTemplate>
</asp:UpdatePanel>

Code behind (server side code)

protected void UpdateTimer_Tick(object sender, EventArgs e)
{
   // Update the ui with database changes
}
Adil
  • 146,340
  • 25
  • 209
  • 204
  • can you please give me some example – user1852933 Jan 02 '13 at 10:09
  • First you have to decide what to choose among the possible methods, you can use timer of asp.net ajax it would be more straight forward to start with, http://ajax.net-tutorials.com/controls/timer-control/ – Adil Jan 02 '13 at 10:13
1

Read about Long polling and Comet programming

Vladimir Gordienko
  • 3,260
  • 3
  • 18
  • 25
1

For a web page the best method would be polling a page asynchronously using javascript/jQuery

javascript: setTimeout - http://www.w3schools.com/js/js_timing.asp

jquert: $.ajax - http://api.jquery.com/jQuery.ajax/

  1. Create a function that loops at a specified interval - function CheckServer(seconds)
  2. Create a page that tells there is an update on the server - Updated.aspx
  3. Use $.ajax to call Updated.aspx in function CheckServer(seconds) and check for updates.
  4. When there is an update run the code to refresh the necessary parts on page

You can find the example as a VS2012 ASP.NET Project here:

https://docs.google.com/file/d/0B9m3YXHeSTDOakxVbk50MHhhTkE/edit

There are 3 important files:

  1. UserPage.aspx - Where the user(s) will see the updates.
  2. MakeUpdatePage.aspx - Where the Admin makes the updates.
  3. Instead of checking updates vie aspx pages I created web services in services.asmx which the manage updates and data exchange.

The code is not perfect, just to give you the gist of the story but it works. I tested it on a 2 user, 1 admin scenario.

This solution is a bit complicated as I also introduced you to Web Services. And not the default ones. These are little a bit customized. They have attributes that enable them to be called by client's browsers directly and return json objects. So you will also be learning what json is...

You may find a nice article about these concepts here:

http://www.codeproject.com/Articles/45275/Create-a-JSON-WebService-in-ASP-NET-2-0-with-a-jQu

erichste
  • 759
  • 4
  • 16
  • this is what i thought of doing but as i am just a beginner, i don't exactly know how to do it. can u please show me a working example or something like that – user1852933 Jan 02 '13 at 10:20
  • I created an example. It is a vs2012 asp.net project. https://docs.google.com/file/d/0B9m3YXHeSTDOakxVbk50MHhhTkE/edit – erichste Jan 02 '13 at 13:46
  • Added details to my answer... I hope it helps... – erichste Jan 02 '13 at 13:54