CustomValidator – level 200

The CustomValidator control is really flexible. Here’s a quick example of validating that a checkbox is checked:


<%@ Page language=”c#” Codebehind=”ValidateCheckbox.aspx.cs” AutoEventWireup=”false” Inherits=”WebApplication1.ValidateCheckbox” %>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN” >
<HTML>
 <HEAD>
  
 function ValidateChecked(oSrc, args){
  if(document.all[“”].checked == false){
   alert(“Has to be checked.”);
   args.IsValid = false;
  }
 }
  
 </HEAD>
 <body>
  <form id=”Form1″ method=”post” runat=”server”>
   <asp:CustomValidator ClientValidationFunction=”ValidateChecked” Runat=”server” ID=”val” />
   <asp:CheckBox ID=”chk” Runat=”server” />
   <asp:Button ID=”btn” Runat=”server” Text=”Submit” />
  </form>
 </body>
</HTML>


And the code-behind:



private void val_ServerValidate(object source, ServerValidateEventArgs args)


{


    args.IsValid = true;


    if(chk.Checked == false)


        args.IsValid = false;


}

Newsgroup rant – am I preaching to the choir? – level 100

The following is a basic skill that every developer should have – even those who have been developing for just a few days:  Searching the Internet.


Yes, that’s it, folks.  How many developers still don’t have this skill?  How many newsgroup posts could be avoided by a simple Internet search.  A favorite developer search is Google because we can do a search, and if an article doesn’t pop up, we can “group” it, and somebody is bound to have made a newsgroup post about it already (with an answer).


I’ve answered many newsgroup posts just by doing the google search for them.  Am I helping them or hurting them?  For instance, in the ASP.NET newsgroup yesterday there was a question as to what other ASP.NET IDEs were out there besides VS and Web Matrix?  Wow, what a tough question.  Let’s do a search and find out instantly. . . or I can post a newsgroup post and hope to get an answer by the next day or day after that.


What’s worse is to find a newsgroup post of this level posted by someone with MCAD or MCSD by their name. . . *gasp*

VB6 still works! Who would have thought? – level 000

There is a current VB6 process that I was considering rewriting in .Net with C#.  I began learning the code to prepare for the rewrite, and there is a LOT of code.  I realized as I dug deeper into the code that I would be better off making the code changes necessary in VB6 instead of doing a complete rewrite.  Now, I am not a veteran Visual Basic developer, so I prefer working in .Net with C#, but the more I thought about it the more I knew that keeping this process as VB6 would be better.  This code was several years old and had stood the test of time.  My rewrite would undoubtedly introduce bugs that don’t exist now.


The point of this post is that we should not rewrite to .Net just for the sake of .Net.  We code every new process in .Net, but out legacy code still marches on.

CopyAsHtml Add-in for VS – level 100

This is a very useful tool.  Properly formatting code for a blog post has been a hassle up until now!  This is great.  A must-have tool.  Great job Colin.


Here is an example of code copied directly from VS using this add-in.



        private void WebForm1_Init(object sender, EventArgs e)


        {


            Trace.Write(“WebForm1_Init”);


        }


 


        private void WebForm1_Load(object sender, EventArgs e)


        {


            Trace.Write(“WebForm1_Load”);


        }


 


        private void WebForm1_PreRender(object sender, EventArgs e)


        {


            Trace.Write(“WebForm1_PreRender”);


        }


 


        private void WebForm1_Unload(object sender, EventArgs e)


        {


            Trace.Write(“WebForm1_Unload”);


        }

How to use app-relative paths in ASP.NET – level 100

I’ve been using this feature since the beginning, but I’ve come to realize
that a lot of people aren’t aware of this.  The application path shortcut:
“~/”

When including any resource in your page whether it be a user control,
image, css or whatever, it has to be located.  The simplest thing to do is
to type the entire path, http://localhost/myApp/myStyle.css, but that isn’t
very maintainable because when you deploy, you have to change this
reference.  So the next step is making the path relative to the page (buried
2 levels deep):  ../../myStyle.css, but what if the site structure changes?
What do you do?  You could make it domain-relative:  /myApp/myStyle.css, but
what if your application needs to be deployed to it’s own domain:
http://myAppDomain.com/ ?  Now this reference is broken.  Some people get
around this by setting the path with Request.ApplicationPath +
“/myStyle.css”, but this doesn’t work in all cases.  If you have a virtual
path, you get /myApp, but if you are at the root, you get “/”, so in the
case of /myApp, you have to add a “/” to the end.

