ASP.NET C# Form Generator

27. July 2010

I decided to make a simple tool generate forms for ASP.NET.  The link to the tool is below.  If you'd like more of a background on the form keep reading. Otherwise, hope the tool helps you!

ASP.NET C# Form Generator

-----------------------------------------------------

Usually every website needs some type of input form. Whether that is for a contact page, suggested comments page, etc...  Because I find this to be a tedious task I decided to make a simple ASP.NET C# Form Generator.  Am I that lazy? Probably :)  I guess don't like wasting time when I don't need to.  To create a form I would always simply copy and paste a previous form and them modify as needed. As simple as that sounds, the modifying part is/was annoying. I would have to change the text and variable names as needed for the new form.

Now, with my simple form generator I don't have to worry about that anymore. I simply type in the name of the field I want and select what type of field it is.  I am not claiming this to be a tool that is used for advanced forms. Just simple and easy. 

ASP.NET C# Form Generator

As you enter in your field information, it will append the markup and code behind per field. Now I really only have to copy and paste and that's it. No modification needed.  It will also create a simple email message that you can use for whatever code you already have to send emails via your forms.

This tool is geared towards developers, as in the end it can be used for a simple form. If anything it can be used as a template to get started.  Either way I think you will find this saves you time.  Below is a screenshot of the tool.


If this doesn't serve your purpose...sorry :)  I needed something for myself right now to save me some time. However, feel free to drop comments or suggestions as what could be added.

Personally, I don't think I will ever make this tool advanced enough to where I would never have to write code for a form. Partially, because I don't mind writing code to insert to the database or writing code to send emails, because most likely, although similiar, each one will be different. However, this is a good start on implementing a form and getting rid of the tedious tasks.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

ASP.NET

Using ColorBox in ASP.NET

22. July 2010

I learned about this jquery lightbox called ColorBox via SitePoint's "jquery Novice to Ninja" e-book. I created just a small sample on how you can go about integrating it into an asp.net application.

Note: thanks SitePoint for the free e-book because of the world cup.

Anyway, this sample simply uses a DataList to create the elements needed for ColorBox to function properly.  This example provides a thumbnail preview of images before the user clicks on one to start navigating through them via ColorBox.

Note: this code is for demo only and other things could be done to clean it up and style the datalist better.

The concept is that you are filling an object such as a DataSet with the fields you need to make this happen. I will post the manual code I used to test this out in the code behind

First, here is a screenshot of the my Visual Studio solution.  I chose to contain all of the ColorBox files in a folder named ColorBox....go figure right?

 

Because I did this I had to edit the CSS file to point to the correct path for all images. In my case one of the lines looked like this:

#cboxOverlay{background:url(/ColorBox/images/overlay.png) 0 0 repeat;}

Notice the path in the url(). You will need to change all lines in the CSS file to their respective image paths as needed.

 

 

Second, here is the result from the code in the browser.


 

Next, here is the markup from the ASPX page.

Finally, here is the code behind to manually create the test data

DataSet ds = new DataSet();

protected void Page_Load(object sender, EventArgs e)
{
    BuildDataSet();
    BuildPhotoList();
}

private void BuildDataSet()
{
    ds.Tables.Add("Photos");
    ds.Tables[0].Columns.Add("ImageUrl");
    ds.Tables[0].Columns.Add("Description");

    DataRow dr1 = ds.Tables[0].NewRow();
    dr1["ImageUrl"] = "~/Images/homer.jpg";
    dr1["Description"] = "Your Description Here";            

    DataRow dr2 = ds.Tables[0].NewRow();
    dr2["ImageUrl"] = "~/Images/marylou.jpg";
    dr2["Description"] = "Your Description Here";

    DataRow dr3 = ds.Tables[0].NewRow();
    dr3["ImageUrl"] = "~/Images/ohoopee1.jpg";
    dr3["Description"] = "Your Description Here";

    DataRow dr4 = ds.Tables[0].NewRow();
    dr4["ImageUrl"] = "~/Images/ohoopee2.jpg";
    dr4["Description"] = "Your Description Here";

    DataRow dr5 = ds.Tables[0].NewRow();
    dr5["ImageUrl"] = "~/Images/ohoopee3.jpg";
    dr5["Description"] = "Your Description Here";

    DataRow dr6 = ds.Tables[0].NewRow();
    dr6["ImageUrl"] = "~/Images/homer.jpg";
    dr6["Description"] = "Your Description Here";

    DataRow dr7 = ds.Tables[0].NewRow();
    dr7["ImageUrl"] = "~/Images/marylou.jpg";
    dr7["Description"] = "Your Description Here";

    DataRow dr8 = ds.Tables[0].NewRow();
    dr8["ImageUrl"] = "~/Images/ohoopee1.jpg";
    dr8["Description"] = "Your Description Here";

    DataRow dr9 = ds.Tables[0].NewRow();
    dr9["ImageUrl"] = "~/Images/ohoopee2.jpg";
    dr9["Description"] = "Your Description Here";

    ds.Tables[0].Rows.Add(dr1);
    ds.Tables[0].Rows.Add(dr2);
    ds.Tables[0].Rows.Add(dr3);
    ds.Tables[0].Rows.Add(dr4);
    ds.Tables[0].Rows.Add(dr5);
    ds.Tables[0].Rows.Add(dr6);
    ds.Tables[0].Rows.Add(dr7);
    ds.Tables[0].Rows.Add(dr8);
    ds.Tables[0].Rows.Add(dr9);
}

