Martijn
Martijn

Reputation: 24799

Passing master page form data to another webform

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.

Upvotes: 1

Views: 8097

Answers (6)

HectorMac
HectorMac

Reputation: 6143

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.

Upvotes: 0

Rob Cooper
Rob Cooper

Reputation: 28867

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.

Upvotes: 0

Nick
Nick

Reputation: 5696

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.

Upvotes: 0

mjmcinto
mjmcinto

Reputation: 218

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")

Upvotes: 1

Michael Brown
Michael Brown

Reputation: 9153

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.

Upvotes: 1

kͩeͣmͮpͥ ͩ
kͩeͣmͮpͥ ͩ

Reputation: 7846

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.

Upvotes: 1

Related Questions