Click here to Skip to main content
12,759,601 members (33,116 online)
Click here to Skip to main content
Add your own
alternative version


33 bookmarked
Posted 11 Oct 2004

Generic Search Web User Control with event

, 11 Oct 2004
Rate this:
Please Sign up or sign in to vote.
This article explains how to create a web user control and trap its events.

Sample Image - SearchWebUserControl.jpg


This control provides a functionality to search a table based on the name column. The user interface uses DataGrid to display the records and the control fires an event trapped by the container to process the request.


Recently, I was developing an application which required a look up functionality of around 30 tables. It seemed a lot of effort and time to code one web page for each table.


To speedup the development time and ease of maintainability, I decided to create a generic search control that does this all for me. As all lookup tables have common column named “name”, it was much easier for me to have only one filter to search across the data in all tables.


Start a new web project named “SearchTest”. Add a new web user control and rename it to “SearchControl”. Rename the default web page “webform1.aspx” created to “default.aspx”. Add a new web page named “ControlContainer” that would contain our control.

Add following to the “SearchControl”:

  1. Web Label named “lblHeader” to display entity against which search is performed.
  2. Web TextBox named “txtName” to enter the name to be searched.
  3. Web DataGrid named “dgResults” to display the search results.
  4. Web Button named “btnRetreive” to fire the event to search result.
  5. HTML button named “Close” to close the parent form.

To “Close” button, add JavaScript to close the form.

INPUT type="button" value="Close" 
  onclick=" return CloseForm();" void function CloseForm() { window.close(); }

Modify control look and feel based on your requirement.

In the code behind the control, add:

// Event fired when user click button on user control
public event System.EventHandler ButtonClicked; 
/// <summary> 
/// Function that called by button click which
/// in turns fires the buttonclicked event 
/// trapped by the client 
/// </summary> 
protected virtual void OnClick(object sender) 
    // Raise the tabclicked event. 
    if(this.ButtonClicked != null) 
        this.ButtonClicked(sender, new EventArgs()); 
/// <summary> 
/// Function that called when data is retrieve from data layer 
/// Displays the data into the data grid 
/// </summary> 
private void BindData(DataSet ds) 
    this.dgResults.DataSource = ds.Tables[0]; 

private void Page_Load(object sender, System.EventArgs e) 
    // Put user code to initialize the page here 

private void btnRetrieve_Click(object sender, System.EventArgs e) 
    // call function that would fire the event trapped by the container 
/// <summary> 
/// Property to bind dataset to the Grid 
/// </summary> 
public DataSet GridDataSource 

/// <summary> 
/// Property to get data against which record to be searched 
/// </summary> 
public string TextName 
    get { return this.txtName.Text; } 
/// <summary> 
/// Property to set Header text 
/// </summary> 
public string HeaderText 
    set { this.lblHeader.Text = value;  } 
    get { return this.lblHeader.Text;  } 

Now, open the “ControlContainer” web page in design mode and drag-drop the “SearchControl” on this page. Rename the “SearchControl” as “searchCtrl” and add a new property named “HeaderText”. Set this property as “Customers”.

<uc1:SearchControl id="searchCtrl" runat="server" HeaderText="Customers">

In code behind of this page, add:

protected SearchControl searchCtrl;

In Page_Load event of this page, assign function to trap the ButtonClicked event of the control.

private void Page_Load(object sender, System.EventArgs e)
    searchCtrl.ButtonClicked +=new EventHandler(searchCtrl_ButtonClicked);
// Function that would be fired when
// user clicked “Retrieve” button on user control

private void searchCtrl_ButtonClicked(object sender, System.EventArgs e)
    SearchBLL bll = new SearchBLL();

Add business and data layer to read from database. See the attached code.

Now, add a HTML button to default.aspx page. Add OnClick event to this button to open the “ControlContainer” page.

void function OpenForm() 
    window.showModalDialog('ControlContainer.aspx', window, 

This code would open a modal dialog web page with our user control on it. Note: showModalDialog only works with IE.

Compile and run the application. Clicking on the button on default page would open a modal dialog box with control on it. Enter name and press “Retrieve” button to view the results.

You would have noticed that results have appeared in a new page rather than on the modal web page. To make this work, add:

base target="_self"

Between “<Head>” tag of “ControlContainer” web page, add:

  <base target="_self"> 
  <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
  <meta name="CODE_LANGUAGE" Content="C#">
  <meta name="vs_defaultClientScript" content="JavaScript">
  <meta name="vs_targetSchema" content="">

Compile and run the application again.

Hope this provides an understanding of creating a web control, using it and trapping its events in the container. It could be extended further to map each table in the database with searching on different column.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

S Sansanwal
Australia Australia
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralView State in Custom Control Pin
faiqshah30-Mar-07 11:43
memberfaiqshah30-Mar-07 11:43 
GeneralRe: View State in Custom Control Pin
S Sansanwal30-Mar-07 15:14
memberS Sansanwal30-Mar-07 15:14 
Generaltrap usercontrol event in Pin
Sanded5-Sep-06 21:05
memberSanded5-Sep-06 21:05 
GeneralRe: trap usercontrol event in Pin
S Sansanwal6-Sep-06 11:39
memberS Sansanwal6-Sep-06 11:39 
GeneralRe: trap usercontrol event in Pin
Sanded6-Sep-06 15:00
memberSanded6-Sep-06 15:00 
GeneralRe: trap usercontrol event in Pin
S Sansanwal6-Sep-06 17:37
memberS Sansanwal6-Sep-06 17:37 
GeneralRe: trap usercontrol event in Pin
Nathan Buckley6-Sep-06 18:50
memberNathan Buckley6-Sep-06 18:50 
QuestionRe: trap usercontrol event in Pin
RNilesh7-Aug-08 18:24
memberRNilesh7-Aug-08 18:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 11 Oct 2004
Article Copyright 2004 by S Sansanwal
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid