<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>crunchlife: Setting Focus in ASP.NET Ajax Pages</title>
    <link>http://crunchlife.com/articles/2008/03/31/setting-focus-in-asp-net-ajax-pages</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Setting Focus in ASP.NET Ajax Pages</title>
      <description>&lt;p&gt;&lt;a href="http://www.amazon.com/gp/product/B000YJ2OJ2?ie=UTF8&amp;amp;tag=crunchlife-20&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;creativeASIN=B000YJ2OJ2"&gt;&lt;img border="0" src="/files/316ZntjzQ2L._AA_SL160_.jpg" class="photo right"&gt;&lt;/a&gt;&lt;img src="http://www.assoc-amazon.com/e/ir?t=crunchlife-20&amp;amp;l=as2&amp;amp;o=1&amp;amp;a=B000YJ2OJ2" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /&gt;At the new job I&amp;#8217;ve been using a lot of &lt;a href="http://www.asp.net/ajax/" target="_blank"&gt;ASP.NET Ajax&lt;/a&gt; to help ease the transition of users from &lt;a href="http://en.wikipedia.org/wiki/VB6" target="_blank"&gt;VB6&lt;/a&gt; desktop applications to web applications on our company &lt;a href="http://en.wikipedia.org/wiki/Intranet" target="_blank"&gt;intranet&lt;/a&gt;. In doing this, the UpdatePanel has become my new best friend. Albeit charming, my old ASP.NET 2.0 friends were not as impressed.&lt;/p&gt;

&lt;p&gt;With ASP.NET 2.0, came the long-awaited &lt;a href="http://aspnet.4guysfromrolla.com/articles/111506-1.aspx" target="_blank"&gt;Focus&lt;/a&gt; method that allowed developers to set the page focus without having to write any JavaScript. Developers loved it and all was right with the world. That is until ASP.NET Ajax showed up.&lt;/p&gt;

&lt;p&gt;I recently spent way too much time trying set the page focus on a page that contained just a few TextBox controls and an UpdatePanel. My site&amp;#8217;s ScriptManager was located in a MasterPage, but more on that later. My obligatory googling turned up many work arounds, but none as simple as the following:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;// Example

if (Page.IsPostback)
{   
    // Use the GetCurrent method if your ScriptManager is located
    // in a MasterPage. Word.
    ScriptManager.GetCurrent(this.Page).SetFocus(TextBox1);
}
else
{
    // Here is the money.
    AjaxControlToolkit.Utility.SetFocusOnLoad(TextBox2);
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The above example is a two-fer. The ScriptManager&amp;#8217;s SetFocus method is handy, but remember that only one ScriptManager is allowed per page and if you&amp;#8217;ve put your ScriptManager in a MasterPage, then you&amp;#8217;ll need to access it in code using the GetCurrent method of the ScriptManager class. &lt;/p&gt;

&lt;p&gt;Two. The ASP.NET 2.0 UpdatePanel is not the only Ajax control. Download the &lt;a href="http://www.codeplex.com/Wiki/View.aspx?ProjectName=AtlasControlToolkit" target="_blank"&gt;ASP.NET Ajax Control Toolkit&lt;/a&gt;. With this toolkit, you&amp;#8217;ll have access to dozens of controls and best of all &amp;#8211; the SetFocusOnLoad method. Located in the Utility class, the SetFocusOnLoad method is the answer to your !Page.IsPostback problems.&lt;/p&gt;</description>
      <pubDate>Mon, 31 Mar 2008 15:24:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:a059a73f-ed79-457f-9df5-24ab32e0fdd7</guid>
      <author>Ryan Baxter</author>
      <link>http://crunchlife.com/articles/2008/03/31/setting-focus-in-asp-net-ajax-pages</link>
      <category>Code Snippets</category>
      <category>ASPNET</category>
      <category>Ajax</category>
      <category>Oops</category>
      <enclosure url="http://crunchlife.com/files/316ZntjzQ2L._AA_SL160_.jpg" type="image/jpeg" length="9085"/>
      <trackback:ping>http://crunchlife.com/articles/trackback/52</trackback:ping>
    </item>
  </channel>
</rss>
