How to use reCaptcha with c#

Hello all,

Today i want to share example of reCaptcha. Some of us have try to implement reCaptcha with c# but they are facing some issues to implement it. So, i have create one simple example with update panel.

First register your site at recaptcha.net. By clicking on create key you will get your public key and private key. We will need this key to implement our captcha.

Now, we need recaptcha.dll for c#. You will get dll from here or go to resourses in recaptcha site.

We are ready to code in our application. First add below code in your aspx page.

// Add to register captcha
<%@ Register TagPrefix="recaptcha" Namespace="Recaptcha" Assembly="Recaptcha" %>

// Add in your page html

            
                
Post a Comment

Now, add following code in cs file.

// Add following code for your button btnSubmit_Click event.
recaptcha.Validate();
if (Page.IsValid && recaptcha.IsValid)
{
     // your code here
}
else
{
        divRecaptcha.Visible = true;
        ScriptManager.RegisterClientScriptBlock(
          recaptcha,
          recaptcha.GetType(),
          "recaptcha",
          "Recaptcha._init_options(RecaptchaOptions);"
          + "if ( RecaptchaOptions && \"custom\" == RecaptchaOptions.theme )"
          + "{"
          + "  if ( RecaptchaOptions.custom_theme_widget )"
          + "  {"
          + "    Recaptcha.widget = Recaptcha.$(RecaptchaOptions.custom_theme_widget);"
          + "    Recaptcha.challenge_callback();"
          + "  }"
          + "} else {"
          + "  if ( Recaptcha.widget == null || !document.getElementById(\"recaptcha_widget_div\") )"
          + "  {"
          + "    jQuery(\"#" + divRecaptcha.ClientID + "\").html('
');" + " Recaptcha.widget = Recaptcha.$(\"recaptcha_widget_div\");" + " }" + " Recaptcha.reload();" + " Recaptcha.challenge_callback();" + "}", true ); }

Here, script manager block is for reload the captcha when captcha is incorrect. I have faced many issues to implement captcha like my dialog box is not submited, captcha is submit but invalid captcha enter and reload captcha not done etc…

Use custom validator to validate reCaptcha. Also, reload captcha issue solved by script manager.

Remove invalid characters from uploaded file name

Hi,

Today I have a requirement in my web application to remove the invalid characters from uploaded file name.

For this, I have created small function which removes the invalid characters from file name.

see the below function which I have used for the above requirement.


     string invalid = new string(Path.GetInvalidFileNameChars());
     foreach (char c in invalid)
     {
          fileName = fileName.Replace(c.ToString(), "");
     }
}

you can also replace it with “underscore sign” or any other valid character by using below code


     string invalid = new string(Path.GetInvalidFileNameChars());
     foreach (char c in invalid)
     {
          fileName = fileName.Replace(c.ToString(), "_");
     }
}

If you want to remove invalid characters from path then use below code


     string invalid = new string(Path.GetInvalidPathChars());
     foreach (char c in invalid)
     {
          fileName = fileName.Replace(c.ToString(), "_");
     }
}

Hope this will help.

How to call server side button event from javascript function

Hello Friends,

Today I am going to describe how to call server side event from client side.

we can do this by different ways like web service and Page Methods, using jquery, using scriptmanger and __dopostback, using javascript etc.

Today I will show you how to call a server-side ASP.NET button event from client-side using JavaScript function.

See the below aspx code;


   
This is test page :

You can see that, I have defined one server side button with onclick event.

Here, I have also defined aspx label and client side button.

I have used client side button event for calling the server side button event; where I have write test message in aspx label.

see below code for aspx code behind button event


protected void btnTest_Click(object sender, EventArgs e)
{
    lblMessage.Text = "Test message";
}

Now how can we call this event from client side function; see the below client side funtion


function TestCall()
{
     var btnTest = document.getElementById("<%= btnTest.ClientID %>");
     btnTest.click();
}

That’s it.

Hope this will help you !!….

Increase textbox width onclick by jQuery

Hello Friends,

Today i want to share one small example of increase textbox width onclick by jquery. Actually to increase width of textbox we will animate textbox with jquery.

We will use following code to animate textbox.

        $(document).ready(function ()
        {
            $('#txtSearch').focus(function ()
            {
                /*to make flexible, store the current width in an attribute*/
                $(this).attr('width-default', $(this).width());
                $(this).animate({ width: 250 }, 'slow');
            }).blur(function ()
            {
                /* restore the original width */
                var w = $(this).attr('width-default');
                $(this).animate({ width: w }, 'slow');
            });
        });

Replace txtSearch with your textbox name and also change width in animate as appropriate your width. Enjoy!!!

How to open and close jquery UI dialog box

Hi Friends,

Today I am going to represent how to use jquery dialog box in html page.

In this article I will describe, How to open and close jQuery UI Dialog box on click of anchor tag.

Add below code in your html header section







I have added online jquery script reference. you can download it from those URL.

Now add below code in html body section


Sample demo of jquery modal popup

click here to open dialog box

Sample content of modal popup window comes her

This is sample jquery modal popup window...

Design this as per your requirement


That’s it.

Click here for online demo of jquerymodal

Update xml element in asp.net using linq

Hi Friends,

In my earlier post, we have seen that how can we delete xml element using linq to xml query.

you can check it from here

Today we will see how to update element in existing XML file.

For this, we need to load XML file from the disk, update the element and save the updated XML to the disk.

Let’s see how can we do this using below example.

see the below searched xml element:

 
   3
   Gautam
   bhatia
   30
 
 

I have searched xml element in my previous post for deleting it.
Now we will update last name for the Employee Gautam


.............
.............
first search the element employee from the emplyees.xml file
load it into xmlData variable
.............
.............

XDocument doc = XDocument.Load(XMLDocPath);
var xmlData = (from item in doc.Descendants("employee")
               select item).ToList();

foreach (var item in xmlData)
{
   item.Element("lastname").Value = "Gambhir";
}

doc.Save(XMLDocPath);

hope this will help you.

How do I get human readable file size in c#

Hi Friends,

Today I have small requirements in my web application to display file size in human readable format like B,KB,MB,GB etc.

I have created small function for the above requirement.

Please check below code which fulfill the above requirement

private string GetFileSize(FileInfo f)
    {
        string[] fileUnits = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
        int fuCount = 0;
        long size = f.Length;

        while (size >= 1024)
        {
            fuCount++;
            size /= 1024;
        }

        return string.Format("{0} {1}", size, fileUnits[fuCount]);
    }

put above function in your page and call it like


 FileInfo f = new FileInfo(youfilepath);
 ltFileSize.Text = GetFileSize(f);

It will display your provide file size in human readable format.

Hope this will help you !!…