Programming Languages Hacks

Importanti regole per linguaggi di programmazione rilevanti come Java, C, C++, C#…

  • Subscribe

  • Lettori

    I miei lettori abituali

  • Twitter

Page Doesn’t Postback After Validation Error

Posted by Ricibald on 27th June 2013

If there is a validation error, web controls that don’t need to repeat validation don’t postback on first attempt but postback on second attempt.

The reason (thanks to Lions Den) is that after running Page_ClientValidate ASP.NET runs another built in function ValidatorCommonOnSubmit. Within Page_ClientValidate, Page_BlockSubmit is set based on the validation. The postback is then blocked in ValidatorCommonOnSubmit if Page_BlockSubmit is true. No matter what, at the end of the function Page_BlockSubmit is always reset back to false. If a page does a postback without running any validation and Page_BlockSubmit has not been reset to false, the partial postback will be blocked.

First solution: reset Page_BlockSubmit

In this solution we just reset Page_BlockSubmit. This is the simplest solution and doesn’t have collateral effects. But you need to set this on every case you’ll need it.

function DoValidation() {
   var validated = Page_ClientValidate('ValidationGroup');

   // THIS IS THE CHANGE: REMOVE THE FLAG
   Page_BlockSubmit = false;

   return validated;
}

Second solution: never block postback

In this solution we redefine the code of ValidatorCommonOnSubmit returning always true (just make sure to copy and paste the same ASP.NET implementation):

function ValidatorCommonOnSubmit() {
   Page_InvalidControlToBeFocused = null;
   var result = !Page_BlockSubmit;
   if ((typeof(window.event) != "undefined") && (window.event != null)) {
      window.event.returnValue = result;
   }
   Page_BlockSubmit = false;

   // THIS IS THE CHANGE: ALWAYS RETURN TRUE
   return true;
}

Posted in asp.net | No Comments »

Adapt/convert Microsoft Ajax events to JQuery

Posted by Ricibald on 22nd July 2011

It’s very useful to express everything in JQuery instead of ASP.NET AJAX events system: in this way we can use a lot of useful JQuery controls.

To achieve this just add this snippet of javascript code:

<script type="text/javascript">
	Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequest);
	Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequest);
	function BeginRequest(sender, args) {
		jQuery.event.trigger('ajaxSend');		
	}
	function EndRequest(sender, args) {   
		jQuery.event.trigger('ajaxComplete');		                             
	}
</script>

In this way you can capture standard JQuery event to obtain for example a modal progress feedback both in case of JQuery ajax POST and in case of ASP.NET UpdatePanel events:

<script type="text/javascript">
	$(document).bind("ajaxSend", function () {
		$("#loadingAjax").dialog({ modal: true });
	}).bind("ajaxComplete", function () {
		$("#loadingAjax").dialog("close");
	});
</script>

Posted in asp.net, javascript, jQuery | No Comments »

jQuery: HTTP POST with specific <input type=”submit”>

Posted by Ricibald on 19th April 2011

jQuery allows only to do a global form submit using

$("form").submit()

This method doesn’t allow to do a specific form submit when multiple <input type=”submit”> exists, like in many ASP.NET sites, where there are a single form with many buttons.

To achieve this and obtain an ASP.NET POSTBACK behavior simply use this snippet of jQuery code that builds a “submit simulation from input”:

// simulate submit from button "myButton"
var input = $("<input>").attr("type", "hidden").attr("name", $('#myButton').attr("name")).val("");
$('form').append($(input));
$('form').submit();

Posted in .net, javascript, jQuery | No Comments »

WebControl Sempre Abilitati

Posted by Ricibald on 6th February 2009

Il problema è questo: devo creare un LinkButton che sia sempre Enabled. Ma se un suo contenitore (ad esempio un Panel) è disabilitato, allora il LinkButton nel rendering va ad impostare la proprietà disabled navigando tra i padri.

Sarebbe bello se riuscissimo a modificare il comportamento dei WebControl per non rilevare disabled in uno dei padri. La soluzione sta nell’impedire che il nostro controllo figlio (ad esempio il nostro LinkButton) faccia uso di alcune funzionalità ereditate dalla classe base WebControl.

Ricerche su Internet non sono servite. Ho cercato come è implementato il WebControl ispezionando il codice sorgente di WebControl di Mono e il codice sorgente di LinkButton.

Il risultato è di seguito:

    public class LinkButtonSmartAlwaysEnabled : LinkButton
    {
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            //base.AddAttributesToRender(writer);
            if (ID != null)
                writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);

            if (AccessKey != string.Empty)
                writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, AccessKey);

            //if (!enabled)
            //    writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");

            if (ToolTip != string.Empty)
                writer.AddAttribute(HtmlTextWriterAttribute.Title, ToolTip);

            if (TabIndex != 0)
                writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, TabIndex.ToString());

            if (Enabled &amp;&amp; Page != null)
            {
                string href = Page.ClientScript.GetPostBackClientHyperlink(this, "");
                writer.AddAttribute(HtmlTextWriterAttribute.Href, href);
            }

            if (this.Attributes != null)
                foreach (string s in this.Attributes.Keys)
                    writer.AddAttribute(s, this.Attributes[s]);
        }
    }

Posted in .net | 1 Comment »