+1-888-365-2779
Try Now
More in this section

Forums / General Discussions / Search via POST, not GET

Search via POST, not GET

9 posts, 0 answered
  1. Donnie Hale
    Donnie Hale avatar
    13 posts
    Registered:
    28 May 2010
    03 Aug 2010
    Link to this post
    I'm working on a slightly customized search capability. The main reason is because a simple textbox and submit button for searching don't play nice inside an asp.net form element on a page with other form elements (e.g. textboxes) and another submit button for those form elements. When the focus is on the search textbox and the "Enter" key is pressed, the user would intuitively expect that the search will get executed. Instead, the asp.net postback to itself happens on behalf of the other submit button.

    What I'm trying to do to work around this is use an asp:Panel with a DefaultButton set to the ID of an asp:Button with a PostBackUrl of the search URL:

    <asp:Panel runat="server" ID="pnlSearch" DefaultButton="btnSearch" BorderWidth="0">
      <
    input id="IndexCatalogue" name="IndexCatalogue" type="hidden" value="Content" />
      <input id="SearchQuery" name="SearchQuery" type="text" value="Search" class="input" />
      <asp:Button runat="server" ID="btnSearch" Text="Search" PostBackUrl="~/SearchResults.aspx" class="submit" />
    </
    asp:Panel>

    Since the default button behavior involves an HTTP POST instead of an HTTP GET (which is how the SearchResults.aspx URL is typically requested), the search variables that are normally in the query string come as POST variables instead.

    I've used Fiddler (www.fiddler2.com) to verify that the URL being requested is the same doing a standard search vs. doing it my way. And the POST variables have the same names and values as in the query string.

    Should this work? Does the code that gets executed when SearchResults.aspx is requested look specifically in the query string?

    If this won't work the way I'm trying to do it, any suggestions?

    Thanks,

    Donnie
  2. Donnie Hale
    Donnie Hale avatar
    13 posts
    Registered:
    28 May 2010
    03 Aug 2010
    Link to this post
    I used Reflector to see definitively that the code which handles the SearchResults.aspx URL looks specifically in the query string (Request.QueryString). So an HTTP POST is out of the question. While my solution is not as elegant as I'd like, I think I have things working the way I want.

    First, I've got a few javascript functions in a .js file:

    function onSearchTextFocus(txtQuery) {
        if (txtQuery.value == 'Search') txtQuery.value = '';
        txtQuery.inSearchText = true;
    }
     
    function onSearchTextBlur(txtQuery) {
        if (txtQuery.value == '') txtQuery.value = 'Search';
        txtQuery.inSearchText = false;
    }
     
    function onMasterFormSubmit(urlSearchResults) {
        var doSubmit = true;
     
        var txtQuery = document.getElementById('query');
        if (txtQuery != null &&
            txtQuery.inSearchText != 'undefined' &&
            txtQuery.inSearchText == true &&
            txtQuery.value.length > 0 &&
            txtQuery.value != 'Search') {
            doSubmit = false;
            document.location.href = urlSearchResults + '?IndexCatalogue=Content&SearchQuery=' + escape(document.getElementById('query').value);
        }
     
        return doSubmit;
    }

    I have some inline script in my master page:

    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterClientScriptInclude("prototype", ResolveUrl("~/assets/js/prototype.js"));
            Page.ClientScript.RegisterClientScriptInclude("application", ResolveUrl("~/assets/js/application.js"));
            Page.ClientScript.RegisterOnSubmitStatement(this.GetType(), "MasterFormOnSubmit",
                String.Format(@"return onMasterFormSubmit('{0}');", ResolveUrl("~/SearchResults.aspx")));
        }
    </script>

    And finally I have the HTML search elements:

    <div id="search">
        <input id="query" type="text" value="Search" onfocus="javascript:onSearchTextFocus(this);" onblur="javascript:onSearchTextBlur(this);" />
        <input id="btnSearch" type="button" name="sa" value="Search" class="submit" />
    </div

    (The <div> doesn't really matter.)

    When the focus is on the search textbox and "Enter" is pressed, the onMasterFormSubmit function is called. It sees that it was the search textbox that was in use and issues a GET to the SearchResults.aspx URL (using document.location.href). If it wasn't the search textbox that was being edited when Enter was pressed, the normal asp.net form submission process happens. In the case of one of our pages that has an enrollment form (via a .ascx user control), the code-behind for the user control gets invoked.

    I have no idea if this is the cleanest way to do it, but it's handled every test scenario I can come up with. Maybe it will help someone else...

    Donnie

  3. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    04 Aug 2010
    Link to this post
    Hi Donnie Hale,

    You are right, the post is out of the question indeed.
    The approach you have taken looks very nice. We are giving you 800 Telerik points for posting the solution in our forums. Thank you!

    Sincerely yours,
    Georgi
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  4. Donnie Hale
    Donnie Hale avatar
    13 posts
    Registered:
    28 May 2010
    04 Aug 2010
    Link to this post
    I'd say that not being able to use a POST for the search results is a bug. You can easily look in both the query string and the POST variables with one check.
  5. Donnie Hale
    Donnie Hale avatar
    13 posts
    Registered:
    28 May 2010
    04 Aug 2010
    Link to this post
    There was one issue with the solution as I posted it before - if the user used the mouse to click on the "Search" button, nothing happened. That was because it was an input of type "button" instead of "submit". Unfortunately, changing it to type "submit" doesn't completely work - it breaks doing the GET to the search page and hitting "Enter" now causes that submit button to happen instead of the submit button for our enrollment form (when the focus is in one of the enrollment form fields).

    Here's how I pulled it together:

    1) Add one more javascript function:

    function onSearchButtonClicked(btnSearch) {
        var txtQuery = document.getElementById('query');
        txtQuery.inSearchText = true;
        return true;
    }

    2) Change the search button HTML from what I posted before:

    <input id="btnSearch" type="submit" name="sa" value="Search" class="submit" onclick="javascript:return onSearchButtonClicked(this);" />

    3) Use an asp:Panel around the enrollment form in my .ascx user control, setting the DefaultButton property on the panel to the ID of the submit button that's in the enrollment form.

    So far, I haven't found a scenario where the page doesn't do what I expect, either via hitting the "Enter" key or by clicking one of the submit buttons.

    Hope this helps,

    Donnie
  6. briankb
    briankb avatar
    56 posts
    Registered:
    23 Feb 2004
    09 Nov 2010
    Link to this post
    We are still using v3.2, is there a hot fix that corrects this bug?
  7. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    09 Nov 2010
    Link to this post
    Hi briankb,

    SearchBox control sets only QueryString parameters with data - index catalog and query. The SearchResults control reads the parameters and pass the data to the SearchManager and its static method - Search.  This applies to Sitefinity 3.7 SP4.

    Kind regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  8. briankb
    briankb avatar
    56 posts
    Registered:
    23 Feb 2004
    09 Nov 2010
    Link to this post
    Two related questions.

    I'm trying to create a user control that performs the search. Previously we just edited the SearchBox controltemplate but this time I need a custom control. With the 3.4 SP4 version can I simply add a asp:input, hiddeninput, and button and have it postback to the search_results.aspx page and have it just work?

    How much work or possible issues would there be trying to upgrade form v3.2 to 3.4 SP4?

    Thanks.
  9. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    09 Nov 2010
    Link to this post
    Hi briankb,

    You can create your own control that uses the controls you want. The only thing that you can do is passing the parameters to the url, otherwise you should have a custom results control which could be just a repeater bound to datasource returned by SearchManager.Search method.

    As for the upgrading, if you do not have a lot of customizations, there would not be troubles. Since 3.6 we changed the module architecture and there could be some issues if you have  a custom modules.

    Greetings,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Register for webinar
9 posts, 0 answered