<h1>First Visual Webpart</h1>
<table>
<tr>
<td colspan="2">
<SharePoint:SPGridView ID="gvICSSDocuments" runat="server" AutoGenerateColumns="false" Width="850px">
<RowStyle BackColor="#D0D8E8" Height="30px" HorizontalAlign="Left"/>
<AlternatingRowStyle BackColor="#E9EDF4" Height="30px" HorizontalAlign="Left"/>
<HeaderStyle HorizontalAlign="Left" CssClass="ms-viewheadertr" />
<Columns>
<asp:TemplateField HeaderText="Title" ControlStyle-Width="250px" SortExpression="Title" HeaderStyle-CssClass="ms-viewheadertr">
<ItemTemplate>
<asp:Label ID="lblTitle" runat="server" Text='<%# Eval("Title") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<asp:Label ID="lblNoAccess" Text="No documents available" runat="server" CssClass="emptyDataLabel"></asp:Label>
</EmptyDataTemplate>
</SharePoint:SPGridView><br><br>
</td>
</tr>
<tr>
<td>First Name</td><td>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Last Name</td><td>
<asp:DropDownList ID ="ddlDep" runat="server"></asp:DropDownList></td>
</tr>
<tr><td> </td><td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
</td>
</tr>
</table>
______________________________________________________________________________________
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.Collections.Generic;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;
namespace Sharepoint2010Samples.SampleVisualWP
{
public partial class SampleVisualWPUserControl :
UserControl
{
//public
wbDocumentViewer PropertyObj { get; set; }
ObjectDataSource gridDS = null;
protected void Page_Load(object sender, EventArgs e)
{
try
{
//docLibrary
= PropertyObj.DocLibrary;
//managerGroup
= PropertyObj.ManagerGroup;
//viewerGroup
= PropertyObj.ViewerGroup;
//manageDocPageURL
= PropertyObj.ManageDocumentPageURL;
const string DATASOURCEID = "gridDS";
gridDS = new ObjectDataSource();
gridDS.ID = DATASOURCEID;
gridDS.SelectMethod = "SelectData";
gridDS.TypeName = this.GetType().AssemblyQualifiedName;
gridDS.ObjectCreating += new ObjectDataSourceObjectEventHandler(gridDS_ObjectCreating);
this.Controls.Add(gridDS);
BindDocuments();
}
catch (Exception ex)
{
SPDiagnosticsService diagnosticsService = SPDiagnosticsService.Local;
SPDiagnosticsCategory cat = diagnosticsService.Areas["SharePoint Foundation"].Categories["Unknown"];
diagnosticsService.WriteTrace(1, cat, TraceSeverity.Medium, ex.StackTrace, cat.Name, cat.Area.Name);
SPUtility.TransferToErrorPage("Some
Error occured, Please try after some time. <br/> If problem persists,
contact your adminstrator");
}
}
private void BindDocuments()
{
gvICSSDocuments.AllowPaging = true;
//
Sorting
gvICSSDocuments.AllowSorting = true;
//allow
Filtering
gvICSSDocuments.FilterDataFields = "ID,Title";
gvICSSDocuments.FilteredDataSourcePropertyName = "FilterExpression";
gvICSSDocuments.FilteredDataSourcePropertyFormat = "{1} = '{0}'";
gvICSSDocuments.Sorting += new GridViewSortEventHandler(gvDocuments_Sorting);
gvICSSDocuments.PageIndexChanging
+= new GridViewPageEventHandler(gvDocuments_PageIndexChanging);
//For
Filtering
gridDS.Filtering += new ObjectDataSourceFilteringEventHandler(gridDS_Filtering);
gvICSSDocuments.AutoGenerateColumns
= false;
gvICSSDocuments.AllowFiltering = true;
gvICSSDocuments.PagerTemplate = null;
gvICSSDocuments.PageSize = 10;
gvICSSDocuments.DataSourceID =
gridDS.ID;
gvICSSDocuments.DataBind();
}
public DataTable SelectData()
{
DataTable dataSource = new DataTable();
try
{
SPSite site = SPContext.Current.Web.Site;
SPWeb web = site.OpenWeb();
SPList lstICSSDocuments = web.Lists["DepList"];
SPQuery query = new SPQuery();
//
IEnumerable<SPListItem> lstItemICSSDocuments =
lstICSSDocuments.GetItems(query).OfType<SPListItem>();
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite spSite = new SPSite("http://sriram-pc:6666/Hrmx/"))
{
spSite.AllowUnsafeUpdates = false;
using (SPWeb spWeb = spSite.OpenWeb())
{
spWeb.AllowUnsafeUpdates = false;
SPList List =
spWeb.Lists.TryGetList("DepList");
if (List != null)
{
dataSource =
List.Items.GetDataTable();
}
}
}
});
web.Dispose();
}
catch (Exception ex)
{
SPDiagnosticsService diagnosticsService = SPDiagnosticsService.Local;
SPDiagnosticsCategory cat = diagnosticsService.Areas["SharePoint Foundation"].Categories["Unknown"];
diagnosticsService.WriteTrace(1, cat, TraceSeverity.Medium, ex.StackTrace, cat.Name, cat.Area.Name);
SPUtility.TransferToErrorPage("Some
Error occured, Please try after some time. <br/> If problem persists,
contact your adminstrator");
}
return dataSource;
}
void gridDS_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
e.ObjectInstance = this;
}
void gvDocuments_Sorting(object sender, GridViewSortEventArgs e)
{
string lastExpression = "";
if (ViewState["SortExpression"] != null)
lastExpression = ViewState["SortExpression"].ToString();
string lastDirection = "asc";
if (ViewState["SortDirection"] != null)
lastDirection = ViewState["SortDirection"].ToString();
string newDirection = string.Empty;
if (e.SortExpression == lastExpression)
{
e.SortDirection =
(lastDirection == "asc") ? System.Web.UI.WebControls.SortDirection.Descending
: System.Web.UI.WebControls.SortDirection.Ascending;
}
newDirection = (e.SortDirection ==
System.Web.UI.WebControls.SortDirection.Descending) ? "desc" : "asc";
ViewState["SortExpression"]
= e.SortExpression;
ViewState["SortDirection"]
= newDirection;
gvICSSDocuments.DataBind();
//For
Filter
if (ViewState["FilterExpression"] != null)
{
gridDS.FilterExpression = (string)ViewState["FilterExpression"];
}
}
void gvDocuments_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvICSSDocuments.PageIndex =
e.NewPageIndex;
gvICSSDocuments.DataSourceID =
gridDS.ID;
gvICSSDocuments.DataBind();
}
//For
Filtering
private void gridDS_Filtering(object sender, ObjectDataSourceFilteringEventArgs e)
{
ViewState["FilterExpression"]
= ((ObjectDataSourceView)sender).FilterExpression;
}
protected sealed override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
if (Context.Request.Form["__EVENTARGUMENT"] != null &&
Context.Request.Form["__EVENTARGUMENT"].EndsWith("__ClearFilter__"))
{
//
Clear FilterExpression
ViewState.Remove("FilterExpression");
}
}
}
}