Category Archives: .NET

One Advantage of CodeWriter

A few weeks ago I posted about WebMatrix, which is a really nice light weight tool if you don’t want to fire up full Visual Studio.  I also indicated that I was using CodeWriter.  I have noticed one real advantage that CodeWriter has over WebMatrix.  I can run CodeWriter on my Surface 2.  My Surface 2 is not a pro version so I cannot run Visual Studio on it, and in turn I cannot run WebMatrix on it.

I will admit, I do not do a whole lot of dev work on my Surface 2.  In fact I normally write something up in CodeWriter so I can copy/paste it into OneNote or test something really small.

WebMatrix Collapsible Areas

Just a quick post about something I noticed in WebMatrix today.  It has some very cool collapsible areas, something I wish I had in Visual Studio 2013 Premium.  Take a look at the screenshot below, you’ll see what I am talking about.  Don’t worry about the code, it is just some basic JavaScript I threw together for this post.

WebMatrix Collapsible Areas

Hello WebMatrix

I have been using a tool for some PluralSight classes and currently a book I’m reading.  Normally I just jump into Visual Studio, but I will admit it is a bit over kill for just playing with HTML5/CSS3.  The PluralSight courses and Pro HTML5 with Visual Studio 2012 make use of WebMatrix.  In fact when I’m on my Windows 8.1 box I often use CodeWriter for the lightweight stuff, but the instructor and writer used WebMatrix so I followed suit.

Not only is WebMatrix quick and lightweight it has intellisense and even the ability to create your site in Azure if you take that option.  Like Visual Studio you can launch your site from the tool in the browser of your choice.  I have not explored everything WebMatrix can do, but I will definitely be using it for small minor things where Visual Studio would just be over kill.  At this time I really don’t see myself using it for real work, just for when I want to play/learn outside a real project.  For example I know you can create an MVC project in WebMatrix but I feel more comfortable in Visual Studio for the real projects.

So if you’re looking for something lightweight that feels like Visual Studio I highly suggest Web Matrix.

Breaking Into Your Lambda

I just learned something new today at Microsoft Virtual Academy.  Heck I almost always learn something new or even things I forgot at Microsoft Virtual Academy.  In this case I thought it was pretty neat.  We all use break points throughout our code, most of the time I just break on a line.  I just learned how to break into a Lambda expression.

Basically you click inside your Lambda expression and then press F9.  To show this I will create a quick class called Stuff.

    public class Stuff
        public string Name { get; set; }
        public int Quantity { get; set; }

In another area I am going to use this class to create a List<Stuff> and then use a Lambda on that list.

            List someStuff = new List()
                                new Stuff{Name = "Dice", Quantity = 2}, 
                                new Stuff{Name="Cards", Quantity = 52}, 
                                new Stuff{Name = "Tokens", Quantity = 20}

            var result = someStuff.Where(s => s.Name.Length > 4);

Next I click in my Lambda and press F9, below is a screenshot of the break point inside my Lambda.

Breaking Into Lambda

The WSDL Tool

The WSDL tool is a handy tool to generate a proxy class to consume a web service.  There is plenty of information out there about it, so I will not go into detail.  I will provide some links though.  One bit of advice is to add a new Environment Variable to your machine.  Here is an example, I named mine Path.

Variable name: Path

Variable value: %PATH%;C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin

You may need to verify that path on your actual machine, it should be the location of wsdl.exe.

Web Services Description Language Tool on MSDN.

Read the rest of this entry

Viewing Your Proxy Class’ SOAP

Most of us are a bit spoiled with .NET and its lovely ability to take a WSDL (via the WSDL Tool) and create us a proxy class.  We can then use this object to work with the web service.  What if you need to actually see that XML?

Well, Microsoft has a solution.  You can use an extension method to log your SOAP.  Not sure how to do it, don’t worry about that either, Microsoft has already written it for you.

Please go to this link to read the full article.

Using Web User Controls

So you’ve written a nice little Web User Control, but how do you use it?

Using your Web User Control is actually really simple. I am not going to address creating one just how to add that reference to your nice little .ascx file. There are two ways to do this, as a page directive or as I prefer, in the web.config file.

<%@ Register Src="MyControl.ascx" TagName="MyControl" TagPrefix="uc1" %>

For myself I prefer to use the web.config setting. In your controls section which is inside the pages section you just add them. In my example below I wanted to show one that goes to a dll (the Ajax Control Toolkit) and one that goes to an ascx file inside a UserControl folder in my project.

<pages viewStateEncryptionMode="Always">
    <add tagPrefix="cc1" namespace="AjaxControlToolkit" assembly="AjaxControlToolkit"/>
    <add tagPrefix="my" tagName="FormFields" src="~/CustomControls/MyFormFields.ascx"/>

You will reference your control with <tagPrefix:tagName>.  The tagPrefix is the first half of the control tag and should be fairly obvious and is something you control.  So instead of <asp:  you would have <my: or <cc1:  For the other half of the tag it is a bit different, the ascx sets the tag name, <my:FormFields  …. />  In the namespace one it will be one of the objects in the dll, <cc1:Accordian … />.

Please see the MSDN site for Walkthrough: Creating Reusable Elements with ASP.NET User Controls.

Are Leading Zeros Required For Range Validation

The quick answer to this, NO. If you have a range validator that appears to want you to have leading zeros chances are you made a bone headed mistake. Much like I did just now. I forgot to set the Type property of my range validator which gave me some interesting results.

The below range validator required me to enter 01 – 100. Another odd thing about this mistake was that a value of 43, had to be 043.

<asp:RangeValidator ID="valrngTest" runat="server" ControlToValidate="txtTest" 
    Text="*" ErrorMessage="Test must be between 0 - 100." SetFocusOnError="true" 
    Display="Dynamic" MinimumValue="0" MaximumValue="100">

This one worked just as you would expect. Notice the addition of the Type=”Integer”.

<asp:RangeValidator ID="valrngTest" runat="server" ControlToValidate="txtTest" 
    Text="*" ErrorMessage="Test must be between 0 - 100." SetFocusOnError="true" 
    Display="Dynamic" MinimumValue="0" MaximumValue="100" Type="Integer">

Custom Validation Controls

Custom validation controls are a nice tool that you can use when the standard validation controls just won’t do the job.  Most examples I found out there only addressed the server side validation aspect.  Don’t get me wrong, you can do that, just use server side, but ideally you want to do both.

Trust me though, if you only want to do one side, do server side.  You will want to always do server side as a minimum just in case the user has javascript disabled or something.  Server side only is kind of old school but it gets the job done very reliably.

Read the rest of this entry

Encrypt That ViewState!

Something that is probably over looked from time to time unless you are just always that secure is encrypting your ViewState.  While SSL may prevent some things you will may want to make it a bit harder for an end user to see the information stored in ViewState.

Luckily this is very simple.  You can set it in the @Page directive (<%@ Page Language=”C#” ViewStateEncryptionMode=”Always” %>) or in the web.config file (<pages viewStateEncryptionMode=”Always” />).

Of course, like many things .NET there are many ways to do this depending on your specific situation.  For that amount of detail you should read the ViewState Overview.