Copy to Clipboard AJAX Control


This is actually the same code that I used for the BlogEngine.Net extension that I created a while back. However I needed it to be a bit more generic (not BlogEngine specific) and take the code from a text box so I ended up creating an AJAX extender:

CopyToClipboardExtender.zip (2.07 kb)

It's rather basic. The targetID is the link you want them to click on to copy the text. The CopyID is the text box's ID. It most likely will not work in Firefox, etc. and is really only IE specific. However it's better than nothing. 

As far as setting it up, sometimes I get a bit lazy when it comes to explaining how to use some of the code on here. In the case of the AJAX controls, I usually leave out the fact that you need to download the AJAX Control Toolkit. Once you have that and you've added the templates, etc. like it says in the setup, you need to create an ASP.Net AJAX Control Project (I've used the name of AJAXControls, so if you use something different you'll need to potentially change some of the code to point to the correct namespace). From there, you add the code (making sure to have the js file as an embedded resource). Compile and you're ready to go...

Now that jQuery is going to be the norm, the setup process will probably change in the future. Plus I plan on packaging these up at some point like the utilities. For now though I leave you with the code. So try it out, leave feedback, and happy coding.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: AJAX | ASP.Net | Web Design
Posted by James Craig on Friday, December 12, 2008 11:00 AM
Permalink | Comments (0) | Post RSSRSS comment feed

ASP.Net, the Myth


I try to stay up to date on about 10 or so languages (I'm currently looking through the potential updates to C++ and liking most of them). One of the languages I try to stay on top of is PHP and as such I subscribe to a couple of blogs on the subject (Although I find it difficult to find good ones on the subject. If someone has a list of decent ones, feel free to send it to me.) What I find funny is the fact that about once a month either they write or link to an article about why ASP.Net "sucks". Everyone has their opinion on various languages and even I have languages that I hate (Lisp), but I at least understand the need for them, try to understand their strengths/weaknesses, etc. These posts annoy me though as the vast majority of them have their facts wrong and are usually biased because of, well, Microsoft (which I would liken to my bashing of Google/Apple, except in my case I bash because I know the companies can do better. The posts I'm talking about just hate Microsoft.)

All of that being said, I figured I'd write a short list of the common misconceptions that these posts latch onto and why they're wrong:

  1. Windows hosting costs more - This used to be true. I remember trying to find windows hosting early in 2000 and it would have cost me double the cost of a Linux host in some cases. However every host that I've used in the past four years or so has offered it at the same price as Linux hosting (and sometimes as little as $5 a month). Sometimes the feature set is different, but usually not to the point where it would make a difference. Plus if you don't want to use windows/IIS, you don't have to because of...
  2. I have to use Windows Server/IIS - This hasn't been true for a while now. Mono is currently at 2.0 and while there are still issues between it and running code on Windows, their not that big of a deal most of the time. But if you want, you can run Mono on Apache on a Linux box with no issues (in fact, Ubuntu comes with Mono by default). Heck, if you want to run .Net projects on OS X, you can with Mono (although I haven't tried it yet to see how well it works).
  3. I'm stuck using Visual Studio - Once again, not true. MonoDevelop can be used to create ASP.Net web sites on Linux.
  4. I like OS X and you can't do ASP.Net on a Mac - You didn't bother going to the MonoDevelop page, did you? It works on OS X.
  5. I can't make a standards compliant website using ASP.Net - The standard controls that come with ASP.Net aren't the best and for some odd reason people stop as soon as they see that fact. But if you don't like them, you don't have to use them. You can roll your own, use the innerHTML property, etc. You have 100% control over what it spits out.
  6. The view state is so large/ASP.Net is a bandwidth hog - Once again, you have complete control over what it spits out. You don't have to use the view state (turn it off) or if you do you can compact it or not even send it and save it on the server. On top of that you can turn off "pretty printing", set up compression using gzip or deflate on the various pages, set ETags, etc.

