Web parts in ASP.NET

I thought of publishing this article showing how to play with Web parts in ASP.net. If you use VS 2005 you can try this out..

enjoy…..

 for this particular example, first you need to drag & drop a Webpart Manager control from the tool box. Then simply add a Webpart zone, so that you can drag the web parts between different Webpart zones.

And additioally I added a Webpart editor zone to allow to modify the web parts. finally simply add some controls to the Web parts that you ‘ve added so that you can view the different parts clearly.

So that’s it. Go ahead and copy the code below to the .cs file and ‘run’.using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

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

{

WebPartManager _wpmanager;

protected void Page_Load(object sender, EventArgs e)

{

}

void Page_Init(object sender, EventArgs e)

{

Page.InitComplete += new EventHandler(Init_Complete);

}

public void Init_Complete(object sender, System.EventArgs e)

{

_wpmanager = WebPartManager.GetCurrentWebPartManager(Page);

foreach (WebPartDisplayMode mymode in _wpmanager.SupportedDisplayModes)

{

string modeName = mymode.Name;

if (mymode.IsEnabled(_wpmanager))

{

ListItem myItem = new ListItem(modeName, modeName);

DropDownList1.Items.Add(myItem);

}

}

}

void Page_PreRender(object sender, EventArgs e)

{

ListItemCollection myList = DropDownList1.Items;

int sel_index = myList.IndexOf(myList.FindByText(_wpmanager.DisplayMode.Name));

DropDownList1.SelectedIndex = sel_index;

}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

{

string selectedItem = DropDownList1.SelectedValue;

WebPartDisplayMode _mode = _wpmanager.SupportedDisplayModes[selectedItem];

if (_mode != null)

_wpmanager.DisplayMode = _mode;

}

}

 So hope this code snippet become useful to you. If you have any comments please publish them..

Advertisements

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 )

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