Search box in master page

This must be a classic situation, and I can think of x ways to solve this. But I'd like to have your opinion on what's considered "best practice".

I have a search box in the header of my master page. Fine. Whenever i click the search button, I'd like the results.aspx page to load, regardless of what (content) page I'm currently visiting.

Should I change action on form1 (to point to results.aspx)? Should I introduce a second form (pointing to results.aspx) that only contains the search box? Or should I redirect in the Search_Click method?

 


If this post was useful to you, please mark it as answer. Thank you!
0
johram
10/25/2007 8:31:58 AM
asp.net.navigation-controls 13714 articles. 0 followers. Follow

10 Replies
1712 Views

Similar Articles

[PageSpeed] 35

johram:
Or should I redirect in the Search_Click method?
 

I usually go with this approach as then you are not altering any of the functionality of the master page (i.e by changing the action). However, the idea of using a second form may be just as good if not better as then it eliminates the need to post back to whatever page you are on just to redirect the user (I use this method for google searches on my site).

So, I'd say the best practice would be to go with the second form as it has the least impact on the site.
 


Website Design Darlington - http://mdssolutions.co.uk
http://lessthandot.com - Experts, Information, Ideas & Knowledge
http://aspnetlibrary.com - An online resource for professional ASP.NET developers


Please remember to click "Mark as Answer" on this post if it helped you

0
ca8msm
10/25/2007 8:46:56 AM

johram:

This must be a classic situation, and I can think of x ways to solve this. But I'd like to have your opinion on what's considered "best practice".

I have a search box in the header of my master page. Fine. Whenever i click the search button, I'd like the results.aspx page to load, regardless of what (content) page I'm currently visiting.

Should I change action on form1 (to point to results.aspx)? Should I introduce a second form (pointing to results.aspx) that only contains the search box? Or should I redirect in the Search_Click method?

 

You can use any of the approach as it depends on which one you are most comfortable with. Usually I create templates with Search option in the master page, these templates are general so as to fit in with any language, i.e. independent of asp.net, and hence I use query redirection, i.e. pass the search item in the query to the page that displays the stuff.

Later when making these pages functional, I just grab the search keyword in the redirection. This is more flexible as you dont need to alter the form or any other thing, and can fit in with CGI-Perl, JSP, Asp.net, or Php.


Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
0
naturehermit
10/25/2007 10:18:06 AM

My first attempt was to have the (master) button postback to self and then redirect to search.aspx, but this seemed to contain an uneccesary step.

Second attempt involved implementing it as a second form with a different action than the main form. But it turns out a page can only have one server-side form. And I think I want to have it server-side to be able to avoid having to pass parameters manually.

Third attempt was to set PostBackUrl of the search button to search.aspx. This approach was actually successful, although I don't like the way I had to retrieve the search box value from the caller. There can be two situations: get the value from PreviousPage.Master.FindControl (if cross-page post) and Master.FindControl (if postback). It doesn't feel "kosher". I also experienced problems here with Page_Init/Page_Load between master page and content page, ending up in a behavior where old values were not populated when I tried to access them (from content Page_Load, since it is called before Master Page_Load). Phew.

In my fourth attempt I tried to eliminate all FindControl stuff and go for Naturehermit's method, and pass the actual value on the query string. Still using one server-side form, but by modifying the PostbackUrl property. The problem I stumbled into here was that I needed to intercept and modify the PostbackUrl before it is submitted, in order to attach the actual value of the search box. Dang. No success.

So, in my despair I am thinking that maybe I should go for a classic approach. Split it into two forms; one client-side search form, using good ole GET method, and one conventional server-side form for the contents of the page. And then do the necessary plumbing myself.

Sigh. Any comments or thoughts on this? Again, I am not looking for a solution that works. I want a good-looking solution that works Stick out tongue


If this post was useful to you, please mark it as answer. Thank you!
0
johram
10/31/2007 2:54:08 PM

johram:
But it turns out a page can only have one server-side form. And I think I want to have it server-side to be able to avoid having to pass parameters manually.
 

What do you mean manually? If you have a look at the source of my site, you'll see that I have two forms. The first is a form for the google search (i.e. no runat="server") with the method set as GET (although this could just as easily be POST). The second is my main ASP.NET with the runat="server" attribute. Both work as long as you don't try to nest them.


