I am going to suggest a jQuery approach and get rid of the server side timer. Using jQuery and the jQuery.Timer plugin: http://code.google.com/p/jquery-timer/ , you can make it easier, in my opinion, and it will be better on the client side.
On an aspx page, you have an element to display the output:
<span id="status" style="display:none"></span>
as well as a link to jQuery, jQuery.Timer.js, and then your code to call the C # web method.
<script src="js/jquery-1.7.1.min.js" type="text/javascript"></script> <script src="js/jquery.timer.js" type="text/javascript"></script> <script> $(function () { var timer = $.timer(getMessage, 10000); timer.play(); }) function getMessage() { $.ajax({ type: 'POST', contentType: 'application/json; charset=utf-8', url: 'message-service.asmx/getDBMessages', data: "{}", dataType: 'json', success: getMessageSuccess, error: getMessageError }) } function getMessageError(jqXHR, textStatus, errorThrown) { $('#status').html(errorThrown).show(); } function getMessageSuccess(data, textStatus, jqXHR) { $('#status').html(data.d).show(); } </script>
I would use a web service to search for db messages on the server side:
[WebService(Namespace = "http://site.com/service")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.Web.Script.Services.ScriptService] public class contact_service : System.Web.Services.WebService { [WebMethod] public string getDBMessages() { try { if (MyConnection.State == System.Data.ConnectionState.Open) { MyConnection.Close(); } MyConnection.Open(); OdbcCommand cmd = new OdbcCommand("Select message from messages where name=?", MyConnection); cmd.Parameters.Add("@email", OdbcType.VarChar, 255).Value = "human"; OdbcDataReader dr = cmd.ExecuteReader(); ArrayList values = new ArrayList(); while (dr.Read()) { string messagev = dr[0].ToString(); } MyConnection.Close(); return messagev; } catch (Exception ex) { return ex.Message; } }
Hope this makes sense. I created a web server (asmx) and created my server code to return a string. In aspx markup, I use jQuery to start the timer for 10 seconds and then an .ajax message to poll the service, get the message and display it. There is no page refresh and much less hacked from my point of view.