Passing home page form data to another web form

I have a main page with one form. This is a search form that should always be visible. When this form button is clicked, I want the form data to be submitted to search.aspx. The problem is I don't know how to do it. I cannot set the form action to search.aspx because all my other pages that use the main form will go to search.aspx. I don’t want that.

Hope someone can help me :)

Thnx.

+1


source to share


6 answers


To pass the values ​​of the "txtSearch" control when Server.Transfer is executed, you can do a lot of things, including pass it through a querystring variable or set up a session variable, and then check any of those in the Page_Load event for Search.aspx, and if it is filled, fire an event that will be fired when the user clicks the submit button on the Search.aspx page.



Also, if the Search.aspx file uses the same master page, you can use this.Master.FindControl ("txtSearch") to get the control (it's you look at the source of the file after creating it in the browser, you will notice that the controls on the master page are not actually called by their id, but there is something added to them (ie, it could now be called "ctl00_txtSearch")

+1


source


You can create your search form in a separate form and force it to use GET instead of POST.



Either that, or have a main form handling the search button and use Server.Transfer to navigate to the search form.

+1


source


There can be multiple forms on a single page that I believe. This way, one form (your search form) will have an action set to search.aspx and another will be set for the page itself.

+1


source


ASP.NET Web Pages only have one form (which is usually included in the master page). You can set the backlink url for the search button to your search page.

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

      

.. or just redirect it from the handler on the main 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 request, you don't need to use Server.UrlDecode () - it's done for you.

0


source


I would:

  • Add some code to your homepage to discover the source of the POST.
  • Once I have a POST source (like a search box). Then I'll submit the request to the search form.

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

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

Form Elements in Master Pages and ASP.NET Pages

Once I put my arms around it, it looked like a pretty simple and reasonably graceful solution.

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

Hope it helps.

Note:

You can only have one form with runat="server"

per ASPX page. Additional forms MUST be HTML FORMS .

0


source


Since your search form is on the home page, you can probably structure it to contain 2 forms. Place your search form tags with an action set to "search.aspx" outside of the tag 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 page structure is not enabled, you can set the submit button PosbackUrl to point to "search.aspx". In this case, "search.aspx" must be encoded in order to view the PreviousPage property for the form data, or use Request.Form to access the input.

0


source







All Articles