Monday, June 9, 2008

Create Custom Sortable GridView

Hi, all this is a server custom control for a gridview. which allow sorting on click on its header. and Page Level Programmer not need to put a line for sorting :)
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
/// DataView
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/]"));


}
}

}

}
}

Tuesday, June 3, 2008

creating custom control for search TextBox and Button and retrun dataset

Hi, here all technical details of creating custom control for search. Search dustom control return dataset on page. We can use this dataset for various database operation on page. At the end of document there is sql statements of creating table and insert values.

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 SearchControl : 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 search Custom Control :-
/// This is Example custom control with possibility of enhancement. we used forecolor, connection string as property
/// similar we can use property of table name.

/// 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 :- this.SearchControl1.ConnectionString = @"Data Source=192.168.0.4\ver2005;Initial Catalog=test_navdeep;User ID=navdeep;Password=navdeep";

/// protected void onSubmitName(object sender, EventArgs e)
/// {
/// DataSet dst = this.SearchControl1.ReturnDataSet;
/// }

#region property and variables

bool handled = false;
private Color _ForeColor;
private string _searchkey;
private string str = string.Empty;
private DataSet dstTest1 = new DataSet();
private string _connectionString = string.Empty;

public string ConnectionString
{
get { return _connectionString; }
set { _connectionString = value; }
}


private SqlConnection conPub;
public string SearchkeyColmunName
{
get { return _searchkey; }
set { _searchkey = value; }
}
private TextBox txtSearchKey; ///Control to add in our composite control

private DataSet dst = new DataSet();
private string _returnDataSet = string.Empty ;


public event EventHandler Submit; /// It's a delegate Eventhandler. The other two data member variables created are actually of type
/// EventHandler. We will be using them for handling the events that
/// are going to be raised by the Button controls
///Similar we can add or more event handler

public Color ForeColor
{
get { return _ForeColor; }

set { _ForeColor = value; }
}

public string SearchKey
{
get
{
this.EnsureChildControls(); ///Determines whether the server control contains child controls.
///If it does not, it creates child controls.
return txtSearchKey.Text;

}

set
{
this.EnsureChildControls();
txtSearchKey.Text = value;

}
}

#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

{

Label lblSearchKey = new Label();
lblSearchKey.Text = "Search Key: ";
lblSearchKey.ForeColor = _ForeColor;
Controls.Add(lblSearchKey);

txtSearchKey = new TextBox();
Controls.Add(txtSearchKey);

Controls.Add(new LiteralControl("
")); /// for breaking a new line



Controls.Add(new LiteralControl("
"));

Button btnSubmit = new Button();
btnSubmit.Text = "Search";
btnSubmit.CommandName = "Submit";
btnSubmit.ForeColor = _ForeColor;
Controls.Add(btnSubmit);

Controls.Add(new LiteralControl(" "));



}
#endregion Create design view of control

#region virtual bubleEvent
protected override bool OnBubbleEvent(object Source, EventArgs Sender)///These methods are OnBubbleEvent and
///RaiseBubbleEvent. We will be using the OnBubbleEvent for our event handling.
///So here is the code for the method that we override for usage within our own class.

{



if (Sender is CommandEventArgs)
{

CommandEventArgs ce = (CommandEventArgs)Sender;

if (ce.CommandName == "Submit") /// if we contain two button on composite then we determine to fire it
/// delepending on its command name on button
{

onSubmit(ce);
handled = true;
}


}

return handled;

}

#endregion virtual bubleEvent

#region onSubmit
protected virtual void onSubmit(EventArgs Sender)
{
if (Submit != null)
{
_returnDataSet = txtSearchKey.Text;


Submit(this, Sender);/// set event handling



}
}
#endregion onSubmit

#region ReturnDataSet
///
/// Search depends on searchkey property and return dataset of desird record.
/// for example we can set ID and Aname for two defferent searches
///

public DataSet ReturnDataSet
{
get {
if (txtSearchKey.Text.ToString() != "")
{
if (_searchkey == "ID")

{


SqlDataAdapter dadAuthors;
dstTest1 = new DataSet();
conPub = new SqlConnection( _connectionString);
dadAuthors = new SqlDataAdapter("Select * From Test1 where id = '" + _returnDataSet + "'", conPub);
dadAuthors.Fill(dstTest1, "Test1");

return dstTest1;

}
if (_searchkey == "Aname")


{
conPub = new SqlConnection(_connectionString);
SqlDataAdapter dadAuthors;
dstTest1 = new DataSet();
dadAuthors = new SqlDataAdapter("Select * From Test1 where Fname = '" + _returnDataSet + "'", conPub);
dadAuthors.Fill(dstTest1, "Test1");


return dstTest1;
}


return dstTest1;
}
else
{


return dstTest1;
}




}
}

#endregion ReturnDataSet



}
}


Table which we used for search


select * from Test1
Create table Test1( ID varchar(50), Fname varchar(50), Lname varchar(50), Address varchar(50), depID varchar(50))
create proc
@id varchar(50),
@Fname varchar(50),
@Lname varchar(50),
@Address varchar(50),
@depID varchar(50)
as
insert into Test1 values(@id,@Fname,@Lname,@Address,@depID)

insertInTO '1', 'A_Fname', 'A_Lname1','A_Address', 'D1'
insertInTO '2', 'B_Fname', 'B_Lname1', 'B_Address', 'D2'
insertInTO '3', 'C_Fname', 'C_Lname1', 'C_Address', 'D3'
insertInTO '4', 'D_Fname', 'E_Lname1', 'F_Address', 'D4'
insertInTO '6', 'E6_Fname', 'E6_Lname1', 'E6_Address', 'D6'
insertInTO '5', 'E_Fname', 'E5_Lname1', 'E5_Address', 'D5'

insertInTO '6','E6_Fname','E6_Lname1','E6_Address','D6'
insertInTO '5','E_Fname','E5_Lname1','E5_Address','D5'
insertInTO '7','7G_Fname','G7_Lname1','G_Address','D7'
insertInTO '8','H8_Fname','E8_Lname1','8F_Address','D8'
insertInTO '9','9I_Fname','I9_Lname1','9I_Address','D9'

insertInTO '10','9I_Fname','I9_Lname1','9I_Address','D10'
insertInTO '11','9I_Fname','I9_Lname1','9I_Address','D11'
insertInTO '12','9I_Fname','I9_Lname1','9I_Address','D12'
insertInTO '13','9I_Fname','I9_Lname1','9I_Address','D13'
insertInTO '14','9I_Fname','I9_Lname1','9I_Address','D14'
insertInTO '15','9I_Fname','I9_Lname1','9I_Address','D15'
insertInTO '16','9I_Fname','I9_Lname1','9I_Address','D16'
insertInTO '17','9I_Fname','I9_Lname1','9I_Address','D17'