Thursday, September 27, 2012

ASP.NET - Registering an user control in web.config

Instead of registering an user control (.ascx) on top of every page we need it, there's a way to register it on the web.config file one time only.
For example, here I am registering an user control of a contact form:


    <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID">
      <controls>
        <add tagPrefix="uc" tagName="contact_form" src="~/uc/contact_form.ascx"/>
      </controls>
    </pages>

And then I can use it anywhere on my website:


<div style="width: 1000px; min-height: 510px;">
<uc:contact_form ID="my_contact_form" runat="server" />
</div>


Cheers!

Friday, September 21, 2012

ASP.NET/C#/XML - Get one random node from XML file

I will show the way I retrieve a single random node from XML file using ASP.NET/C#. In this case I will use an XML file used to store math problems and solution for human verification on an online "Contact Us" form.

XML file format:
<human_check>
  <qa>
    <q>2 + 2</q>
    <a>4</a>
  </qa>
  <qa>
    <q>1 + 2</q>
    <a>3</a>
  </qa>
  <qa>
    <q>6 + 3</q>
    <a>9</a>
  </qa>
  <qa>
    <q>3 + 5</q>
    <a>7</a>
  </qa>
</human_check>

C# code:
public static string get_math_problem(){

Random rnd = new Random();
XDocument questions = XDocument.Load(@"C:\FILE_LOCATION\human_check.xml");

var qas = questions.Descendants("qa");
int qaCount = qas.Count();
return qas.ElementAt(rnd.Next(0, qaCount - 1)).Element("q").Value;
}



Cheers!

Monday, September 17, 2012

Visual Studio - Backspace and arrow keys stop working

Sometimes the backspace and arrow keys stop working - instead of restarting the application simply press ALT + ENTER and it will resume working. Seems like a bug MS didn't get their hand on.

Cheers!

JQuery/AJAX - Fire JQuery functions after all AJAX calls on the document are completed.


$(document).ajaxStop(function() {
            my_jquery_stuff();
});

Thursday, September 6, 2012

JQuery - set cursor/focus after last character in textbox

1. Create a function:


(function($){
    $.fn.setCursorToTextEnd = function() {
        $initialVal = this.val();
        this.val($initialVal + ' ');
        this.val($initialVal);
    };
})(jQuery);

2. Call the function after the normal JQuery focus function:

$('#my_text_box').focus();
$('# my_text_box').setCursorToTextEnd();





Cheers!

Tuesday, September 4, 2012

JQuery/AJAX/IE - screen jumps to top of the page after an AJAX call

Internet Explorer will jump to the top of the page after an AJAX call. You need to add return false after the AJAX call function and it will solve the issue.


            $.ajax( {
                  type : 'GET',
                  url : 'Code/my_code.php?' + new Date().getTime(),
                  data : request_data,
                  success : function(response) {
                        if (response == 'success') {
                              //Do cool stuff
                        }
                        else{
                              alert(“Error”);
                        }
            }});
            return false;

Cheers!