Website Design Darlington - http://mdssolutions.co.uk
http://lessthandot.com - Experts, Information, Ideas & Knowledge
http://aspnetlibrary.com - An online resource for professional ASP.NET developers


Please remember to click "Mark as Answer" on this post if it helped you

0
ca8msm
10/31/2007 3:08:33 PM

Have a read through this article for a pattern

http://msdn2.microsoft.com/en-us/library/6c3yckfw(vs.71).aspx

and you can have two forms (not with runat="server" and not nested)

and you can request query by Request.Querystring


Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
0
naturehermit
10/31/2007 3:15:51 PM

ca8msm:

What do you mean manually? If you have a look at the source of my site, you'll see that I have two forms. The first is a form for the google search (i.e. no runat="server") with the method set as GET (although this could just as easily be POST). The second is my main ASP.NET with the runat="server" attribute. Both work as long as you don't try to nest them.

 

With manually I mean that if I go for a non server-side form, I will have to take care of loading the posted form values myself, as compared to a server-side version where viewstate would possibly? solve this automatically. (Although I've read Dave Reed's thorough article on Viewstate, I can't say I master the concept fully).

I checked your site, and that implementation is the one I am likely to choose. Having a non server-side search form in the top which uses GET to transfer the values to the search page. That way, the search page can be invoked from wherever, if you just know the parameters. It will be a nice loose coupling ;-)


If this post was useful to you, please mark it as answer. Thank you!
0
johram
10/31/2007 3:53:37 PM

OK, this is how it finally ended up:

The input box is contained within the form that spans the entire page. Search button has PostBackUrl set to Search.aspx. In Page_Load of Search.aspx I have the following code:

if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
{
searchText = ((MasterPage)PreviousPage.Master).SearchText;
Master.SearchText = searchText;
}
else if (IsPostBack)
{
searchText = Master.SearchText;
}
else
{
searchText = Request.QueryString["search"] != null ? Request.QueryString["search"].ToString() : "";
Master.SearchText = searchText;

Or, even simpler:

if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
{
	Master.SearchText = ((MasterPage)PreviousPage.Master).SearchText;
}
else if (!IsPostBack)
{
	Master.SearchText = Request.QueryString["search"] != null ? Request.QueryString["search"].ToString() : "";;
} 

I have a public property in my master page called SearchText which sets/gets SearchTextBox.Text. The code in Page_Load allows for three types of invocation:

1) Cross-page postback. If we invoke search from another content page, then we need to use PreviousPage to get what we want. Since I have @MasterType specified in Search.aspx I can cast the Master page to the correct type, and through that access the property instead of having to do an ugly FindControl.

2) Normal postback. In this case I can read out the value from Master.SearchText directly.

3) Trough query string parameters (this is a bonus). The nice thing about this is that it allows the page to be invoked from outside the web, as an external link.

