Masterpage search textbox to search.aspx textbox- how?

Last post 07-03-2009 12:21 PM by Dave Sussman. 6 replies.

Sort Posts:

  • Masterpage search textbox to search.aspx textbox- how?

    07-02-2009, 9:03 PM
    • Member
      11 point Member
    • nadude
    • Member since 05-15-2009, 8:41 PM
    • Posts 27

     Hi. I'm new to asp.net, using vb and masterpage. I've got a working search.aspx, enter whatever into txtSearch (the textbox id), and hit Search button, and gridview gets results. I added a textbox with id txtSearchH and search button to my master page. And hit the search button's postbackurl to search.aspx. But nothing happened, so I site googled for search and masterpage, and came up with a few options to try but nothing worked. Heres my code so far, hope someone can help:

     

    masterpage code:

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            Response.Redirect("~/Search.aspx?txtSearch=" & Server.UrlEncode(txtSearchH.Text))
        End Sub
    </script>
    
    ...
    
    <asp:TextBox ID="txtSearchH" runat="server"></asp:TextBox>
                        <asp:Button ID="Button1" runat="server" Text="Search" 
                            PostBackUrl="~/Search.aspx?txtSearch=@txtSearchH" 
                            onclick="Button1_Click" />


     


    and here is the code in the search.aspx page:

    <%@ Page Title="Search" Language="VB" MasterPageFile="~/LayoutMasterPage.master" %>
    
    <script runat="server">
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim txtSearch As TextBox = DirectCast(Page.Master.FindControl("txtSearchH"), TextBox)
           
        End Sub
    </script>
    
    ...
    
    <asp:TextBox ID="txtSearch" runat="server" AutoPostBack="True" ></asp:TextBox> 
            <asp:Button ID="Button1" runat="server" Text="Search" />


    but when i enter a value into the textbox in home page, it loads search, and nothing happens/appears/searches.

  • Re: Masterpage search textbox to search.aspx textbox- how?

    07-03-2009, 12:00 AM
    Answer
    • Contributor
      5,086 point Contributor
    • RickNZ
    • Member since 01-01-2009, 8:43 AM
    • Nelson, New Zealand
    • Posts 854

    Er, well, let's see:

    1. Your PostBackUrl should not include the query string.  That will be added for you.

    2. By posting back to another page, you don't need the onclick handler or the page redirect.

    3. In the cross-postback case, your search page will be receiving the search details in a query string, so you don't need to access the master page.

    4. You will need some logic to differentiate between a postback from another page and one from the search page itself, and pick up the arguments from the corresponding place: from the query string or from the txtSearch TextBox.


  • Re: Masterpage search textbox to search.aspx textbox- how?

    07-03-2009, 4:25 AM
    Answer
    • All-Star
      16,933 point All-Star
    • Dave Sussman
    • Member since 06-17-2002, 11:53 AM
    • UK
    • Posts 2,345
    • ASPInsiders
      TrustedFriends-MVPs

    You can do this in one of two ways:

    1. Use a standard postback event and redirect with a query string.
    2. Use cross page posting.

    At the moment you've got a combination of the two. If you use cross-page posting then, as Rick says, remove the query string from the PostbackUrl and remove the onclick handler. In your search page, you then need to use PreviousPage to access the controls. For example, in the Page_Load in search.aspx:

    If PreviousPage IsNot Nothing Then
      Dim txtSearch As TextBox = DirectCast(PreviousPage.FindControl("txtSearchH"), TextBox)

      ' perform the search
    End If

    Using PreviousPage gives you acces to the page that posted to you and will only have a value if PostbackUrl was used; that is, it will be Nothing if youarrive at search.aspx via any other means. One thing you have to be careful of though, is what happens in that previous page's events (PreInit, Init & Load in particular as these are the most commonly used, but essentially every page event up to and including PreRender), because using PreviousPage instantiates the previous page and runs through the partial page lifecycle recreating the page state (and allowing you access to the control contents). This means that on the previous page if you do any heavy processing (eg database access) then that would get run again, and you may not want that (it's a performance issue). So on any page that posts to another via PostbackUrl you should check IsCrossPagePostback. For example:

    Protected Sub Page_Load(...)
      If Not Page.IsPostback AndAlso Not Page.IsCrossPagePostback Then
        ' fetch stuff from database
      End If
    End Sub

    Not that IsCrossPagePostback should ideally be checked in the master page and every content page that causes the cross page postback; you don't have to do it, but should to avoid re-running code that isn't required; filling grids and stuff usually isn't required when all you need is the search term from a textbox.

    If you want to go the query string route, then remove PostbackUrl from the button on the master page, and in the search page just pull the value from the query string:

    Dim searchValue As String = Request.QueryString("txtSearch")

    There's no massive difference between the two.

     

     

  • Re: Masterpage search textbox to search.aspx textbox- how?

    07-03-2009, 6:26 AM
    • Member
      11 point Member
    • nadude
    • Member since 05-15-2009, 8:41 PM
    • Posts 27

     hey guys, thanks for your replies, clarified stuff for me, but still no search results...

    i decided to go the postbackurl route (is a lot simpler for me to understand, the code kinda makes sense). I removed the onclick event on master page, and changed the postbackurl to just "~/search.aspx" without the query stuff. And on page load of the search results i added:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
            If PreviousPage IsNot Nothing Then
                Dim txtSearch As TextBox = DirectCast(PreviousPage.Master.FindControl("txtSearchH"), TextBox)
            End If

    the only problems i can guess are:

    1. txtSearch is set in page_load, and is also the name of the textbox on the searchpage, with a submit button next to it. i'm not sure how, but clicking submit triggers teh grid view to act. i set the gridview select parameters to txtSearch. So maybe, by declaring/setting the value of txtSearch twice on the page, once in page load to a value, then once in search.aspx page:

    <asp:TextBox ID="txtSearch" runat="server" AutoPostBack="True" ></asp:TextBox> 

    causes problems? how can I set the gridviews select control to two values instead of just one?

    2. your message said "perform the search"... not sure how to code that? so far when i got it to work by dragging a button on the page, and it worked its magic... do i need to trigger the gridviews to load in page load also then?

    ABOUT the if page.ispostback, once i get the search working i'll deal with that later... :)

    really appreciate your expertise on this... ;) my site is beginning to thread together into a pretty cool interactive masterpiece! lol

  • Re: Masterpage search textbox to search.aspx textbox- how?

    07-03-2009, 6:48 AM
    Answer
    • All-Star
      16,933 point All-Star
    • Dave Sussman
    • Member since 06-17-2002, 11:53 AM
    • UK
    • Posts 2,345
    • ASPInsiders
      TrustedFriends-MVPs

    1. Just rename the local variable to something else. Or just take the value from the previous page and put it into the current page:

    txtSearch.Text = DirectCast(PreviousPage.Master.FindControl("txtSearchH"), TextBox).Text

    You can then go ahead and run the search based upon the value.

    When I said "run the search" I meant do whatever it is you do that queries the data. If you're using DataSource controls, which I suspect you are fiven that you mean "select parameters" then the data source queries for its data before you've fetched the value, so you'd need to requery the data. Just use the DataBind method of the grid:

    GridView1.DataBind()

     

     

  • Re: Masterpage search textbox to search.aspx textbox- how?

    07-03-2009, 7:59 AM
    • Member
      11 point Member
    • nadude
    • Member since 05-15-2009, 8:41 PM
    • Posts 27

    woooooooooooohoooooooooooooooooooooooooooooooooooooo it works!!!!!!!!! thank you thank you thank you thank you!!!!!!!!!!!

    one little problem though, by activating the postbackurl on the masterpage search button. even if im on the home page, and i click on login on the home page, it used to bring me back to home page after login. now after login, instead of bringing me back to the page that i selected to login from, it always brings me back to the search page.

    how do i stop that from happening?

  • Re: Masterpage search textbox to search.aspx textbox- how?

    07-03-2009, 12:21 PM
    • All-Star
      16,933 point All-Star
    • Dave Sussman
    • Member since 06-17-2002, 11:53 AM
    • UK
    • Posts 2,345
    • ASPInsiders
      TrustedFriends-MVPs

    If you are redirected to the login page because of an authorisation failure, then you are automatically sent back to the page you originally came from. You can actually force this redirection; I think you set the DestinationUrl on the Login control then it always goes to that page.

    Do you have a login box on the master page, or is it a link that redirects to the login page? If so is it just a hyperlink you've added, or are you using the LoginStatus control?

Page 1 of 1 (7 items)