<?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/"
	>

<channel>
	<title>zombiehugs* &#187; ASP</title>
	<atom:link href="http://www.zombiehugs.com/codetags/asp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.zombiehugs.com</link>
	<description>code solutions from a code dunce</description>
	<lastBuildDate>Tue, 26 Nov 2013 15:47:32 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.7.41</generator>
	<item>
		<title>ASP.NET MVC AJAX Action Button (Unobtrusive)</title>
		<link>http://www.zombiehugs.com/2013/10/asp-net-mvc-ajax-action-button/</link>
		<comments>http://www.zombiehugs.com/2013/10/asp-net-mvc-ajax-action-button/#comments</comments>
		<pubDate>Tue, 15 Oct 2013 22:38:10 +0000</pubDate>
		<dc:creator><![CDATA[gerdsen]]></dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[experimental]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[ASP]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://www.zombiehugs.com/?p=235</guid>
		<description><![CDATA[It&#8217;s been a while since I&#8217;ve made a post, mostly because I&#8217;ve been busy and mostly because I usually post information I feel will be helpful to others. Today I want to share a snippet of code that I put together because I was unable to track down the answer I was looking for. Currently [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>It&#8217;s been a while since I&#8217;ve made a post, mostly because I&#8217;ve been busy and mostly because I usually post information I feel will be helpful to others. Today I want to share a snippet of code that I put together because I was unable to track down the answer I was looking for.</p>
<p>Currently I am in a long term project that is primarily based ASP.NET MVC3 and with that comes troubles of its own. The trouble I was having was I wanted to create an HTML BUTTON that behaved much like MVC&#8217;s Ajax.ActionLink does. I wanted to be able to specifiy some button content as well as AjaxOptions for Unobtrusive parsing. I looked on the web, no luck. So I ventured out and created my own extension that I hope you find worthy.<br />
<strong><br />
C# (Place in new or existing file)<br />
</strong></p>
<pre class="prettyprint">public static MvcHtmlString ActionButton(this AjaxHelper ajaxHelper,
   string buttonText,
   string iconSrc,
   AjaxOptions ajaxOptions,
   object routeValues = null,
   object htmlAttributes = null,
   object iconAttributes = null)
{
UrlHelper urlHelper = new UrlHelper(ajaxHelper.ViewContext.RequestContext);
RouteValueDictionary htmlAttributeDictionary =
    HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
string imgUrl = urlHelper.Content(iconSrc);
&#47;&#47;Build icon for use in button
TagBuilder buttonIcon = new TagBuilder(&#34;img&#34;);
buttonIcon.MergeAttribute(&#34;src&#34;, imgUrl);
buttonIcon.MergeAttributes(new RouteValueDictionary(iconAttributes));
&#47;&#47;Text
TagBuilder text = new TagBuilder(&#34;span&#34;) { InnerHtml = buttonText };
&#47;&#47;Build Button and place text and icon inside
TagBuilder button = new TagBuilder(&#34;button&#34;);
button.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes());
button.MergeAttributes(htmlAttributeDictionary);
button.InnerHtml = buttonIcon.ToString(TagRenderMode.SelfClosing) + text.ToString();
return MvcHtmlString.Create(button.ToString());
}</pre>
<p>You can of course overload or customize this extension method to suit your needs (remove routeValues, htmlAttributes, etc&#8230;), we actually have a few variants (icon, no icon) for use in our system for different calls but I wanted to show you a full capability call.</p>
<p><strong>We are not out of the woods just yet, Microsoft&#8217;s Unobtrusive AJAX support library for jQuery does not attach to Buttons!</strong> They support attaching to A, Form, INPUT[type=image] and Submit but not to regular ole Button. So you are going to need to add a little binding in there to get you hooked up. Copy and paste the following.</p>
<p><strong>JavaScript (Place in your Unobtrusive jQuery Library File)</strong></p>
<pre class="prettyprint">    $(&#34;button&#91;data-ajax=true&#93;&#34;).live(&#34;click&#34;, function (evt) {
        evt.preventDefault();
        if (!$(this).attr(&#34;disabled&#34;)) {
            asyncRequest(this, {
                url: this.href,
                type: &#34;GET&#34;,
                data: &#91;&#93;
            });
        }
    });</pre>
<p>&nbsp;</p>
<p><strong>Making the call.<br />
</strong> Now that you are all set you can use the following in your code in your View to return unobtrusive BUTTONS with AJAX bindings, below is a sample call.</p>
<pre class="prettyprint">    @Ajax.ActionButton(&#34;Button Text&#34;, &#34;&#47;Images&#47;x_sm.png&#34;, new AjaxOptions
                {
                    Url = Url.Action(&#34;Add&#34;),
                    UpdateTargetId = &#34;Details&#34;,
                    OnComplete = &#34;javascript: weAreDone();&#34;,
                    InsertionMode = InsertionMode.Replace,
                    OnSuccess = &#34;javascript: hooray();&#34;
                },
                null,
                new { id = &#34;btn1&#34;})</pre>
<p>&nbsp;</p>
<p>That&#8217;s it. Now you have an unobtrusive button in MVC3. Get to styling.</p>
<p>I hope this helps you. I think in the future I will allow for iconAttributes to allow for height/width options. Maybe IDK, I might just leave it all up to CSS.</p>
<p>&nbsp;</p>
<p><strong>HTML Output from above call:<br />
</strong></p>
<pre class="prettyprint">
&lt;button id=&#34;btn1&#34; data-ajax-update=&#34;#Details&#34; data-ajax-success=&#34;javascript:
hooray();&#34; data-ajax-mode=&#34;replace&#34; data-ajax=&#34;true&#34; data-ajax-url=
&#34;&#47;Assignments&#47;Assignments&#47;Add&#34; data-ajax-complete=&#34;javascript: weAreDone();&#34;&gt;
   &lt;img src=&#34;&#47;Images&#47;x_sm.png&#34; alt=&#34;&#34; &#47;&gt;
   &lt;span&gt;Button Text&lt;&#47;span&gt;
&lt;&#47;button&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.zombiehugs.com/2013/10/asp-net-mvc-ajax-action-button/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