As have already been written in many blogs and articles, you should avoid using FindControl to access data from your Master page. Better expose public properties, since you will then have an easier dependency (you don't rely on the actual id that is used in the master aspx). 

 


If this post was useful to you, please mark it as answer. Thank you!
0
johram
11/2/2007 9:16:47 AM

Would it be possible to see the code for the 'complete' solution? As all this of setting a 'public property' doesn't make sence to a fairly newcomer. What I mean is; what code is put exactly where in the MasterPage, and what code is put exactly where in Search.aspx. Would be very nice indeed.

 And I must add, that Microsoft certainly didn't make web development easier and more accessible as Adobe tends to do.

0
svdelle
2/7/2008 8:27:25 AM

Hi Johram,

I have a problem posted here:

http://forums.asp.net/t/1417589.aspx

I did same your above instructions but no luck.

Any help highly appreciated.

Thanks

0
rajsedhain
5/1/2009 7:26:57 PM

I solved it.

Thanks

0
rajsedhain
5/4/2009 5:08:30 PM
Reply:

Similar Artilces:

Master page search box to other page
  I have a Master page where is searching funtionality. When I search it display the records into repeater.I have other 2 pages (Browse by Name and browse by Author) ; both pages use the master page so that the search textbox and button appear on those pages.   My problem is:  When I browse by Name, I created a A-Z lists there and it display the records, In the mean time when I use that search button it display the data on the same Name.aspx page (with those browse data) .I want to display those data into default.aspx when click the search button even even I open Name.aspx...

Content controls have to be top-level controls in a content page or a nested master page that references a master page.
Why in the world am I getting this error? The solutions I have seen either don't apply or don't work.  Here is my master page code:<%@ Master Language="C#" AutoEventWireup="false" CodeFile="TechLMSMasters/MainSide.master.cs" Inherits="MainSide" %> <%@ Register TagPrefix="usercontrol" TagName="Search" Src="~/library/uc/Search.ascx" %> <%@ Register TagPrefix="usercontrol" TagName="UserName" Src="~/library/uc/UserNameDisplay.ascx" %><!DOCTYPE html PUBLI...

Post from one search box (on master page) to another search page's query and display result.
Hi, I am using the code for a search using index server from this page http://www.codeproject.com/aspnet/search.asp  I want to post to the search from a text box on another page.  E.g. I have a navigationg box at the top of my page, included in all my page which has a search box and a submit button.  I want, when a user types in their search term, for the query to be posted to the search page and the results shown in one motion like google for example.  The search offers a number of different search options such as all words, any words etc. I would be quite happy for it t...

ERROR Content controls have to be top-level controls in a content page or a nested master page that references a master page.
This all used to work until about 5 minutes ago. Did I change something? For some reason I am now getting this error: Content controls have to be top-level controls in a content page or a nested master page that references a master page. What I have:  I have a masterpage inside of a directory. That directory is a first level directory with its own web.config file....The whole thing is protected by the web.config file above it (in the main public directory). That main public directory has a masterpage as well. I am not currently linking the two masterpages together becuase...

Error : Content controls have to be top-level controls in a content page or a nested master page that references a master page.
when I make a directory on my root, and in that directory I put a masterpage and a web form ... I get always an error : Content controls have to be top-level controls in a content page or a nested master page that references a master page.   Does somebody know what the problem is ? Is it possible you can't set a masterpage in a directory ... ?Kind Regards,Joeri Pansaerts.From Bruges, Belgium :) i've founded ... I had to nest my masterpage ...Kind Regards,Joeri Pansaerts.From Bruges, Belgium :) Hi Joeri Pansaerts, I am receiving the same error as the one posted her...

I have made a search box, which works prefectly as I wished in that page, I would like to add the same search box in all other page
 I have made a search box, which works prefectly as I wished in that page, I would like to add the same search box in all other page. I have added the textbox adn all that . Transferred the textbox value to session variable. Made a resposnse.redirect to the target page. But then the grid is not taking the value frmo the session variable. Better say grid is not reacting to the whole linked up scenariio. Could any one help. Thanks in advance.  Digish A D The gridview that you are binding to probably uses a DataSourceObject. The paramater you are passing in to your s...

how to access child page control (check box) in master page
hi all,        I need to implement it darely. can anybody tell me how can i access a child page check box in my master button event.       i need it urgent. Pls help me as early as possible Asraful hi, check th link how to access controls in masterpage http://forums.asp.net/p/1330013/2670187.aspx#2670187Please Mark as Answer if it helps you Try  this if (this.Master.FindControl(" contentplaceholder1").FindControl("chkboxID").Checked == false){//your code stuff} Happy Coding...▌µùthùkûµâ₧á∩▐ . U can do l...

Master Control
I find Master Page idea brilliante. But wouldn't it be nice to have an ability to assign master layout for user controls? Does anyone know of something like that? Look into Themes and Skins. Skins are user to set a template for a contol.Themes and SkinsThemes and Skins 2 www.hopeasp.net Hmm Sean, themes and skins will only help with STYLING the components not layout. What I need is something to set the LAYOUT of the user control and use that master layout to place components in. Like in master page but in user controls.Simple example: Master Control fileUsercontrol layout st...

Model popup in master page not updating the child page list box control
 Hello, I am facing two unusual types of problems.  I have a Master Page that includes a Model Popup Extender code. When master page is loaded with inner child page, the child page has list box control to show address book list. The master page calls the model popup extender to edit the address books and add new address books. The issues comes, when I try to close the model popup extender that also saves the address book contacts. I want when this popup closes it should refresh the list box in child page with new address book data. Is it possible to block the AJAX call wh...

