views:

3642

answers:

9

I have a master page with one form on it. It is a search form which must always be visible. When the button of that form is clicked I want the form data to be sent to search.aspx. The problem is, I don't know how. I cannot set the form action to search.aspx, because all my other pages which use the master form will go to search.aspx. This I don't want.

Hope someone can help me out :)

Thnx.

+1  A: 

You could create your search form in a separate form, and get it to use GET instead of POST.

Either that, or have the master form handle the search button click and use Server.Transfer to go to the search form.

David Kemp
both good suggestions :)
Mike Brown
+1  A: 

You can have multiple forms in one page I believe. So one form (your search form) would have its action set to search.aspx and the other would be set for the page itself.

Mike Brown
A: 

How do I create a seperate form? When i add multiple forms i get the error that i only can have one form.

With Server.Transfer("Search.aspx", true); How do i access the form data. A control has id="txtSearch" In search.aspx Request.Form["txtSearch"]; doesn't work. How come?

Martijn
Make sure you don't add the `runat="server"` attribute to the search form.
David Kemp
A: 

ASP.NET webform pages only have one form (which would generally be included on the master page). You can set the postback url for the search button to your search page..

<asp:Button ID="btnSearch" runat="server" Text="Search" PostBackUrl="~/search.aspx" />

..or just redirect to it from the handler in your master page like this:

protected void btnSearch_Click(object sender, EventArgs e)
{
    Response.Redirect(@"~/search.aspx?q=" + Server.UrlEncode(txtSearch.Text)); 
}

..or use Server.Transfer as suggested by David Kemp.

Note: If you use Request.Query[@"q"] on your search page to get your query, you don't need to use Server.UrlDecode() - it's done for you.

Nick
+1  A: 

In order to pass the values of the control "txtSearch", when Server.Transfer is executed, you could do many things, including passing it via a querystring variable or setting up a session variable, and then check either of those in the Page_Load event of Search.aspx, and if it's populated, call the event that is fired when the user would hit the submit button on the Search.aspx page.

Also, if the Search.aspx file is using the same masterpage, then you can use this.Master.FindControl("txtSearch") to get the control (it you look a the source of the file after it is generated in the browser, you'll notice that controls in the master page aren't really called by their ID, rather that have something appended to them (i.e. it would now possibly be called "ctl00_txtSearch")

mjmcinto
A: 

I would:

  • Add some code to the master page code-behind to detect the source of the POST.
  • Once I have the source of the POST (e.g. the Search box). I would then pass its query to the Search form.

I used a similar process with having a HTML login form on the master page.

I posted a question and subsequent solution here - check it out:

Form Elements in ASP.NET Master Pages and Content Pages

Once I got my head round it, it seemed a pretty simple and reasonably elegant solution.

The benefit of this is that you have complete control over how the data is sent to the search form. And you don't need to enable transfer of form data to the search form and all that nasty stuff, just create a new GET request to the search page and let it do what it is supposed to do :)

Hope this helps.

NOTE:

You can only have one form with runat="server" on an ASPX page. Additional forms MUST be HTML FORMS.

Rob Cooper
A: 

Because your search form is in the master page, you can probably structure it to contain 2 forms. Place the search form tags with the action set to "search.aspx" outside of the tag that is used by the rest of the site.

<body>
    <form action="search.aspx>
        <!--search box and submit button-->
    </form>
    <form runat="server">
        <!--rest of page inc placeholder-->
    </form>
</body>

If the structure of the page will not enable this, you can set the submit button's PosbackUrl to point to "search.aspx". In this case, "search.aspx" would need to be coded to look in the PreviousPage property for the form data, or use Request.Form to access the input.

HectorMac
A: 

Thnx all for replying :)

@Rob: How can i detect the source of the POST? I don't get your solution. Maybe you can post a code sample?

Now i use the server.transfer("search.aspx", true) solution. However, it is not working very well. Many times i get a StackOverflowException. What does this mean and how do i get rid of it?

A: 

What is the PreviousPage property? Is this a property i have to make myself?

When you post using the PostBackUrl feature of the button, the property will just available to the target page. It can be used to access the control values from the page originating the request as if it were a postback (you could retrieve the search textbox value).
HectorMac