To get around all this, use “~/” as a shortcut to you application path.
With that shortcut at the beginning of your path, any server control will
resolve the url for you.  If you use this in a non-server control or just
print it out, you will need to resolve it yourself using the ResolveUrl(…)
method of the Control class.  This method is inherited by all controls, so
you can use it directly in Page, UserControl and all web controls.  I use
nothing but app-relative paths now.

In .Net 2.0, we’ll get more App-relative shortcut properties like
Request.AppRelativePath.  It’s really a much cleaner way to represent a
resource relative to the application no matter what the path to the
application is.

Impersonating a web user – level 200

One of these days, it will become necessary to impersonate a user at a
website.  Or, a more common use would be to use a legacy ASP app as a
make-shift web service.  Say an ASP page does an important function?  From
your application(whether windows, console, or web), you can write code to
contact this page and interact with it over HTTP.  Here is a sample:

String uriString = http://localhost/WebApplication1/AcceptFile.aspx;
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
string fileName = @”c:upload.txt”;
byte[] responseArray = myWebClient.UploadFile(uriString, “POST”, fileName);


Using the WebClient class in the System.Net namespace, it’s very easy to
create a custom web request and execute it.  Then you can capture the
response and analyze it. . . parse any important data contained in it.

If you have used Application Center Test or the Web Application Stress Tool,
you record a web session, and then that session is repeated many times.
Code is being written to repeat the web requests.

EASY, EASY custom web.config section – level 200

Some people my shy away from this, but see how easy it is.  You need a block of custom xml as configuration, so you create your own .xml file and read it in.  Instead, use the web.config file.  It’s already there, and it’s good to keep all configuration in one place.  This is all it takes to make your own Configuration Handler class:


public class Config : IConfigurationSectionHandler {


//stores authorization xml from .config file


protected static XmlNode m_oXmlSection;


public static XmlNode XmlSection{


get { return m_oXmlSection; }


}


public object Create(object parent, object configContext, XmlNode section) {


m_oXmlSection = section.FirstChild;


return section.InnerXml;


}


}


Then, you just access Config.XmlSection, and you have the complete Xml, so why wouldn’t you do it this way?

Tabbed browsing revisited

I’m reposting my previous post in binary for those geeks for whom English is a second language.  Go to http://nickciske.com/tools/binary.php to translate.