HELP..... Master Page Error (The page has controls that require a Master Page, but none is specified.)
Hi, I had almost finished creting my master page to use as a template for all future internal applications.  I had created a template to use in creatign new applications and had sucessfully created a new project based upon the MasterPage template... All was working well when I noticed a small graphic inconsistancy on the MasterPage... I reopened the MasterPage and I got teh above error message... I have looked at the HTML and everything seems to be okay and there including the header lines... <%@ Master Language="VB" CodeFile="MasterPage.master.vb" Inherits=&qu...

Doing Postback from master page to page in master's content place holder(search page)
Hi, Currently I am trying to do a postback which returns results. The master page has a textfield and button and the button redirects to the search page.The search page also has a textfield and button and when the button is clicked, the query statements take the input from teh user and looks for results. Currently, A Crosspage postback of master page + other pages in content place holder ------> master page+search page in content place holder works.I am trying to do a postback which returns results in master page+search page which gets its input from the textbox of the master page ...

Using controls from pages, master pages, and user controls
How would I structure my site in this scenario: I'd like to use a dropdownlist all over my site, sometimes in a regular page, sometimes in a master page, and sometimes in a user control.  I wish to bind its events programmatically, so normally I would just put that in the codebehind.  However, I notice a lot of code duplication because I have to declare the control in everywhere, in my base page (which all my pages inherit from), in my base master page (which all my master pages inherit from) and in my base user control (which all my user controls inherit from).How should I be...

Net::LDAP::Control::Paged unexpected behavior using multiple paged searches against AD [SEC=UNCLASSIFIED]
------=_NextPart_000_003B_01CEEE7E.0BE685F0 Content-Type: multipart/related; boundary="----=_NextPart_001_003C_01CEEE7E.0BE685F0" ------=_NextPart_001_003C_01CEEE7E.0BE685F0 Content-Type: multipart/alternative; boundary="----=_NextPart_002_003D_01CEEE7E.0BE685F0" ------=_NextPart_002_003D_01CEEE7E.0BE685F0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit UNCLASSIFIED Hi . I am finding unexpected behaviour when using this control against our AD servers. I have tried this using 5.8 (perl-ldap 0.4...

master, child page search control
.net 2.0I have a master and child pages. In the chid page there are 10 File upload controls. When user click the Upload button, I want to check whether user has selected file in all the controls using a for loop.how can I do this?  thanks in advance.feed the creative machine You could do this a few different ways... Do you need the user to have files in each box, or you need to know which ones have files selected and which ones dont, to process? If your making the fields required, use the validation control provided with .net 2.0 for each of your file upload boxes. If you...

Web resources about - Search box in master page - asp.net.navigation-controls

SEO, Search Engine Optimisation - Website Optimisation
Hit Search Limited - digital marketing agency specialising in search engine optimisation, affiliates, ROI and pay per click. Online marketing ...

SEO & Search Engine News : SEJ
Latest Search Engine News from the Search Engine Journal.

Search Engine Marketing (SEM), Paid Search Advertising (PPC) & Search Engine Optimization (SEO) - Search ...
The authoritative guide to search engine marketing (SEM), search engine optimization (SEO), paid search advertising (PPC) and social media marketing. ...

Search Newz :: Search Engine News
... To Fight Over Paid Links Google prepared a note about a proposal by French lawmakers and backed by French news publsihers, which want search ...

State of Search - Where Search and Social meet up
The fastest growing Search and Social blog covering strategy developments in search and social media

Yahoo! Search - Wikipedia, the free encyclopedia
This article is about the Yahoo! Search Engine . For the corporation, see Yahoo! . For other uses, see Yahoo! (disambiguation) . , the 2nd largest ...

Coward punch: Brisbane teen assaulted in Fortitude Valley - The Courier-Mail Search Search
A SECOND man has been charged over the alleged coward punch attack on a teenager in Fortitude Valley this morning.

Steelcats psyched for new season - TownsvilleBulletin Search Search
WITH weeks of hard work already behind them the Magnetic North Steelcats are primed to make their mark on the revised state wide netball competition ...

MPA Continues Search for President
The search for a new head of the Association of Magazine Media (MPA) continues. The MPA has been without a president since Mary Berner departed ...

Tripnary lets you search for travel destinations based on your budget
... offers a great solution for anyone looking to combat the January blues with a nice vacation in the sun. The app allows iOS users to search out ...

Resources last updated: 1/16/2016 1:57:59 PM