C# and jQuery code to update all checkboxes for multiple ASP.NET grids on a page

I dad a need to update all checkboxes on client side in a grid. Found a great article posted here by Cinnie Patel. I just added a slight mod to make it function on multiple grids on a page. In a nutshell the jQuery code is something like:

function SelectAll(CheckBoxControl,GridName)
{
   var newState;
   if (CheckBoxControl.checked == true)
   {
      newState = true;
   }
   else
   {
      newState = false;
   }
   var i;
   for (i=0; i < document.forms[0].elements.length; i++) 
   { 
      if ((document.forms[0].elements[i].type == 'checkbox') 
           && (document.forms[0].elements[i].name.indexOf(GridName) > -1))
      {
         document.forms[0].elements[i].checked = newState;
      }
   }
}

and then you place the code to call this on the header item of a grid (assuming you are using checkboxes in the header as well to select all/select none) like this:

<input id="SelectAllCheckboxes" onclick="SelectAll(this,'##NAME OF YOUR GRID GOES HERE##')" type="checkbox" />

…….
Hope this helps someone and kudos to original poster.

Print Friendly, PDF & Email

C# web page authentication

I came across need the other day where I had to scrape a page. The issue was that if you are not logged in then you get different content to if you were logged in. It was a relatively easy solution but I came across some code that really stood out at stackoverflow.

The bulk of it a nice simple class to encapsulate the cookies and authentication credentials in a class as follows:

public class CookieAwareWebClient : WebClient
{
public CookieAwareWebClient()
{
CookieContainer = new CookieContainer();
}
public CookieContainer CookieContainer { get; private set; }

protected override WebRequest GetWebRequest(Uri address)
{
var request = (HttpWebRequest)base.GetWebRequest(address);
request.CookieContainer = CookieContainer;
return request;
}
}

Thus once you provide login details you can then easily call on the WebClient method as illustrated in sample code (also from the stackoverflow site):

using (var client = new CookieAwareWebClient())
{
var values = new NameValueCollection
{
{ "username", "john" },
{ "password", "secret" },
};
client.UploadValues("http://domain.loc/logon.aspx", values);

// If the previous call succeeded we now have a valid authentication cookie
// so we could download the protected page
string result = client.DownloadString(“http://domain.loc/testpage.aspx”);
}

Again this is not my code – I just liked it’s elegance in solving a simple problem.

Print Friendly, PDF & Email

Amazon AWS VPC – PEM, SSH and Putty….

I recently had to connect from Windows to Amazon hosted Ubuntu VPC. Putty for Windows generally does the job but Amazon uses a key file with extension PEM. Putty on the other hand uses key file with extension PPK. To facilitate the connection between Putty and Amazon one must thus create a PPK file and configure Putty accordingly. The utility to convert PEM file to PPK file is called PuttyGen and is downloaded from the same web page as Putty

Here are the steps:

  • Open PuttyGen and select Conversions -> Import Key
  • Load the PEM file which you got from Amazon
  • Select Save Private Key (you can ignore the password warning)
  • Save the file to known location or to wherever you save such file s(e.g. Document folder)

Now go to Putty and follow these steps:

  • Specify the address for your Amazon hosted Ubuntu (or whatever other Linux server)
  • On the left hand side select Data and specify Auto-login username – for Ubuntu it by default is Ubuntu
  • Again on the left hand side expand SSH under Connection tree and select Auth
  • Click browse and select your PPK file
  • Go back to Session on the left and presumably save this for future use

Hope this helps out someone.

Cheers!

Print Friendly, PDF & Email