A better web part with event handling: code

As Visual Studio 2008 doesn’t support a designer for developing Sharepoint web parts, we have to create all controls from scratch. We need to create a table having three rows with two columns (to hold six controls). Each of those controls must be added to each of those cells. Finally, the table must be added to page. The following explains how to do this in a step by step manner.

The first step is to create a table to hold all the controls. The following code does the same:


Dim tbl As New Table

Dim tRow1 As New TableRow

Dim tRow2 As New TableRow

Dim tRow3 As New TableRow


The above code creates a new table with three rows (rows are not added to table yet). Next, we need to create cells for each row.


Dim tR1C1 As New TableCell

Dim tR1C2 As New TableCell

Dim tR2C1 As New TableCell

Dim tR2C2 As New TableCell

Dim tR3C1 As New TableCell

Dim tR3C2 As New TableCell


Now we add the cells to their respective rows:









Then we add the rows to the table:






Next, we create instances of the controls, provide values to properties and add event handlers (if any):


Dim txtFirst As New TextBox

txtFirst.ID = “txtFirst”

Dim txtSecond As New TextBox

txtSecond.ID = “txtSecond”

Dim lblFirst As New Label

lblFirst.Text = “Enter first:”

Dim lblSecond As New Label

lblSecond.Text = “Enter second:”

Dim lblResult As New Label

lblResult.ID = “lblResult”


Dim btnSubmit As New Button

btnSubmit.Text = “Submit”

AddHandler btnSubmit.Click, AddressOf btnSubmit_click



Now we add controls to their respective cells:









Finally, we add the table to a page (say, web part):





The output of the web part should look like the following image:


About eagle081183

Passionate, Loyal
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s