Chainable control creation with extension methods

30. December 2009

Many times one need to create controls and add them to an ASP.NET page/control from code. Sharepoint is the master example when this need is more somewhat of a rule than exception.

Looking at the dull Sharepoint webpart code sample at MSDN (in my opinion) show the tedious task of constantly creating, configuring and adding controls to other control containers.

Knowing that Jquery has the abillity to chain methods one after another, and .NET’s abillity to write extension methods the ingridients are set up for some .NET chained method extension voodo ;)

About the sample

In this simple demo I want to show the same pattern for creating a simple UI with first creating control in the same manner as the MSDN example, and secondly with extension methods, after the demo I will show/talk about the solution.

“Msdn-style”

using System;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page {

    protected Button btnSubmit;
    protected TextBox txtUsername;

    protected void Page_Load(object sender, EventArgs e) {
        var t = new HtmlTable();
        plcControlContainer.Controls.Add(t);

        // row1
        var r = new HtmlTableRow();
        t.Rows.Add(r);

        var c = new HtmlTableCell(); // cell1
        r.Cells.Add(c);

        var ctrl = new Label();
        // set properties
        ctrl.Text = "Username";
        ctrl.ToolTip = "your selected username";
        c.Controls.Add(ctrl);

        c = new HtmlTableCell(); // cell2
        r.Cells.Add(c);

        txtUsername = new TextBox();
        c.Controls.Add(txtUsername);


        // row 2
        r = new HtmlTableRow();
        t.Rows.Add(r);

        c = new HtmlTableCell(); // cell1
        r.Cells.Add(c);

        c = new HtmlTableCell(); // cell2
        r.Cells.Add(c);

        btnSubmit = new Button();
        // set properties
        btnSubmit.Text = "Submit";
        btnSubmit.CssClass = "submit";
        btnSubmit.Click += new EventHandler(btnSubmit_Click);
        c.Controls.Add(btnSubmit);
    }
}

More...

ASP.NET, Extension methods, SharePoint , , , ,

The great Form lie

7. December 2009


The "lie"

In Asp.Net development many web form developers tend to think that a Form runat server is a necessity...
Several tutorials, bloggers tend to suggest that there must be a form (or there could be no .aspx page)

This is NOT the case, and I will show/prove it in numerous ways to rid yourself of this "lie"/misconception.

So what is a form (really) in (x)html

W3Scool has the following to tell.
There are 2 ways of sending data from a form, get and post. In Standard (x)Html one can have 0..to many forms.

There is no form

Just as in the movie "The Matrix" you must understand that there is no spoon, or Form runat server. So just because the Visual Studio template show a default form runat server does not mean one HAVE to use it, (the same can be said about the doctag!), in context it's just a text template that one can use as is or change either after creation OR the template itself.
Grasp "there is no form" and the journey  towards a more free mind with asp.net webforms development has begun.

 

More...

ASP.NET ,