Can I use an asp:Button like an Html.ActionLink?


Can I use an asp:Button like an Html.ActionLink?



I have an ActionLink:

    <%: Html.ActionLink("MyAction", "MyAction") %> 

I would like to use a button instead. Something like this:

    <asp:Button ID="Button1" runat="server" Text="MyAction" /> 

What do I need to do to make clicking the button perform the same action as clicking the ActionLink?


asp.net mvc 2 web application inside a Web site?

1:

How to pass dyanamic parameters in ajax autocompelete query
The actionlink would be rather comparable to Hyperlinkalthough ton or Hyperlink controls in webforms. MVC building email bodyThey all render a standard anchor element in html. MVC2 ViewModel BindingSo yes if you want to make it look like a although ton you should add any css sauce on top of it.. Attribute 'DisplayColumn' is not valid on this declaration type. It is only valid on 'class' declarations
<%: Html.ActionLink("MyAction", "MyAction", null, new { @class = "although ton" } %> 
and in the css file:. What can asp.net *WebForms* developers expect in future?
.although ton  {     outline: 0;      margin: 0 4px 0 0;     padding: 0 1em;     height: 2em;     text-decoration: none !important;      cursor: pointer;      position: relative;     text-align: center;     display: inline-block;     background-color:Green;     border:1px solid Lime; } a.although ton { color:White;} 
Note: I'm a developer so any what design challenged. How do I keep field values in the ASP.NET MVC form if it has been invalidated?Playing around with border-left, -right, -bottom and -top you must make it look nicer.. Fill textboxes based upon current Dropdownlist (database items) selection (asp.NET MVC) Update after the first comment:. Other alternatives, which don't look this nice to me, are:.
<% Using (Html.BeginForm("About", "Home")) {%>      <input type="submit" value="About" />  <% } %> 
Or with the Button control:.
<form id="Form1" runat="server">      <asp:Button runat="server" ID="sfsf" Text ="Go!" PostBackUrl="/Home/About" />  </form> 

2:

I had same problem, and just wrote what I wanted in html..
<input type="submit" value="Cancel" onclick="document.location = '/Product/Index'; return false;" /> 
I would have liked to use a type="although ton", although since this used a different style then i made it a submit although ton and added return false..

3:

If you want an actual Ajax although ton element, rather than a styling hack or mini-form or JQuery, it is also possible although a little involved. It is a shame this MS has not yet chosen to add an ActionButton to both the Html and Ajax helpers as the differences are actually very minor when you remove the duplication of private support methods (you would only need the ActionButton and GenerateButton methods shown below).. The end result is you must have real although tons this trigger like ajax action links:.

e.g.

@Ajax.ActionButton("Delete", "Delete", "document",       new { id = ViewBag.Id },       new AjaxOptions()       {           Confirm="Do you really want to delete this file?",           HttpMethod = "Get",           UpdateTarreceive Id = "documentlist" },           new { id = "RefreshDocuments"       }) 

1. Create an AjaxHelper extension

The code below is based on a decompile of the AjaxExtensions class as many of the required helper methods are not exposed on HtmlHelper..
public static partial class AjaxExtensions {     public static MvcHtmlString ActionButton(this AjaxHelper ajaxHelper, string although tonText, string actionName, string controllerName, object routeValuesBlah, AjaxOptions ajaxOptions, object htmlAttrialthough esBlah)     {         // Convert generic objects to specific collections         RouteValueDictionary routeValues = new RouteValueDictionary(routeValuesBlah);         RouteValueDictionary htmlAttrialthough es = HtmlHelper.AnonymousObjectToHtmlAttrialthough es(htmlAttrialthough esBlah);          if (string.IsNullOrEmpty(although tonText))             throw new ArgumentException("Button text need be provided");         string tarreceive Url = UrlHelper.GenerateUrl((string)null, actionName, controllerName, routeValues, ajaxHelper.RouteCollection, ajaxHelper.ViewContext.RequestContext, true);         return MvcHtmlString.Create(GenerateButton(ajaxHelper, although tonText, tarreceive Url, AjaxExtensions.GetAjaxOptions(ajaxOptions), htmlAttrialthough es));     }      public static string GenerateButton(AjaxHelper ajaxHelper, string linkText, string tarreceive Url, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttrialthough es)     {         TagBuilder tagBuilder = new TagBuilder("input");         tagBuilder.MergeAttrialthough e("value", linkText);         tagBuilder.MergeAttrialthough es<string, object>(htmlAttrialthough es);         tagBuilder.MergeAttrialthough e("href", tarreceive Url);         tagBuilder.MergeAttrialthough e("type", "although ton");         if (ajaxHelper.ViewContext.UnobtrusiveJavaScriptEnabled)             tagBuilder.MergeAttrialthough es<string, object>(ajaxOptions.ToUnobtrusiveHtmlAttrialthough es());         else             tagBuilder.MergeAttrialthough e("onclick", AjaxExtensions.GenerateAjaxScript(ajaxOptions, "Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), {0});"));         return tagBuilder.ToString(TagRenderMode.Normal);     }      private static string GenerateAjaxScript(AjaxOptions ajaxOptions, string scriptFormat)     {         string str = ajaxOptions.ToJavascriptString();         return string.Format((IFormatProvider)CultureInfo.InvariantCulture, scriptFormat, new object[1] { str });     }      private static AjaxOptions GetAjaxOptions(AjaxOptions ajaxOptions)     {         if (ajaxOptions == null)             return new AjaxOptions();         else             return ajaxOptions;     }      public static string ToJavascriptString(this AjaxOptions ajaxOptions)     {         StringBuilder stringBuilder = new StringBuilder("{");         stringBuilder.Append(string.Format((IFormatProvider)CultureInfo.InvariantCulture, " insertionMode: {0},", new object[1]         {              ajaxOptions.InsertionModeString()         }));         stringBuilder.Append(ajaxOptions.PropertyStringIfSpecified("confirm", ajaxOptions.Confirm));         stringBuilder.Append(ajaxOptions.PropertyStringIfSpecified("httpMethod", ajaxOptions.HttpMethod));         stringBuilder.Append(ajaxOptions.PropertyStringIfSpecified("loadingElementId", ajaxOptions.LoadingElementId));         stringBuilder.Append(ajaxOptions.PropertyStringIfSpecified("updateTarreceive Id", ajaxOptions.UpdateTarreceive Id));         stringBuilder.Append(ajaxOptions.PropertyStringIfSpecified("url", ajaxOptions.Url));         stringBuilder.Append(ajaxOptions.EventStringIfSpecified("onBegin", ajaxOptions.OnBegin));         stringBuilder.Append(ajaxOptions.EventStringIfSpecified("onComplete", ajaxOptions.OnComplete));         stringBuilder.Append(ajaxOptions.EventStringIfSpecified("onFailure", ajaxOptions.OnFailure));         stringBuilder.Append(ajaxOptions.EventStringIfSpecified("onSuccess", ajaxOptions.OnSuccess));         --stringBuilder.Length;         stringBuilder.Append(" }");         return ((object)stringBuilder).ToString();     }      public static string InsertionModeString(this AjaxOptions ajaxOptions)     {         switch (ajaxOptions.InsertionMode)         {             case InsertionMode.Replace:                 return "Sys.Mvc.InsertionMode.replace";             case InsertionMode.InsertBefore:                 return "Sys.Mvc.InsertionMode.insertBefore";             case InsertionMode.InsertAfter:                 return "Sys.Mvc.InsertionMode.insertAfter";             default:                 return ((int)ajaxOptions.InsertionMode).ToString((IFormatProvider)CultureInfo.InvariantCulture);         }     }      public static string EventStringIfSpecified(this AjaxOptions ajaxOptions, string propertyName, string handler)     {         if (string.IsNullOrEmpty(handler))             return string.Empty;         return string.Format((IFormatProvider)CultureInfo.InvariantCulture, " {0}: Function.createDelegate(this, {1}),",             new object[2]               {                 propertyName,                 handler               });     }      public static string PropertyStringIfSpecified(this AjaxOptions ajaxOptions, string propertyName, string propertyValue)     {         if (string.IsNullOrEmpty(propertyValue))             return string.Empty;         string str = propertyValue.Replace("'", "\\'");         return string.Format((IFormatProvider)CultureInfo.InvariantCulture, " {0}: '{1}',",             new object[2]               {                 propertyName,                 str               });     } } 

2. Modify jquery.unobtrusive-ajax.js

Only a small change is required to the JQuery of jquery.unobtrusive-ajax.js to accept the new although ton object, as it is very close to begin with. First the selector needs to accept although tons as well as links and then the href needs to come from an attrialthough e so than a non-link must provide it (not strictly browser compliant although works for now)..
$(document).on("click", "input[data-ajax=true],a[data-ajax=true]", function (evt) {         evt.preventDefault();         asyncRequest(this, {             url: $(this).attr("href"),             type: "GET",             data: []         });     }); 
*Note: this is using the latest version of everything as at the date of answering (MVC 5).


57 out of 100 based on 42 user ratings 717 reviews

*