I have more, but those are the ones I hear the most. Oh, that and "I don't want to be tied to MS". Once again, the top four items show that you don't have to touch a Microsoft product (I even have a test server running Linux, MySQL, Mono, etc. No MS products). I know that this wont change anyone's opinion, wont reach anyone who isn't already using .Net, etc. Personally, I like C# and ASP.Net, but I like PHP as well. I realize the strengths of both. I just wish that people would read up on something before dismissing it... Anyway, hopefully I'll have some more code by tomorrow. In the mean time, take a look at Mono, set up a Linux box, etc. and happy coding.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: ASP.Net | General
Posted by James Craig on Thursday, December 11, 2008 10:27 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Random String/Password Generation in C#


I'm guessing that someone out there has been in my position at some point in time... Basically I needed a password generator for a web site that I'm working on. I've seen a number of password generators out there and to be honest, I'm not a big fan of most of them. At the same time I couldn't use the default membership providers (long story as to why not). So basically I was stuck finding a way to generate my own random passwords (not to mention my own membership provider).

It's not a difficult task mind you, just annoying. That being said, I came up with the following class:

    public class Random:System.Random
    {
        public Random()
            : base()
        {
        }

        public Random(int Seed)
            : base(Seed)
        {
        }

        public string NextString(int Length)
        {
            if(Length<1)
                return "";
            StringBuilder TempBuilder=new StringBuilder();
            while(TempBuilder.Length<Length)
            {
                TempBuilder.Append(Convert.ToChar(Convert.ToInt32(Math.Floor(94*NextDouble()+32))));
            }
            return TempBuilder.ToString();
        }

        public string NextString(int Length,string AllowedCharacters)
        {
            if (Length < 1)
                return "";
            StringBuilder TempBuilder = new StringBuilder();
            Regex Comparer=new Regex(AllowedCharacters);
            while (TempBuilder.Length < Length)
            {
                string TempValue = new string(Convert.ToChar(Convert.ToInt32(Math.Floor(94 * NextDouble() + 32))), 1);
                if(Comparer.IsMatch(TempValue))
                    TempBuilder.Append(TempValue);
            }
            return TempBuilder.ToString();
        }

        public string NextString(int Length, string AllowedCharacters,int NumberOfNonAlphaNumericsAllowed)
        {
            if (Length < 1)
                return "";
            StringBuilder TempBuilder = new StringBuilder();
            Regex Comparer = new Regex(AllowedCharacters);
            Regex AlphaNumbericComparer=new Regex("[0-9a-zA-z]");
            int Counter = 0;
            while (TempBuilder.Length < Length)
            {
                string TempValue = new string(Convert.ToChar(Convert.ToInt32(Math.Floor(94 * NextDouble() + 32))), 1);
                if (Comparer.IsMatch(TempValue))
                {
                    if (!AlphaNumbericComparer.IsMatch(TempValue) && NumberOfNonAlphaNumericsAllowed > Counter)
                    {
                        TempBuilder.Append(TempValue);
                        ++Counter;
                    }
                    else if (AlphaNumbericComparer.IsMatch(TempValue))
                    {
                        TempBuilder.Append(TempValue);
                    }
                }
            }
            return TempBuilder.ToString();
        }
    }

This class has three functions that it adds to the Random classes normal ones (NextDouble, etc.). The one that is probably of most interest is going to be the last one. That function takes in the length of the string that you want, a regular expression stating what characters are allowed, and the number of non alpha numeric characters. So for instance calling the function with this:

NextString(10,"[0-9a-zA-Z_]",1);

That would create a random string of 10 characters that contained letters, numbers, and up to one underscore. The other functions are similar but with less control. For instance the one that just takes the length will use all characters on the ASCII table from 32 (space) up to 126 (~). But you can use this for quite a few items, passwords, testing, blog post generation when you're extremely bored and lazy... The list is rather endless... Anyway, I hope this helps someone out, so try out the code, leave feedback, and happy coding.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by James Craig on Monday, November 24, 2008 12:27 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Recent comments

None

Calendar

<<  January 2009  >>
SuMoTuWeThFrSa
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

Sponsors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2009