Creating a simple web part with controls in SharePoint

In case when we need to add controls to our web part we need to override the CreateChildControls method of the base WebPart class along with Render method.

Let’s take a simple example for it,  a kind of sample calculator which shows sum of two numbers ]

For this we have added 2 labels, 3 textboxes and 1 button.

On Click of the button the sum of the values entered in textbox1 and textbox2 would be displayed in the textbox3.

SampleCalculatorWebPart
SampleCalculatorWebPart

Following is the code for the above web part.

namespace SampleCalculator

{

public class MyCalculator :WebPart

{

protected Label lblFirstValue;

protected Label lblSecondValue;

protected TextBox txtFirstValue;

protected TextBox txtSecondValue;

protected Button btnCalculate;

protected TextBox txtTotal;

// controls would be initialized and added here

protected override void CreateChildControls(){

lblFirstValue = new Label();

lblSecondValue = new Label();

txtFirstValue = new TextBox();

txtSecondValue = new TextBox();

btnCalculate = new Button();

txtTotal = new TextBox();

lblFirstValue.Text = “Enter First Value :”;

lblSecondValue.Text = “Enter Second Value :”;

btnCalculate.Text = “Calculate”;

// adding eventhandler for click of btnCalculate

btnCalculate.Click += new EventHandler(btnCalculate_Click);

this.Controls.Add(lblFirstValue);

this.Controls.Add(txtFirstValue);

this.Controls.Add(lblSecondValue);

this.Controls.Add(txtSecondValue);

this.Controls.Add(btnCalculate);

this.Controls.Add(txtTotal);

}

void btnCalculate_Click(object sender, EventArgs e){

int total = CalculateSum( Convert.ToInt32(txtFirstValue.Text), Convert.ToInt32(txtSecondValue.Text));

txtTotal.Text= total.ToString();

}

protected override void Render(HtmlTextWriter writer){

// for proper alignment of the controls added

// we can create a table

// <Table>

//<tr><td><td></tr>

//<tr><td><td></tr>

//<tr><td><td></tr>

//</Table>

writer.RenderBeginTag(HtmlTextWriterTag.Table);

writer.RenderBeginTag(HtmlTextWriterTag.Tr);

writer.RenderBeginTag(HtmlTextWriterTag.Td);

lblFirstValue.RenderControl(writer);

writer.RenderEndTag(); //td

writer.RenderBeginTag(HtmlTextWriterTag.Td);

txtFirstValue.RenderControl(writer);

writer.RenderEndTag(); //td

writer.RenderEndTag(); // tr


writer.RenderBeginTag(HtmlTextWriterTag.Tr);

writer.RenderBeginTag(HtmlTextWriterTag.Td);

lblSecondValue.RenderControl(writer);

writer.RenderEndTag(); //td

writer.RenderBeginTag(HtmlTextWriterTag.Td);

txtSecondValue.RenderControl(writer);

writer.RenderEndTag(); //td

writer.RenderEndTag(); // tr


writer.RenderBeginTag(HtmlTextWriterTag.Tr);

writer.RenderBeginTag(HtmlTextWriterTag.Td);

btnCalculate.RenderControl(writer);

writer.RenderEndTag(); //td

writer.RenderBeginTag(HtmlTextWriterTag.Td);

txtTotal.RenderControl(writer);

writer.RenderEndTag(); //td

writer.RenderEndTag(); // tr

writer.RenderEndTag();// table

}

private int CalculateSum(int a, int b){

return a + b;}

}

}

Put the following attribute in your assemblyinfo.cs file

[assembly: AllowPartiallyTrustedCallers]


Strong sign the assembly and install it in GAC.

Open the web.config of your site where you want this webpart

Make a safecontrol entry within the web.config for your webpart.

<SafeControl Assembly=SampleCalculator, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7c6ce78ef8448ffa Namespace=SampleCalculator TypeName=* Safe=True />

The name of the assembly,it’s version, culture and public key token information can be found by right clicking the assembly within gac and selecting properties.

Bye..


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

Share your thoughts

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s