Click here to Skip to main content
15,885,106 members
Articles / Web Development / HTML
Article

Generic Search Web User Control with event

Rate me:
Please Sign up or sign in to vote.
4.09/5 (7 votes)
11 Oct 20042 min read 104K   1.5K   33   8
This article explains how to create a web user control and trap its events.

Sample Image - SearchWebUserControl.jpg

Introduction

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.

Limitation

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.

Solution

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.

Example

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.

HTML
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:

C#
// 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]; 
    this.dgResults.DataBind(); 
} 

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) 
{ 
    OnClick(sender);
    // call function that would fire the event trapped by the container 
}
 
/// <summary> 
/// Property to bind dataset to the Grid 
/// </summary> 
public DataSet GridDataSource 
{ 
    set 
    { 
        this.BindData(value); 
    } 
}

/// <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”.

ASP.NET
<uc1:SearchControl id="searchCtrl" runat="server" HeaderText="Customers">
</uc1:SearchControl>

In code behind of this page, add:

C#
protected SearchControl searchCtrl;

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

C#
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();
    bll.SearchData(this.searchCtrl); 
}

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.

JavaScript
void function OpenForm() 
{ 
    window.showModalDialog('ControlContainer.aspx', window, 
                     'dialogWidth:450px;dialogHeight:400px;');
}

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:

HTML
base target="_self"

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

HTML
<HEAD>
  <base target="_self"> 
  <title>ControlContainer</title>
  <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="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>

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.

License

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


Written By
Architect
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralView State in Custom Control Pin
faiqshah30-Mar-07 10:43
faiqshah30-Mar-07 10:43 
GeneralRe: View State in Custom Control Pin
S Sansanwal30-Mar-07 14:14
S Sansanwal30-Mar-07 14:14 
Generaltrap usercontrol event in VB.net Pin
Nathan Buckley5-Sep-06 20:05
Nathan Buckley5-Sep-06 20:05 
GeneralRe: trap usercontrol event in VB.net Pin
S Sansanwal6-Sep-06 10:39
S Sansanwal6-Sep-06 10:39 
GeneralRe: trap usercontrol event in VB.net Pin
Nathan Buckley6-Sep-06 14:00
Nathan Buckley6-Sep-06 14:00 
GeneralRe: trap usercontrol event in VB.net Pin
S Sansanwal6-Sep-06 16:37
S Sansanwal6-Sep-06 16:37 
GeneralRe: trap usercontrol event in VB.net Pin
Nathan Buckley6-Sep-06 17:50
Nathan Buckley6-Sep-06 17:50 
QuestionRe: trap usercontrol event in VB.net Pin
RNilesh7-Aug-08 17:24
RNilesh7-Aug-08 17: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.