private void BuildPhotoList()
{
    dlColorBox.DataSource = ds;
    dlColorBox.DataBind();
}

 

And that's about it. This should get you started with using ColorBox in an asp.net application without having to statically have all of the elements on the page at design time.

Happy Coding!

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

ASP.NET

Default Route in ASP.NET 4 URL Routing

21. July 2010


I was playing around trying to learn URL Routing in ASP.NET 4. I wanted to find a way to have a wildcard or default route to handle any page ending in .ASPX.

I originally came across this post on stackoverflow

http://stackoverflow.com/questions/2704338/asp-net-4-0-web-forms-routing-default-wildcard-route

However, it didn't seem to work (or at least I couldn't get it to work properly).

So I took the concept of that post and created a default route handler that seems to be working OK.

Note: I'm just now learning this, so if you have a better reference for those reading this please leave a link in the comments or leave a suggestion for a better way :)

Here was the goal I was trying to accomplish

http://localhost:18849/Default -> http://localhost/Default.aspx
http://localhost:18849/About -> http://localhost/About.aspx
http://localhost:18849/Contact -> http://localhost/Contact.aspx
http://localhost:18849/Dir1/Page1 -> http://localhost/Dir1/Page1.aspx

Here is a screenshot of my test solution in Visual Studio 2010

 

 

Now for what I did.

First thing I did was add a reference to System.Web.Routing

Next, you need to add code to the global.asax file to register your routes. My resulting Global.asax.cs page looked like this

void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            RegisterRoutes(RouteTable.Routes);

        }

        void RegisterRoutes(RouteCollection routes)
        {
            routes.Ignore("{resource}.axd/{*pathInfo}");
                     
            //TODO: Rest of known routes here

            //Default route
            routes.Add(new Route("{*value}", new DefaultRouteHandler()));

        }



You'll notice a class called DefaultRouteHandler().  This is class I created to handle the route.

So you'll want to add a new class to your solution called DefaultRouteHandler.cs.  This class will implement the Interface IHttpHandler

public class DefaultRouteHandler: IRouteHandler
    {
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            var pageUrl = requestContext.HttpContext.Request.Path + ".aspx";
            pageUrl = string.Format("~{0}", pageUrl);

            var page = new object();

            try
            {
                page = BuildManager.CreateInstanceFromVirtualPath(pageUrl, typeof(Page));

                if (page != null)
                {
                    //Set the forms postback url to the route
                    var webForm = page as Page;
                    if (webForm != null)
                        webForm.Load += delegate
                        {
                            webForm.Form.Action =
                            requestContext.HttpContext.Request.RawUrl;
                        };
                }

                return page as IHttpHandler;
            }
            catch (Exception ex)
            {
                page = BuildManager.CreateInstanceFromVirtualPath("~/NotFound.aspx", typeof(Page));

                return page as IHttpHandler;
            }
            
        }
    }



You'll see in the code above I grab the Path of the request and append .aspx to it. This is because if someone types in http://localhost:18849/Contact we

need to add the .aspx to it so we can use it in the CreateInstanceFromVirtualPath method.

I wrapped this in a try-catch method because if the CreateInstanceFromVirtualPath failed, then we can redirect to our basic NotFound.aspx page.


With this code I have accomplished what I set out to do by having a route for my .aspx pages that don't do anything special.


Other References:

ASP.NET.4GuysFromRolla.com: URL Routing in ASP.NET 4.0

http://www.4guysfromrolla.com/articles/012710-1.aspx


Walkthrough: Using ASP.NET Routing in a Web Forms Application

http://msdn.microsoft.com/en-us/library/dd329551.aspx

How to: Define Routes for Web Forms Applications

http://msdn.microsoft.com/en-us/library/cc668177.aspx

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

ASP.NET