In a scenario I have to create WebPart which shows a SPGridView in which there will be a Link column which is to be binded from a list column (Single Line Text type).I have googled much for it but could not find it. Under the guidance of Sourabh Khatri I have been able to come up with a solution which helps me to achieve my aim. I divided up it in three parts
1.WebPart
2.DAL
3.Entities
FILE#1: WebPart (TestWebPart.cs) (In this file I have designed the webpart, SPGridView and supporting class to make link column)
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace WebPart
{
public class TestWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
protected override void CreateChildControls()
{
BoundField objField;
SPGridView objGrid;
TemplateField objTemplateField;
Table objTable;
TableCell objTableCell;
TableRow objTableRow;
ListInfo objListInfo = ListHandler.GetAllListItems();
try
{
if (objListInfo!= null && objListInfo.DataList != null)
{
objTable = new Table();
objTableRow = new TableRow();
objTableCell = new TableCell();
objGrid = new SPGridView();
objGrid.DataSource = objListInfo.DataList;
#region Styling the Grid
objGrid.AutoGenerateColumns = false;
objTableCell.CssClass = "gridborder";
objGrid.RowStyle.CssClass = "gridrow";
objGrid.AlternatingRowStyle.CssClass = "grid_alternaterow";
objTable.Width = Unit.Percentage(100);
#endregion
#region Creating Grid Columns
objTemplateField = new TemplateField();
objTemplateField.HeaderTemplate = new
GridViewRowTemplate(DataControlRowType.Header, "ColumnHeaderName");
objTemplateField.HeaderStyle.CssClass = "header";
objTemplateField.ItemTemplate = new
GridViewRowTemplate(DataControlRowType.DataRow, "ColumnName");
objGrid.Columns.Add(objTemplateField);
// A NonLink Field
objField = new BoundField();
objField.DataField = "Description";
objField.HeaderText = "Description";
objField.HeaderStyle.CssClass = "header";
objGrid.Columns.Add(objField);
#endregion
objGrid.DataBind();
objTableCell.Controls.Add(objGrid);
objTableRow.Cells.Add(objTableCell);
objTable.Rows.Add(objTableRow);
this.Controls.Add(objTable);
}
}
catch (Exception e)
{
}
base.CreateChildControls();
}
public class GridViewRowTemplate : ITemplate
{
private DataControlRowType templateType;
private string columnName;
public GridViewRowTemplate(DataControlRowType templateType,string columnName)
{
this.columnName = columnName;
this.templateType = templateType;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (templateType)
{
case DataControlRowType.Header:
Literal objLiteral = new Literal();
objLiteral.Text = "" + columnName + "";
container.Controls.Add(objLiteral);
break;
case DataControlRowType.DataRow:
LinkButton linkButton = new LinkButton();
linkButton.DataBinding += new EventHandler(linkButton_DataBinding);
container.Controls.Add(linkButton);
break;
}
}
void linkButton_DataBinding(object sender, EventArgs e)
{
string reportName = string.Empty;
string ListItemName = string.Empty;
LinkButton linkBtn = (LinkButton)sender;
SPGridViewRow row = (SPGridViewRow)linkBtn.NamingContainer;
ListItemName= Convert.ToString(DataBinder.Eval(row.DataItem, " ListItemName "));
linkBtn.Text = ListItemName;
linkBtn.Attributes.Add("onclick", "javascript:alert(‘”+ListItemName+ ”’);”);
}
}
}
}
FILE#2:DAL(ListHandler.cs) (In this file I have read the list content to be shown in the SPGridView)
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Text;
namespace WebPart
{
public class ListHandler
{
public static ListInfo GetAllListItems ()
{
SPWeb objWeb = null;
SPListItemCollection objItems;
ListItemsInfo objListItemInfo = null;
System.IO.FileInfo objInfo;
try
{
objWeb = SPControl.GetContextWeb(HttpContext.Current).Site.RootWeb;
if (objWeb != null)
{
objItems = objWeb.Lists.TryGetList(“ListName”).Items;
if (objItems.Count > 0)
{
objListItemInfo = new ListItemsInfo ();
foreach (SPListItem objItem in objItems)
{
objListItemInfo.ListItems.Add(new ListInfo (objItem.Name, Convert.ToString(objItem[“ColumnName”]), Convert.ToString(objItem.Title)));
}
}
}
return objListItemInfo ;
}
else
{
throw new Exception("Cannot find the Web");
}
}
catch (Exception ex)
{
throw (ex);
}
}
}
}
FILE#3: Entities (ListInfo.cs) (This file is used for creating properties)
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Microsoft.SharePoint;
using System.Collections.Generic;
namespace WebPart
{
public class ListInfo
{
public string ListItemName { get; set; }
public string Description { get; set; }
public string ListItemTitle { get; set; }
public ListInfo ()
{
}
public ListInfo (string ListItemName, string Description, string ListItemTitle)
{
this.ListItemName = ListItemName;
this.Description = Description;
this.ListItemTitle = ListItemTitle;
}
}
public class ListItemsInfo
{
public ListItemsInfo()
{
ListItems = new List<>();
}
public List ListItems
{
get;
set;
}
}
}
1.WebPart
2.DAL
3.Entities
FILE#1: WebPart (TestWebPart.cs) (In this file I have designed the webpart, SPGridView and supporting class to make link column)
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace WebPart
{
public class TestWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
protected override void CreateChildControls()
{
BoundField objField;
SPGridView objGrid;
TemplateField objTemplateField;
Table objTable;
TableCell objTableCell;
TableRow objTableRow;
ListInfo objListInfo = ListHandler.GetAllListItems();
try
{
if (objListInfo!= null && objListInfo.DataList != null)
{
objTable = new Table();
objTableRow = new TableRow();
objTableCell = new TableCell();
objGrid = new SPGridView();
objGrid.DataSource = objListInfo.DataList;
#region Styling the Grid
objGrid.AutoGenerateColumns = false;
objTableCell.CssClass = "gridborder";
objGrid.RowStyle.CssClass = "gridrow";
objGrid.AlternatingRowStyle.CssClass = "grid_alternaterow";
objTable.Width = Unit.Percentage(100);
#endregion
#region Creating Grid Columns
objTemplateField = new TemplateField();
objTemplateField.HeaderTemplate = new
GridViewRowTemplate(DataControlRowType.Header, "ColumnHeaderName");
objTemplateField.HeaderStyle.CssClass = "header";
objTemplateField.ItemTemplate = new
GridViewRowTemplate(DataControlRowType.DataRow, "ColumnName");
objGrid.Columns.Add(objTemplateField);
// A NonLink Field
objField = new BoundField();
objField.DataField = "Description";
objField.HeaderText = "Description";
objField.HeaderStyle.CssClass = "header";
objGrid.Columns.Add(objField);
#endregion
objGrid.DataBind();
objTableCell.Controls.Add(objGrid);
objTableRow.Cells.Add(objTableCell);
objTable.Rows.Add(objTableRow);
this.Controls.Add(objTable);
}
}
catch (Exception e)
{
}
base.CreateChildControls();
}
public class GridViewRowTemplate : ITemplate
{
private DataControlRowType templateType;
private string columnName;
public GridViewRowTemplate(DataControlRowType templateType,string columnName)
{
this.columnName = columnName;
this.templateType = templateType;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (templateType)
{
case DataControlRowType.Header:
Literal objLiteral = new Literal();
objLiteral.Text = "" + columnName + "";
container.Controls.Add(objLiteral);
break;
case DataControlRowType.DataRow:
LinkButton linkButton = new LinkButton();
linkButton.DataBinding += new EventHandler(linkButton_DataBinding);
container.Controls.Add(linkButton);
break;
}
}
void linkButton_DataBinding(object sender, EventArgs e)
{
string reportName = string.Empty;
string ListItemName = string.Empty;
LinkButton linkBtn = (LinkButton)sender;
SPGridViewRow row = (SPGridViewRow)linkBtn.NamingContainer;
ListItemName= Convert.ToString(DataBinder.Eval(row.DataItem, " ListItemName "));
linkBtn.Text = ListItemName;
linkBtn.Attributes.Add("onclick", "javascript:alert(‘”+ListItemName+ ”’);”);
}
}
}
}
FILE#2:DAL(ListHandler.cs) (In this file I have read the list content to be shown in the SPGridView)
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Text;
namespace WebPart
{
public class ListHandler
{
public static ListInfo GetAllListItems ()
{
SPWeb objWeb = null;
SPListItemCollection objItems;
ListItemsInfo objListItemInfo = null;
System.IO.FileInfo objInfo;
try
{
objWeb = SPControl.GetContextWeb(HttpContext.Current).Site.RootWeb;
if (objWeb != null)
{
objItems = objWeb.Lists.TryGetList(“ListName”).Items;
if (objItems.Count > 0)
{
objListItemInfo = new ListItemsInfo ();
foreach (SPListItem objItem in objItems)
{
objListItemInfo.ListItems.Add(new ListInfo (objItem.Name, Convert.ToString(objItem[“ColumnName”]), Convert.ToString(objItem.Title)));
}
}
}
return objListItemInfo ;
}
else
{
throw new Exception("Cannot find the Web");
}
}
catch (Exception ex)
{
throw (ex);
}
}
}
}
FILE#3: Entities (ListInfo.cs) (This file is used for creating properties)
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Microsoft.SharePoint;
using System.Collections.Generic;
namespace WebPart
{
public class ListInfo
{
public string ListItemName { get; set; }
public string Description { get; set; }
public string ListItemTitle { get; set; }
public ListInfo ()
{
}
public ListInfo (string ListItemName, string Description, string ListItemTitle)
{
this.ListItemName = ListItemName;
this.Description = Description;
this.ListItemTitle = ListItemTitle;
}
}
public class ListItemsInfo
{
public ListItemsInfo()
{
ListItems = new List<>();
}
public List
{
get;
set;
}
}
}
No comments:
Post a Comment