<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>SuperSKa Weblog : jQuery</title><link>http://msmvps.com/blogs/superska/archive/tags/jQuery/default.aspx</link><description>Tags: jQuery</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Autopostback DropDownList with ASP.NET MVC</title><link>http://msmvps.com/blogs/superska/archive/2009/03/02/autopostback-dropdownlist-with-asp-net-mvc.aspx</link><pubDate>Mon, 02 Mar 2009 01:44:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1675241</guid><dc:creator>Stefan Kamphuis</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/superska/rsscomments.aspx?PostID=1675241</wfw:commentRss><comments>http://msmvps.com/blogs/superska/archive/2009/03/02/autopostback-dropdownlist-with-asp-net-mvc.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="PropertiesPane" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/superska/image_5F00_3.png" align="right" border="0" height="219" width="318" /&gt;Didn&amp;#39;t you love the days back when you could just open up the properties tab for your DropDownList in ASP.NET and set the AutoPostBack Property to True? You could even write code in your codebehind class to handle the event properly. Cool, right? &lt;img alt="Hot" src="http://messenger.msn.com/MMM2006-04-19_17.00/Resource/emoticons/shades_smile.gif" /&gt;&lt;/p&gt;
&lt;p&gt;But these are the days of the ASP.NET MVC framework, so everything&amp;#39;s different now. Well, of course I&amp;#39;m being a bit sarcastic &lt;img alt="Sarcastic" src="http://messenger.msn.com/MMM2006-04-19_17.00/Resource/emoticons/50_50.gif" /&gt; here and there are a lot of advantages too to the MVC Framework, but still it requires more work to achieve things-that-used-to-be-simple.&lt;/p&gt;
&lt;p&gt;Anyway, back to the AutoPostBack of the DropDownList. First, of course, you need to have a DropDownList on your page:&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;select&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;#39;dropDown&amp;#39;&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;#39;dropDown&amp;#39;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;option&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;1&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;selected&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;selected&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Option 1&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;option&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;option&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;2&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Option 2&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;option&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;select&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span class="kwrd"&gt;Note that this control needs to be within your Form otherwise it&amp;#39;s values will not be submitted of course...&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class="kwrd"&gt;When you have the control, you&amp;#39;ll need to make it submit the Form it&amp;#39;s in. I chose to do so with jQuery, but you could &lt;a href="http://weblogs.asp.net/jdanforth/archive/2008/08/28/auto-postback-with-javascript-in-asp-net-mvc.aspx" target="_blank"&gt;do it with plain javascript&lt;/a&gt; too:&lt;/span&gt;&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;script&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;#39;text/javascript&amp;#39;&lt;/span&gt;\&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;/pre&gt;
&lt;pre class="csharpcode"&gt;  $(&lt;span class="str"&gt;&amp;#39;#dropDown&amp;#39;&lt;/span&gt;).change(&lt;span class="kwrd"&gt;function&lt;/span&gt; () {&lt;br /&gt;    $(&lt;span class="kwrd"&gt;this&lt;/span&gt;).parents(&lt;span class="str"&gt;&amp;#39;form&amp;#39;&lt;/span&gt;).submit();&lt;br /&gt;  });&lt;/pre&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;script&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;For those of you not fully up to speed with jQuery: this code finds the element named &amp;quot;dropDown&amp;quot; and adds a handler to the onChange event of it. The handler first finds the parent of the DropDownList with elementname &amp;quot;form&amp;quot;, and submits it.&lt;/p&gt;
&lt;p&gt;You could also change the action of the form here in the handler if that might be needed, for example to fire a specific controller action for the postback of the DropDownList.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1675241" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/superska/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://msmvps.com/blogs/superska/archive/tags/ASP.NET+MVC/default.aspx">ASP.NET MVC</category><category domain="http://msmvps.com/blogs/superska/archive/tags/jQuery/default.aspx">jQuery</category></item></channel></rss>