Adding errors to a ValidationSummary

April 24, 2008 14:31 by Rob

Instead of having a custom <asp:Label> with the exception from the backend, you can inject a custom error message to the validationsummary of an asp.net page.

Of course, you could use a server-side validator, but that fires everytime you submit the page, and not only after a succesful post. A Custom Validator is therefore in the wrong place.

Here's what I came up with:

public class CustomValidationError : IValidator

{

private string _message;

public CustomValidationError(string message)

{

_message = message;

}

public string ErrorMessage

{

get { return _message; }set {}

}

public bool IsValid

{

get{ return false; }

set

{

}

}

public void Validate()

{

}

}

To add a custom message to the summary, just do this:

this.Validators.Add(new CustomValidationError("There is no backend!"));


Currently rated 3.5 by 2 people

  • Currently 3.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: ASP.Net | C#
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Three times a column

March 26, 2008 17:09 by Rob

For a client i needed a ASP.Net control that showed its contents in multiple columns. The CSS requirements forced me to develop a custom repeater for it, since the normal ASP.Net controls didn't suit me. I decided to share it with you, so here it is:

This templated databound control will collect items in multiple css div's when rendering. Provide a collection of css classes to indicate the columns (in this example, 3 divs). Rendering will be done from left to right, top to bottom while iterating over the div's.

+------++-------++-------+
| one  || two   || three |
| four || five  ||       |
+------++-------++-------+

[code] public class MultiColumnRepeater : Repeater
    {
        private string _columnClasses;

        /// <summary>
        /// Specifies the amount of columns and their css classes
        /// </summary>
        /// <example>
        /// To create 2 columns, specify "column_left,column_right".
        /// </example>
        public string ColumnClasses
        {
            get { return _columnClasses;  }
            set { _columnClasses = value;  }
        }

        protected override void RenderChildren(HtmlTextWriter writer)
        {
            //iterate over the different items.
            string[] classes = _columnClasses.Split(',');
            for (int column = 0; column < classes.Length; column++)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, classes[column]);
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
               
                //iterate over all children, and only write the ones for the current columns.
                int index = 0;
                foreach (Control control in this.Controls)
                {
                    if (index % classes.Length == column)
                    {
                        control.RenderControl(writer);
                    }
                    index++;
                }

                writer.RenderEndTag();
                writer.Write(writer.NewLine);
            }
        }
    } [/code]


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: ASP.Net | C#
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Why don't you use those expressions regularly?

January 21, 2008 15:45 by Rob
I found a very nice tutorial on Regular Expressions. If you're still a bit uncertain how it al works, or want to brush up on your RegEx skillz, check it out.

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: ASP.Net | C# | Software Engineering
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Boxing problems?

November 15, 2007 14:38 by Rob
Checkout this link which explains all your boxing needs. I especially like the Doctype list which shows what box model is used in IE6 and above. Yeah, I know, it's been in there for years, but why didn't anybody tell me sooner?

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: General | ASP.Net
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Health Monitoring in ASP.NET 2.0

October 12, 2007 16:22 by Rob

Now here's something interesting I didn't notice before: ASP.Net has health monitoring features built in. In Asp.net 1.1 I used to roll out our own code to catch web application errors and log them, but now they seem to be built in into the framework.

