Visual WebPart Ascx CS Code
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Data;
namespace TestVisualWebPart1.VisualWebPart1
{
public partial class VisualWebPart1UserControl : UserControl
{
DataTable dt = new DataTable();
DataSet dset = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
sgvTest.DataSource = getData();
sgvTest.DataBind();
}
{
public partial class VisualWebPart1UserControl : UserControl
{
DataTable dt = new DataTable();
DataSet dset = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
sgvTest.DataSource = getData();
sgvTest.DataBind();
}
}
public DataSet getData()
{
try
{
dt.Columns.Add("Title");
dt.Columns.Add("Status");
dt.Columns.Add("Priority");
SPWeb currentWeb = SPContext.Current.Web;
SPList lstTasks = currentWeb.Lists["Tasks"];
SPListItemCollection myColl = lstTasks.Items;
if (myColl.Count > 0)
{
foreach (SPListItem item in myColl)
{
string strTitle = item["Title"].ToString();
string strStatus = string.Empty;
string strPriority = string.Empty;
if (item["Status"].ToString() != "")
{
strStatus = item["Status"].ToString();
}
else
{
strStatus = "DNA";
}
if (item["Priority"].ToString() != "")
{
strPriority = item["Priority"].ToString();
}
else
{
strPriority = "DNA";
}
DataRow drow = dt.NewRow();
drow["Title"] = strTitle;
drow["Status"] = strStatus;
drow["Priority"] = strPriority;
dt.Rows.Add(drow);
}
}
dset.Tables.Clear();
dset.Tables.Add(dt);
return dset;
}
catch (Exception ex)
{
lblException.Text = ex.ToString();
return null;
}
}
}
}
Visual WebPart Ascx Code
public DataSet getData()
{
try
{
dt.Columns.Add("Title");
dt.Columns.Add("Status");
dt.Columns.Add("Priority");
SPWeb currentWeb = SPContext.Current.Web;
SPList lstTasks = currentWeb.Lists["Tasks"];
SPListItemCollection myColl = lstTasks.Items;
if (myColl.Count > 0)
{
foreach (SPListItem item in myColl)
{
string strTitle = item["Title"].ToString();
string strStatus = string.Empty;
string strPriority = string.Empty;
if (item["Status"].ToString() != "")
{
strStatus = item["Status"].ToString();
}
else
{
strStatus = "DNA";
}
if (item["Priority"].ToString() != "")
{
strPriority = item["Priority"].ToString();
}
else
{
strPriority = "DNA";
}
DataRow drow = dt.NewRow();
drow["Title"] = strTitle;
drow["Status"] = strStatus;
drow["Priority"] = strPriority;
dt.Rows.Add(drow);
}
}
dset.Tables.Clear();
dset.Tables.Add(dt);
return dset;
}
catch (Exception ex)
{
lblException.Text = ex.ToString();
return null;
}
}
}
}
Visual WebPart Ascx Code
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="VisualWebPart1UserControl.ascx.cs" Inherits="TestVisualWebPart1.VisualWebPart1.VisualWebPart1UserControl" %>
<table width="100%">
<tr>
<td>
<asp:Label ID="lblException" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
<SharePoint:SPGridView ID="sgvTest" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:BoundField DataField="Title" HeaderText="Title" />
<asp:BoundField DataField="Status" HeaderText="Status" />
<asp:BoundField DataField="Priority" HeaderText="Priority" />
</Columns>
</SharePoint:SPGridView>
</td>
</tr>
</table>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="VisualWebPart1UserControl.ascx.cs" Inherits="TestVisualWebPart1.VisualWebPart1.VisualWebPart1UserControl" %>
<table width="100%">
<tr>
<td>
<asp:Label ID="lblException" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
<SharePoint:SPGridView ID="sgvTest" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:BoundField DataField="Title" HeaderText="Title" />
<asp:BoundField DataField="Status" HeaderText="Status" />
<asp:BoundField DataField="Priority" HeaderText="Priority" />
</Columns>
</SharePoint:SPGridView>
</td>
</tr>
</table>
No comments:
Post a Comment