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
�:
- Web
Label
named �lblHeader
� to display entity against which search is performed.
- Web
TextBox
named �txtName
� to enter the name to be searched.
- Web
DataGrid
named �dgResults
� to display the search results.
- Web
Button
named �btnRetreive
� to fire the event to search result.
- 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:
public event System.EventHandler ButtonClicked;
protected virtual void OnClick(object sender)
{
if(this.ButtonClicked != null)
this.ButtonClicked(sender, new EventArgs());
}
private void BindData(DataSet ds)
{
this.dgResults.DataSource = ds.Tables[0];
this.dgResults.DataBind();
}
private void Page_Load(object sender, System.EventArgs e)
{
}
private void btnRetrieve_Click(object sender, System.EventArgs e)
{
OnClick(sender);
}
public DataSet GridDataSource
{
set
{
this.BindData(value);
}
}
public string TextName
{
get { return this.txtName.Text; }
}
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);
}
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.