Since I don't like typing very much, here's the link to the full article on 4GuysFromRolla:

  • Health Monitoring Basics - explores the concepts and advantages of the Health Monitoring system and looks at logging events to a Microsoft SQL Server database.
  • Notifications via Email - looks at security-related events and shows how to alert an administrator to failed authentication attempts by "logging" events to email.
  • Raising Custom Events - learn how to create and raise custom Health Monitoring events.

  • Be the first to rate this post

    • Currently 0/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5
    Tags:
    Categories: ASP.Net | C#
    Actions: E-mail | Permalink | Comments (2) | Comment RSSRSS comment feed

    Strong-typed provisioning of custom lists in Sharepoint 2007

    October 11, 2007 16:05 by Rob

    In most of our latest Sharepoint projects we need a lot of customization. These customization generally mean that we develop webpparts and custom lists so that we use sharepoint as a sort of database. The list itself is rarely visible to end users and only to a extended sharepoint site that is used as a backoffice application.

    The development of the webparts go as follows:

    • For every webpart we develop we have:
      • The <webpart>.cs control
      • The <webpart>Control.asc User control
      • The <webpart>.wsp XML file that is used to package the webpart
      • A Test<webpart>.aspx file that is used to test the webpart standalone but is never deployed to the sharepoint application.
    • The webparts do not rely on sharepoint, but bind to strongly type objects and collection.
    • A Provider pattern is used for retrieving and updating of the data objects.

    This provider model allows us to test and verify functionality of the webparts without the need to upload/provision these webparts inside a Sharepoint hosted website. We all know this takes a lot of time. The provider is configured in the web.config of the webpart solution file.

    Here's a sample of one of these business objects:

    public class AppointmentObject
    {
      private string _description;

      public string Description
      {
        get { return _description; }
        set { _description = value; }
      }
    }

    The user control uses this class in codebehind like so:

    AppointmentCollection recentAppointments = ServiceProvider.GetRecentAppointments();

    RecentAppointments.DataSource  = recentAppointment;
    RecentAppointments.DataBind();

    You see this is basic ASP.Net stuff.

    I found this a pretty amazing thing; it cuts down on the time used for each development roundtrip, and it allows us to 'outsource' the development of the web parts to ASP.Net specialist who do not (yet) have the required Sharepoint knowledge. The knife cuts on two sides!

    This approach did have an enormous disadvantage however. The provider that implemented the mapping from the hard-coded business object to sharepoint needed a lot of custom code. It only paid of once the business object was used in multiple places. Another problem is that the code that provisioned the custom list was always a one-off piece of custom code. That code has NO relation to the business object.

    Now, my experiences with entitypool helped me in writing a library that solves all this:

    [SharepointList(Name="Appointments", ShowOnQuickLaunch=true)]
    [Description("Appointments"]
    public class AppointmentObject
    {
      private string _description;

      [SharepointTextField(Required=true,MaxLength=16)]
      public string Description
      {
        get { return _description; }
        set { _description = value; }
      }
    }

    To create the list in Sharepoint, all that is needed is the following statement:

    using(SPSite site = new SPSite("http://localhost:90"))
    {
      SharepointProvisioning.CreateList(site, typeof(AppointmentObject), true);
    }

    To retrieve all appointmentobjects we have a factory that converts a SPListItemCollection to AppointmentObject objects:

    ObjectFactory<AppointmentObject> factory = new ObjectFactory<AppointmentObject>();
    AppointmentObject object = factory.GetItem(spListItem);

    Easy? I think so.


    Currently rated 3.7 by 3 people

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

    SourceMetrics

    September 15, 2007 01:37 by Rob

    I'm here to tell you about my latest invention: SourceMetrics. It is a commandline tool to scan a source repository (actually just Visual Sourcesafe for now) and report a cool set of statistics about its contents.

    Currently, it reports:

    • Total number of files
    • Total number of bytes
    • Total number of lines
    • Total number of code lines
    • Total number of comment lines

    As this tool reports code as Xml, it will be very easy to integrate with build script and tools like CruiseControl. The effect of having some kind of time-related statistics are huge. As first beta, and more info will be posted lated on.


    Be the first to rate this post

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

    To Captcha or not to Captcha..

    August 13, 2007 20:38 by Rob

    I just saw this article about an invisible captcha on madskristensens' blog. The principle is amazingly simple.

    Although it has a small security hole it's rather cool and sufficient for most spambots.


    Be the first to rate this post

    • Currently 0/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5
    Tags:
    Categories: ASP.Net
    Actions: E-mail | Permalink | Comments (1) | Comment RSSRSS comment feed