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=\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
this.EnsureChildControls(); ///Determines whether the server control contains child controls.
///If it does not, it creates child controls.
return txtSearchKey.Text;


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;

txtSearchKey = new TextBox();

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(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

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;

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)
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'

No comments: