<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule">

<channel>
	<title>techencoder &#187; JavaScript</title>
	<atom:link href="http://techencoder.com/index.php/category/programming/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://techencoder.com</link>
	<description>Technical ideas in a human readable format</description>
	<lastBuildDate>Wed, 01 Feb 2012 17:19:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>		<item>
		<title>Getting DataTables to work with ASP.Net MVC</title>
		<link>http://techencoder.com/index.php/2009/12/getting-datatables-to-work-with-asp-net-mvc/</link>
		<comments>http://techencoder.com/index.php/2009/12/getting-datatables-to-work-with-asp-net-mvc/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 07:13:12 +0000</pubDate>
		<dc:creator>r.claypool</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[ASP.Net MVC]]></category>
		<category><![CDATA[DataTables]]></category>

		<guid isPermaLink="false">http://techencoder.com/?p=1041</guid>
		<description><![CDATA[Want to get jQuery DataTables to work in an ASP.Net MVC project ??? Here is a demo project in C# that should get you started.  If I find the time, I&#8217;ll post details on how this was put together (but I think most developers will benefit more from running and stepping through the source code [...]]]></description>
			<content:encoded><![CDATA[<p>Want to get jQuery <a href="http://www.datatables.net/">DataTables</a> to work in an ASP.Net MVC project ???</p>
<p>Here is a <a href="http://techencoder.com/wp-content/uploads/2009/12/ASP.Net-MVC-and-DataTables-Demo.zip">demo project</a> in C# that should get you started.  If I find the time, I&#8217;ll post details on how this was put together (but I think most developers will benefit more from running and stepping through the source code anyway).</p>
<p>Hope this helps someone.  Happy Programming!</p>
]]></content:encoded>
			<wfw:commentRss>http://techencoder.com/index.php/2009/12/getting-datatables-to-work-with-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to run JavaScript prior to System.Web.UI.Timer OnTick</title>
		<link>http://techencoder.com/index.php/2009/01/javascript-and-timer-ontick/</link>
		<comments>http://techencoder.com/index.php/2009/01/javascript-and-timer-ontick/#comments</comments>
		<pubDate>Wed, 21 Jan 2009 07:07:34 +0000</pubDate>
		<dc:creator>r.claypool</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Add new tag]]></category>
		<category><![CDATA[StackOverflow]]></category>
		<category><![CDATA[Timer]]></category>
		<category><![CDATA[UpdatePanel]]></category>

		<guid isPermaLink="false">http://techencoder.com/?p=156</guid>
		<description><![CDATA[The short answer: you can&#8217;t &#8230; or at least not easily, but there is a better way. Let&#8217;s say you have a page that needs to post back every x minutes. In my case, I needed to implement an auto-save feature that the client requested. Saving the data would happen server side so I just [...]]]></description>
			<content:encoded><![CDATA[<p>The short answer: <em>you can&#8217;t</em> &#8230; or at least not easily, but there <em>is</em> a better way.</p>
<p>Let&#8217;s say you have a page that needs to post back every x minutes.  In my case, I needed to implement an auto-save feature that the client requested.  Saving the data would happen server side so I just used a <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.timer.aspx" target="_blank">Timer</a> to initiate regular post backs.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;%</span>@ Page Language<span style="color: #008000;">=</span><span style="color: #666666;">&quot;C#&quot;</span> <span style="color: #008000;">%&gt;</span>
<span style="color: #008000;">&lt;</span>script runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">void</span> Timer1_OnTick<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Response<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Last Postback: &quot;</span> <span style="color: #008000;">+</span> DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">Now</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&lt;/</span>script<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>html xmlns<span style="color: #008000;">=</span><span style="color: #666666;">&quot;http://www.w3.org/1999/xhtml&quot;</span><span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>head id<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Head1&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>title<span style="color: #008000;">&gt;&lt;/</span>title<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>head<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>body<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>form id<span style="color: #008000;">=</span><span style="color: #666666;">&quot;form1&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
        <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>ScriptManager ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;sm&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> <span style="color: #008000;">/&gt;</span>
        <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Timer ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Timer1&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> 
                OnTick<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Timer1_OnTick&quot;</span> Interval<span style="color: #008000;">=</span><span style="color: #666666;">&quot;4000&quot;</span> <span style="color: #008000;">/&gt;</span>
    <span style="color: #008000;">&lt;/</span>form<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>body<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>html<span style="color: #008000;">&gt;</span></pre></td></tr></table></div>

<p>The problem surfaced when they also wanted a client-side prompt to allow or cancel the auto post back.  <strong>System.Web.UI.Timer does not have any client side events exposed, </strong> so I searched the web and asked our friends over at StackOverflow <a href="http://stackoverflow.com/questions/439677/how-to-run-javascript-prior-to-system-web-ui-timer-ontick" target="_blank">what they would do</a>.</p>
<p>The only response to my post confirmed that the Timer is a bad fit for this situation.  I could hack it, but <em>there must be a better way.</em></p>
<div id="attachment_170" class="wp-caption alignleft" style="width: 360px"><img class="size-full wp-image-170" title="square-peg-in-a-round-hole" src="http://techencoder.com/wp-content/uploads/2009/01/square-peg-in-a-round-hole.png" alt="Square Peg in a Round Hole" width="350" height="190" /><p class="wp-caption-text">Square Peg in a Round Hole</p></div>
<p>Instead of using a Timer to initiate post backs, I switched to a combination of setTimeout() and __doPostBack().  It&#8217;s pure html and JavaScript goodness!  </p>
<p>In this example, I added an update panel to demonstrate how __doPostBack() can also be used to create a partial post back (leaving some parts of the page unchanged after the auto post back).  That is not necessary, but if you are posting back to the server automatically, an AJAX experience will probably be better for the user.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;%</span>@ Page Language<span style="color: #008000;">=</span><span style="color: #666666;">&quot;C#&quot;</span> <span style="color: #008000;">%&gt;</span>
<span style="color: #008000;">&lt;</span>script runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">void</span> Page_Load<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>Page<span style="color: #008000;">.</span><span style="color: #0000FF;">IsPostBack</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #6666cc; font-weight: bold;">string</span> time <span style="color: #008000;">=</span> DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">Now</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">insidePanelLabel</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Last Postback: &quot;</span> <span style="color: #008000;">+</span> time<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">outsidePanelLabel</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;blah blah&quot;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// Does not show up after partial post back.</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&lt;/</span>script<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>html xmlns<span style="color: #008000;">=</span><span style="color: #666666;">&quot;http://www.w3.org/1999/xhtml&quot;</span><span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>head id<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Head1&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>title<span style="color: #008000;">&gt;&lt;/</span>title<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>script type<span style="color: #008000;">=</span><span style="color: #666666;">&quot;text/javascript&quot;</span> language<span style="color: #008000;">=</span><span style="color: #666666;">&quot;javascript&quot;</span><span style="color: #008000;">&gt;</span>
        window<span style="color: #008000;">.</span><span style="color: #0000FF;">onload</span> <span style="color: #008000;">=</span> function<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// start the auto postback timer.</span>
            setPostbackTimer<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">8</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        function setPostbackTimer<span style="color: #008000;">&#40;</span>seconds<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            window<span style="color: #008000;">.</span><span style="color: #0000FF;">setTimeout</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;conditionalPostback()&quot;</span>, seconds <span style="color: #008000;">*</span> <span style="color: #FF0000;">1000</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        function canContinue<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> confirm<span style="color: #008000;">&#40;</span><span style="color: #666666;">'The page is requesting new data.  Ok or Cancel?'</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        function conditionalPostback<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>canContinue<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">// User permits the postback. </span>
                <span style="color: #008080; font-style: italic;">// Do it.</span>
                __doPostBack<span style="color: #008000;">&#40;</span><span style="color: #666666;">'UpdatePanel1'</span>, <span style="color: #666666;">''</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #008000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">// User denied the postback. </span>
                <span style="color: #008080; font-style: italic;">// Reset the timer (they will be prompted again).</span>
                setPostbackTimer<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">8</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&lt;/</span>script<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>head<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>body<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>form id<span style="color: #008000;">=</span><span style="color: #666666;">&quot;form1&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
        <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>ScriptManager ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;sm&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> <span style="color: #008000;">/&gt;</span>
        <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>UpdatePanel ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;UpdatePanel1&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>ContentTemplate<span style="color: #008000;">&gt;</span>
                <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Label ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;insidePanelLabel&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> 
                    Text<span style="color: #008000;">=</span><span style="color: #666666;">&quot;This text will change during auto postbacks.&quot;</span><span style="color: #008000;">/&gt;</span>
            <span style="color: #008000;">&lt;/</span>ContentTemplate<span style="color: #008000;">&gt;</span>
        <span style="color: #008000;">&lt;/</span>asp<span style="color: #008000;">:</span>UpdatePanel<span style="color: #008000;">&gt;</span>
        <span style="color: #008000;">&lt;</span>br <span style="color: #008000;">/&gt;</span>
        <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Label ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;outsidePanelLabel&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> 
            Text<span style="color: #008000;">=</span><span style="color: #666666;">&quot;This text will not change during auto postbacks.&quot;</span><span style="color: #008000;">/&gt;</span>
    <span style="color: #008000;">&lt;/</span>form<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>body<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>html<span style="color: #008000;">&gt;</span></pre></td></tr></table></div>

<p><strong>Have you ever tried to make Microsoft controls fit into a situation only to find that plain html and JavaScript are a better fit?</strong>  </p>
<p>The WebForms model often leads me to over rely on controls and I forget that plain html and JavaScript solutions are available too.  Using controls is fine, but think twice if you have to hack them to get what you want.  There is probably a better way.</p>
<p>Happy coding.</p>
]]></content:encoded>
			<wfw:commentRss>http://techencoder.com/index.php/2009/01/javascript-and-timer-ontick/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

