Click here to Skip to main content
Click here to Skip to main content

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

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.

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]; 
    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”.

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

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();
    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.

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:

base target="_self"

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

<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

Share

About the Author

S Sansanwal
Architect
Australia Australia
No Biography provided

Comments and Discussions

 
GeneralView State in Custom Control Pinmemberfaiqshah30-Mar-07 11:43 
GeneralRe: View State in Custom Control PinmemberS Sansanwal30-Mar-07 15:14 
Generaltrap usercontrol event in VB.net PinmemberSanded5-Sep-06 21:05 
GeneralRe: trap usercontrol event in VB.net PinmemberS Sansanwal6-Sep-06 11:39 
GeneralRe: trap usercontrol event in VB.net PinmemberSanded6-Sep-06 15:00 
GeneralRe: trap usercontrol event in VB.net PinmemberS Sansanwal6-Sep-06 17:37 
GeneralRe: trap usercontrol event in VB.net PinmemberNathan Buckley6-Sep-06 18:50 
QuestionRe: trap usercontrol event in VB.net PinmemberRNilesh7-Aug-08 18:24 

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

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

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