using System;
using System.Web;
using System.Web.UI;
using System.Drawing;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace searchControl
{
public class SortedGridContrl : Control, INamingContainer
{
/// The Control class is the base class for all ASP.NET server controls.
///INamingContainer is a marker interface without method. When a control inherits with INamingContainer,
///the ASP.NET page framework creates a new naming scope under that control,this ensur us that the child
///controls have unique names.
///About our approch of create sorted gridView Control :-
/// I add a property MaxReocrd which we can set for a message display for max records on
/// If the grid is too large (e.g. if it has so many records that sorting it will kill the server),
/// user gets a message when they try to sort that says “There are too many records to sort –
/// please add more criteria” or something like that.
///
/// This is Example custom control with possibility of enhancement. connection string as property
/// suitable name if we want to fill dataset every time from dataset.if we have to use itemtemplate etc.
/// How to use :-
/// Add reff of dll and choose it as a tool. and drag drop on page from tool box.
///
///
/// on CS page :-
/// page load :- SortedGridContrl1.DataSetSource = dst;
/// and nothing code for soting, only click the header.
/// On:- aspx page
/// [cc1:SortedGridContrl ID="SortedGridContrl1" runat="server" MaxReocrd=25 ]
///[/cc1:SortedGridContrl]
#region property and variables
private DataSet _dstforSorting = new DataSet();
private DataView _dv = new DataView();
private DataTable _dt = new DataTable();
private int? _MaxReocrd = null;
private DataSet _DataSetSource = new DataSet();
public int MaxReocrd
{
set { _MaxReocrd = value; }
}
///
/// Using this viewstate we save a DATABASE round for fill dataset.
///
public DataSet DataSetSource
{
set
{
ViewState["ViewStateDst"] = value;
_dstforSorting = (DataSet)ViewState["ViewStateDst"];
}
}
private Label lblIfRowMoreThanLimit = new Label();
private GridView GV = new GridView();
#endregion property and variables
#region Create design view of control
protected override void CreateChildControls()///Called by the ASP.NET page framework to notify server controls
/// that use composition-based implementation to create any child
/// controls they contain in preparation for posting back or
/// rendering
{
if (!Controls.Contains(GV))
{
#region gridView
GV.EnableViewState = true;
GV.AllowSorting = true;
GV.ShowHeader = true;
GV.AutoGenerateColumns = true;
GV.AllowSorting = true;
GV.Sorting += new GridViewSortEventHandler(this.GV_Sorting);
/// The Sorting event is raised when the hyperlink to sort a column is clicked, but before the GridView
/// control handles the sort operation. This allows you to provide an event-handling method that performs
/// a custom routine
GV.DataSource = (DataSet)ViewState["ViewStateDst"];
GV.DataBind();
Controls.Add(GV);
#endregion gridView
}
}
#endregion Create design view of control
///
/// handling GridViewSortEventHandler . instance of the delegate to the event. And pass
/// GridViewSortEventArgs as agrument in sortedDataView function where sorting acctually taking place.
///
/// GridViewGV
/// GridViewSortEventHandler
protected virtual void GV_Sorting(Object sender, GridViewSortEventArgs e)
{
GV.DataSource = sortedDataView(e.SortExpression);
GV.DataBind();
}
///
/// Performing sorting of dataSet.
///
/// string sortExpression
///
private DataView sortedDataView(string sortExp)
{
_dstforSorting = (DataSet)ViewState["ViewStateDst"];
_dt = _dstforSorting.Tables[0];
_dv = _dstforSorting.Tables[0].DefaultView;
_dv.Sort = sortExp;
return _dv;
}
///
/// If the grid is too large
/// (e.g. if it has so many records that sorting it will kill the server),
/// user gets a message when they try to sort that says “There are too many records to sort
/// – please add more criteria” or something like that.
///
private void MessageforMaxRecords()
{
if (_MaxReocrd != null)
{
if (_dstforSorting.Tables[0].Rows.Count > _MaxReocrd)
{
lblIfRowMoreThanLimit.Text = "There are too many records to sort
– please add more criteria.";
Controls.Add(lblIfRowMoreThanLimit);
Controls.Add(new LiteralControl("[br/]"));
}
}
}
}
}
No comments:
Post a Comment