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">
        <add tagPrefix="uc" tagName="contact_form" src="~/uc/contact_form.ascx"/>

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


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:
    <q>2 + 2</q>
    <q>1 + 2</q>
    <q>6 + 3</q>
    <q>3 + 5</q>

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;


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.


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

$(document).ajaxStop(function() {

Thursday, September 6, 2012

JQuery - set cursor/focus after last character in textbox

1. Create a function:

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

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

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


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
            return false;