Use Object Datastource to Bind SPGridView and Apply Filtering on It

Normally we bind SPgridView with datatable but SPGridView becomes null on page load after postback ,so to maintain its data "ObjectDatasource" is used.

You can also apply filtering by sending field names in "FilterDataFields" property on which fields you want filter.

It will work as DataFormWebpart.

Add below code in page init function :

odsDataSource.ID = "odsDataSource";

odsDataSource.SelectMethod = "GetDataTable";

var assemblyQualifiedName = GetType().AssemblyQualifiedName;

if (assemblyQualifiedName != null) odsDataSource.TypeName = assemblyQualifiedName;

odsDataSource.ObjectCreating += odsDataSource_ObjectCreating;

odsDataSource.ObjectDisposing += odsDataSource_ObjectDisposing;

odsDataSource.Filtering += GridViewDocumentMaster_Filtering;

Controls.Add(odsDataSource);

GridViewDocumentMaster.PagerTemplate = null;

GridViewDocumentMaster.AllowFiltering = true;

GridViewDocumentMaster.FilterDataFields = ",Id,DocumentTitle,DocumentNumber,,,";

GridViewDocumentMaster.FilteredDataSourcePropertyName = "FilterExpression";

GridViewDocumentMaster.FilteredDataSourcePropertyFormat = "{1} = '{0}'";

GridViewDocumentMaster.EnableViewState = false;

 
Then Add below functions Change Gridview Name

/// <summary>

/// Handles the Filtering event of the SPGridViewMappedDocs control.

/// </summary>

/// <param name="sender">The source of the event.</param>

/// <param name="e">The <see cref="System.Web.UI.WebControls.ObjectDataSourceFilteringEventArgs"/> instance containing the event data.</param>

/// <remarks></remarks>

private void GridViewDocumentMaster_Filtering(object sender, ObjectDataSourceFilteringEventArgs e)

{

ViewState["FilterExpression"] = ((ObjectDataSourceView)sender).FilterExpression;

}

 

/// <summary>

/// Handles the ObjectCreating event of the odsDataSource control.

/// </summary>

/// <param name="sender">The source of the event.</param>

/// <param name="e">The <see cref="System.Web.UI.WebControls.ObjectDataSourceEventArgs"/> instance containing the event data.</param>

/// <remarks></remarks>

private void odsDataSource1_ObjectCreating(object sender, ObjectDataSourceEventArgs e)

{

e.ObjectInstance = this;

}

 

/// <summary>

/// Handles the ObjectDisposing event of the odsDataSource control.

/// </summary>

/// <param name="sender">The source of the event.</param>

/// <param name="e">The <see cref="System.Web.UI.WebControls.ObjectDataSourceDisposingEventArgs"/> instance containing the event data.</param>

/// <remarks></remarks>

protected void odsDataSource1_ObjectDisposing(object sender, ObjectDataSourceDisposingEventArgs e)

{

e.Cancel = true;

}`

 

/// <summary>

// ReSharper disable CSharpWarnings::CS1584

/// Sends server control content to the specified <see cref="T:System.Web.UI."/><see cref="HtmlTextWriter"/> object, which writes the content to be rendered on the client.

// ReSharper restore CSharpWarnings::CS1584

/// </summary>

// ReSharper disable CSharpWarnings::CS1584

/// <param name="writer">The <see cref="T:System.Web.UI."/><see cref="HtmlTextWriter"/> object that receives the rendered content.</param>

// ReSharper restore CSharpWarnings::CS1584

/// <remarks></remarks>

[PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]

protected override void Render(HtmlTextWriter writer)

{

GridViewDocumentMaster.DataBind();

SPGridViewMappedDocs.DataBind();

base.Render(writer);

}

 

/// <summary>

/// Restores view-state information from a previous page request that was saved by the <see cref="M:System.Web.UI.Control.SaveViewState"/> method.

/// </summary>

/// <param name="savedState">An <see cref="T:System.Object"/> that represents the control state to be restored.</param>

/// <remarks></remarks>

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["FilterExpression"] = null;

//ViewState["FilterExpressionMappedDocs"] = null;

ViewState.Clear();

//ViewState.Remove("FilterExpression");

}

}

Ebook Download
View all
Learn
View all