Customize SearchBoxEx
A external company made a design for a Internet facing website, in the design the search control was made up in to rows. So my first idea was to create a new controltemplate for the SmallSearchInputBox, but then I realized that in moss the search-control isn’t a controltemplate anymore (in WSS it is). The searchbox in MOSS is a control called SearchBoxEx, and this has some properties for the layout of the control. But is always renders the output as a table with 1 row, so my problem was how to render is in 2 rows using divs. Google didn’t provide me with an answer, so is started coding myself (I don’t want to create a new control with all logic, I just want reuse all the logic of SearchBoxEx and provide new render logic).
I finally found a way to do this, I just create an new control that inherits from SearchBoxEx. My custom look-and-feel can be done used an override of the CreateChildControls:
protected override void CreateChildControls( ) { base.CreateChildControls( ); if ( Controls.Count < 2 ) { return; } HyperLinkLoc hlink = null; if ( Controls[ 1 ].GetType( ) == typeof ( Table ) ) { Table tabel = ( Table ) Controls[ 1 ]; // Loop through cells to find the go button foreach ( TableCell cell in tabel.Rows[ 0 ].Cells ) { if ( cell.Controls.Count > 0 ) { if ( cell.Controls[ 0 ].GetType( ) == typeof ( HyperLinkLoc ) ) { hlink = ( HyperLinkLoc ) cell.Controls[ 0 ]; } } } tabel.Visible = false;
I first call the base.CreateChildControls to have SearchBoxEx create all the controls based on the properties set. Then I start looping through the tablecells to find the Go-button, the other controls (textbox & dropdownlist) are available as protected members. After the go-button has been found, I set the table to invisible. Now that I have a reference to the controls I can start building my own look-and-feel:
if ( m_searchKeyWordTextBox != null ) { HtmlGenericControl div = new HtmlGenericControl( "div" ); div.Attributes.Add( "class", "ms-sbcell" ); m_searchKeyWordTextBox.Style.Clear( ); // remove the width style, done in css div.Controls.Add( m_searchKeyWordTextBox ); Controls.Add( div ); } if ( hlink != null ) { HtmlGenericControl div3 = new HtmlGenericControl( "div" ); div3.Attributes.Add( "class", "ms-sbgo ms-sbcell" ); div3.Controls.Add( hlink ); Controls.Add( div3 ); } if ( m_ddlScopes != null ) { HtmlGenericControl div2 = new HtmlGenericControl( "div" ); div2.Attributes.Add( "class", "ms-sbscopes ms-sbcell" ); div2.Controls.Add( m_ddlScopes ); Controls.Add( div2 ); } } }
Download source: searchbox


Hi Mike,
Please let me introduce myself. My name is Jackie Sun. I’m a programmer from Beijing, China.
After I read this article about how to Customize SearchBoxEx, I think it is very very helpful for the beginer to learn how to reuse all the logic of SearchBoxEx and provide new render logic, like me. Then, I run the code on my server, it works successful. Thanks for your share!
Now, I’m working hard for SharePoint Web Part Development. The main job is customize the search interface in SharePoint with Fast For SharePoint Server. But the official examples is little, so I want to consult you.
The problem is how to add a time select control in the search box. Like diagram 1.
(see diagram 1 URL)
https://k7edcg.blu.livefilestore.com/y1mr-ruYysz3BCL4WRXpxBk7gUUxs5m1tesqLFY-GzZ8lmntx_QDdRV1KwFFkIk3Y9KnDuZMV3z0hXz-Qpn6LVs512Q9ynSTuhQJ_IZTi5icqQylEF6-tj1lWnP2eF1MxzPk4niaQGNdNZ-cm0H5Sritg/searchbox.jpg
The original SharePoint search box is like diagram 2.
(see diagram 2 URL)
https://k7edcg.blu.livefilestore.com/y1m6Vd0FgllKDFtmQnb6B31iVkewyzM1D8EpctjpAgYQ6lRcUnoj9NUlRziUxsxtytPsXsaPOMUJqeDtrw2nT-nG4-qIJ987slWzIhfbPAZtZ1XsMRTS9ljDvxtV9rF86EQk2mD7EaL3zD0wWmvqiLZeA/oldsearchbox.jpg
In past two days, I tried to do some customization, but I can’t deliver the value of the time select control to the search result as parameter.
I’m trying to add a DropDownList control, then add SelectedIndexChanged event process procedure code. All codes is following:
Code:
public class MySearchbox : SearchBoxEx {
…
DropDownList timeSelect;
protected override void CreateChildControls()
{
base.CreateChildControls();
Table searchBoxTable = base.Controls[0] as Table;
TableRow searchBoxTableRow = searchBoxTable.Rows[0];
TableCell timeCell = new TableCell();
timeSelect = new DropDownList();
timeSelect.EnableViewState = false;
timeSelect.Items.Add(new ListItem(“Last 3 Year”));
timeSelect.Items.Add(new ListItem(“Last 3 month”));
timeSelect.Items.Add(new ListItem(“Last 3 week”));
timeSelect.Items.Add(new ListItem(“Last 3 day”));
timeSelect.SelectedIndexChanged += new EventHandler(timeSelect_SelectedIndexChanged);
timeCell.Controls.Add(timeSelect);
searchBoxTableRow.Cells.AddAt(1, timeCell);
}
// Response for SelectedIndexChanged Event
void timeSelect_SelectedIndexChanged(object sender, EventArgs e)
{
base.AppQueryTerms = “I DON’T KNOW HOW TO SET IT.”;
base.AppendToQuery = false;
}
…
}
I think I should pass the arguments by setting AppQueryTerms, but I don’t know how to set it. The program is failed, so I can’t get any search results.
Is this idea wrong? Do you have some good suggestions?
Thank you very much.
Yours
Jackie Sun