anima nearly had it!
webpart: has a .cs and you programmatically add the controls within the creatchildcontrols
visual webpart: its also has the .cs but rather it inherits the visual aspect (UI) from a ascx page that also has its own .cs
visual webparts are to seperate the design aspect from the codeing aspect, You could go ahead and change the ascx without having to rebuild the project... thats the principle behind the visual webpart. easier to maintain for branding ect without having to open the project within vs and rebuilding
either way is up to you! i dont quite get what you mean ;) either way in sharepoint 2010 or 2013 using visual studios 2010 you can add to the feature an event handler from there sharepoint auto adds the feature adding and removing!
to add a feature, within the project on the right hand side look for feature, within feature look for feature.feature, right click and click add event reciver!
its that simple!
a step by step guide
http://msdn.microsoft.com/en-us/library/vstudio/ee231604(v=vs.100).aspx
to add a list in a visual webpart you need to use SPGridview or Gridview its up to you!
open your ascx page and add the following code:
<div style=" position:relative">
<asp:GridView ID="grid" runat="server" AutoGenerateColumns="false" Width="95%" GridLines="None" AllowPaging="true">
<Columns>
<asp:BoundField DataField="ID" HeaderText="Request ref" />
<asp:BoundField DataField="Title" HeaderText="Title" />
<asp:BoundField DataField="Age" HeaderText="Business area" />
<asp:BoundField DataField="Status" HeaderText="Status" />
</Columns>
</asp:GridView>
the above is getting 4 columns of data, ID, title, age and status from my list:
to bind the list to the gridview to get the data that is simple! now goto your ascx.cs, double click. within the onload event add this:
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb web = site.OpenWeb("mySubSite"))
{
SPList list = web.Lists["USER INPUT"];
SPListItemCollection items = list.Items;
grid.DataSource = items.GetDataTable();
grid.DataBind();
}
}
the above is calling the site from the current url, its then access the subsite mySubSite to give you an understanding:
site subsite
http://thisismysite.com/mySubSite
i made a custom list in sharepoint and called it USER INPUT, so thats the name of the list i use to call it and bind to the gridview!
running this it will show 4 columns in a grid. You could have many columns in a list, depending on what you want to show you would amend the ascx accordingly:
say iv added another column it would look like this
<asp:BoundField DataField="ID" HeaderText="Request ref" />
<asp:BoundField DataField="Title" HeaderText="Title" />
<asp:BoundField DataField="Age" HeaderText="Business area" />
<asp:BoundField DataField="Status" HeaderText="Status" />
<asp:BoundField DataField="age" HeaderText="age" />
now to know whats going on above, the DataField is the name of the field that is located within the list, HeaderText is what the column is going to be called in your webpart
i could do the following:
<asp:BoundField DataField="ID" HeaderText="this is my id" />
instead of
<asp:BoundField DataField="ID" HeaderText="Request ref" />
instead of visual webpart and going the webpart route:
within the creatchildcontrols of your .cs class:
protected override void CreateChildControls()
{
if (!this.Page.IsPostBack)
{
//get the site from the current context
SPSite site = SPContext.Current.Site;
//get the root web
SPWeb web = site.RootWeb;
//query the list with the default view
SPQuery query = new SPQuery();
query.ViewXml = "<View/>";
SPList list = web.Lists.TryGetList("USER INPUT");
//get all the list items within the list
SPListItemCollection items = list.GetItems(query);
//create a grid view control
GridView gv = new GridView();
//get the datatable from SPListItemCollection into GridView
gv.DataSource = items.GetDataTable();
//bind the datatable
gv.DataBind();
//add the GridView control to the page
this.Controls.Add(gv);
}
}
the above will bind the whole list!
to do as iv done in the ascx example you need to add some extra stuff:
protected override void CreateChildControls()
{
if (!this.Page.IsPostBack)
{
//get the site from the current context
SPSite site = SPContext.Current.Site;
//get the root web
SPWeb web = site.RootWeb;
//query the list with the default view
SPQuery query = new SPQuery();
query.ViewXml = "<View/>";
SPList list = web.Lists.TryGetList("USER INPUT");
//get all the list items within the list
SPListItemCollection items = list.GetItems(query);
//create a grid view control
GridView gv = new GridView();
//get the datatable from SPListItemCollection into GridView
gv.DataSource = items.GetDataTable();
//bind the datatable
gv.AutoGenerateColumns = false;
gv.Columns.Add(new BoundField { DataField = "ID", HeaderText = "Request ref" });
gv.Columns.Add(new BoundField { DataField = "Title", HeaderText = "Title" });
gv.Columns.Add(new BoundField { DataField = "age", HeaderText = "age" });
gv.Columns.Add(new BoundField { DataField = "Status", HeaderText = "Status" });
gv.DataBind();
//add the GridView control to the page
this.Controls.Add(gv);
}
}
i dont know if you notice but what i added what the columns myself and said not to auto generate the columns:
gv.AutoGenerateColumns = false;
gv.Columns.Add(new BoundField { DataField = "ID", HeaderText = "Request ref" });
gv.Columns.Add(new BoundField { DataField = "Title", HeaderText = "Title" });
gv.Columns.Add(new BoundField { DataField = "age", HeaderText = "age" });
gv.Columns.Add(new BoundField { DataField = "Status", HeaderText = "Status" });
hope it shines some light ;)