010101000110100001101001011100110010000001100100011011110110010101110011011011100010011101110100001000000110001101
101111011011100110001101100101011100100110111000100000001011100100111001100101011101000010000001100101011110000110
001101100101011100000111010000100000011101000110100001100001011101000010000001110100011010000110010100100000011000
100111001001101111011101110111001101100101011100100010000001101001011100110010000001110100011010000110010100100000
011000110110110001101001011001010110111001110100001000000110011001101111011100100010000001000001010100110101000000
101110010011100100010101010100001011100010000000100000010100110110111101101101011001010010000001110000011100100110
000101101001011100110110010100100000010001100110100101110010011001010110011001101111011110000010000001100001011011
100110010000100000011011110111010001101000011001010111001000100000011000100111001001101111011101110111001101100101
011100100111001100100000011001100110111101110010001000000111010001100001011000100110001001100101011001000010000001
100010011100100110111101110111011100110110100101101110011001110010110000100000011000010110111001100100001000000100
100100100000011101000110100001101001011011100110101100100000011101000110100001100001011101000010000001110100011010
000110100101110011001000000110100101110011001000000110000100100000011001100110010101100001011101000111010101110010
011001010010000001110100011010000110000101110100001000000111001101101000011011110111010101101100011001000110111000
100111011101000010000001100010011001010010000001101100011000010110001101101011011010010110111001100111001000000110
011001110010011011110110110100100000010010010100010100101110001000000010000000001101000010100000110100001010010010
010010011101110110011001010010000001100010011001010110010101101110001000000111010101110011011010010110111001100111
001000000100110101100001011110000111010001101000011011110110111000100000001010000111010101110011011001010110010000
100000011101000110111100100000011000100110010100100000010011010111100101001001010001010011001000101001001011000010
000001100001011011100110010000100000010010010010000001100001011011010010000001110110011001010111001001111001001000
000111001101100001011101000110100101110011011001100110100101100101011001000010111000100000001000000100100101110100
001000000110100101110011001000000110000100100000011101110111001001100001011100000111000001100101011100100010000001
100110011011110111001000100000010010010100010100101100001000000110000101101110011001000010000001101001011101000010
000001101011011001010110010101110000011100110010000001101001011011100111001101110100011000010110111001100011011001
010111001100100000011011110110011000100000010010010100010100100000011010010110111000100000011000010010000001110100
011000010110001001100010011001010110010000100000011000010111000001110000011011000110100101100011011000010111010001
101001011011110110111000101110001000000010000001001001001000000110010001100101011101100110010101101100011011110111
000000100000010000010101001101010000001011100100111001000101010101000010000001100001011100000111000001101100011010
010110001101100001011101000110100101101111011011100111001100100000011101110110100101110100011010000010000001001101
011000010111100001110100011010000110111101101110001011000010000001100001011011100110010000100000011101000110100001
100101011100100110010100100000011010010111001100100000011011100110111100100000011001000110100101100110011001100110
010101110010011001010110111001100011011001010010000001100010011001010110001101100001011101010111001101100101001000
000110100101110100001000000111010101110011011001010111001100100000011101000110100001100101001000000100100101000101
001000000110010101101110011001110110100101101110011001010010111000100000001000000000110100001010000011010000101001
000001011011000110111101101110011001110010000001110100011010000110010100100000011000100111001001101111011101110111
001101100101011100100010000001101100011010010110111001100101011100110010110000100000011010010110011000100000011101
110110010100100000001010000100000101010011010100000010111001001110010001010101010000100000011001000110010101110110
011001010110110001101111011100000110010101110010011100110010100100100000011001000110010101110110011001010110110001
101111011100000110010101100100001000000111000001100001011001110110010101110011001000000111010101110011011010010110
111001100111001000000111010001101000011001010010000001011000010010000101010001001101010011000010000001110011011101
000110000101101110011001000110000101110010011001000010000001110111011010010111010001101000001000000100001101010011
010100110010110000100000011101000110100001100101011011100010000001110111011001010010000001110111011011110110111000
100111011101000010000001101000011000010111011001100101001000000111001101101111001000000110110101100001011011100111
100100100000011000110111001001101111011100110111001100101101011000100111001001101111011101110111001101100101011100
100010000001101001011100110111001101110101011001010111001100101110001000000010000001010011011101000110000101101110
011001000110000101110010011001000111001100100000011001100110111101110010001000000111010001101000011001010010000001
110111011001010110001000100000011000010111001001100101001000000111011101100101011011000110110000101101011001010111
001101110100011000010110001001101100011010010111001101101000011001010110010000100000011011100110111101110111001011
000010000001110011011011110010000001110111011001010010000001101110011001010110010101100100001000000111010001101111
001000000110000101100100011010000110010101110010011001010010000001110100011011110010000001110100011010000110010101
101101001000000111001101101111001000000111010001101000011000010111010000100000011000100111001001101111011101110111
001101100101011100100010000001100011011010000110111101101001011000110110010100100000011001000110111101100101011100
110110111000100111011101000010000001101101011000010111010001110100011001010111001000101110001000000010000000001101
000010100000110100001010010010000110111101110111001000000110110101100001011011100111100100100000011100000110010101
101111011100000110110001100101001000000111001101110100011010010110110001101100001000000110010001101111011011100010
011101110100001000000111010101110011011010010110111001100111001000000110000100100000010001000100111101000011010101
000101100101010000010001010010000001101001011011100010000001110100011010000110010101101001011100100010000001110000
01100001011001110110010101110011001111110000110100001010