Advice Needed: Three Web controls talk to each other?

G

George Leithead

Hi all,

I have a requirement to implement a Search Interface component. I
would like to implement this using three different components, for
maximum flexibility.

The first two are "Basic" search and "Advanced Search". Both will use
the same back-end logic to put the search together. I'm using a
WebControl for both of these and overriding the CreateChildControls to
composite controls together (textbox, button, etc.).

The third component would be the "Search Results" component. I want to
base this control on a DataGrid.

The advice I need is how to get the search query from either of the
first two components to the Search Results component? I would like for
the Search Results to 'listen' for an event from one of the other two
components. How do I go about this? I don't want to have to put code
into the ASPX page to handle the communication between the two
controls.

Any advice, and direction of examples (in C#) would be fantastic and
much appreciated.

George
 
D

Doug Salomon

You said : "I don't want to have to put code into the ASPX page to handle the
communication between the two controls."

I think that you probably _should_ put that kind of code into the page
hosting the controls. the advantage to doing this is that your user controls
end up being more generic and hence more reusable in different scenarios.

Also, this is, more or less how the built in controls work. Consider the
following example : You have a page with a buton and a textbox. Clicking the
button causes "Hello World" (of course) to appear in the textbox.

The button and the textbox know nothing about each other. It is code on the
hosting page which handles the click event of the button and assigns the
words "Hello World" to the textbox's text property.

If the creator of the button and textbox had linked those two controls, then
they wouldn't be able to do much more than populate the textbox on click.

Just my opinion, but I hope it helps,
Doug
 
C

Chris Chilvers

I would create a SearchCriteria class to hold all the options for the
search.

The basic and advanced search controls would share the same instance of
SearchCriteria so that they can both set values in it, and a change in
one would be displayed in the other.

This same SearchCriteria instance could then be passed to search results
control so that the search results can view the output.

Lastly there can be some kind of perform search method within the
SearchCriteria class that queries the DB, fetches the results and raises
an event to say that it has a new search waiting. The search control can
subscribe for this event and rebind itself with the new data, this
should make it easy to have a button that performs the search on both
the basic and advanced controls.

Lastly you need to glue all this together, if you don't want it in the
ASPX page you're going to need an ASCX or something that will contain
the basic, advanced and results controls. This glue control will be the
one that is responsible for persisting the search criteria object (by
saving it to the viewstate or something). It will also handle the
switching between the basic, advanced, and results controls.

This should allow the three different parts to work together whilst
remaining de-coupled.

Although the only reason for not making the ASPX page the 'glue' control
that I can see is if you wanted to use the search control on other
pages. For this to be of much use you'd have to look at a more generic
solution as to how you create your basic and advanced search pages if
you wanted to reuse the same search control for any search.


Note, this may not be the best solution, I've just typed this up as I've
thought of it but maybe it can give you some ideas.
 
G

George Leithead

All,

I have managed to do what I wanted to do! The first control UIa and
second control UIb both call SearchResults. This is done through the
following:

In the UIa class
===========
public delegate void SearchEventHandler(object sender,
SearchActivatedEventArgs e);
....
_SearchButton.Click += new EventHandler(_button_Click);

....
private void _button_Click(object source, EventArgs e)
{
...
// Go through the collection of controls in the page, and make sure
that there is a SearchResults control to Call!
foreach (Control ctrl in Page.Controls)
{
foreach (Control chldControl in ctrl.Controls)
{
if (chldControl.GetType() == typeof(myControlLibrary.SearchResults))
((myControlLibrary.SearchResults)chldControl).PerformSearch(this, new
SearchActivatedEventArgs(queryExpression, InstanceName,
CollectionNames, useWildcards));
}
}
}

In the SearchActivatedEventArgs.cs class
==============================
public class SearchActivatedEventArgs : EventArgs
{
public SearchActivatedEventArgs(string inqueryExpression, string
inInstanceName, string inCollectionNames, bool inuseWildCards)
{
queryExpression = inqueryExpression;
InstanceName = inInstanceName;
CollectionNames = inCollectionNames;
useWildCards = inuseWildCards;
}
public string queryExpression;
public string InstanceName;
public string CollectionNames;
public bool useWildCards;
}

In the SearchResults.cs class
======================
public void PerformSearch(object sender, SearchActivatedEventArgs e)
{
....
}

Enjoy...George
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top