views:

2171

answers:

5

I am constructing a search page with a textbox and a button for now, and probably a dropdown to filter results later on. I have my button's PostBackUrl set to my search page (~/search.aspx). Is there an easy way to pass the value in the text box to the search page?

A: 

you may be able to use useSubmitBehavior="true" and put a method="get" on the form. that way it will use the browsers submit behavior and will append the values of the textbox's to the query string

John Boker
Isn't it true that an ASP page can only have one form? I remember reading something like that somewhere. There is a form that was generated by visual studio that encapsulates the content inside the body tag.
Anders
right, it can only have one form, the method=get would have to be on that form. all input fields from the form will be put on the query string. this might not work at all, just something to try.
John Boker
Nevermind that must have been something else. Is there an object (like Request.Form or something) that I need to enumerate to get the value of the text box?
Anders
you could do a normal postback with that button, then in the codebehind generate your query string and do a Response.Redirect(...) to "~/search.aspx?" + querystring
John Boker
A: 

You could also use some JavaScript to accomplish this by catching the Enter key keypress event in the textbox field. You could expand this to perform validation of the text in the textbox as well. (This example is using jQuery)

$(document).ready(function(){
    // Event Handlers to allow searching after pressing Enter key
    $("#myTextBoxID").bind("keypress", function(e){
        switch (e.keyCode){
        case (13):
            // Execute code here ...
            break;
        default:
            break;
        }
    });
});
Andy May
+3  A: 

If you have the PostBackUrl set on your button, then the search box field on your first page, and any other form fields on that page, are already being posted to your search page. The trick is getting access to them in the code-behind for your search.aspx page.

if (Page.PreviousPage != null)
{
    TextBox SourceTextBox = 
        (TextBox)Page.PreviousPage.FindControl("TextBox1");
    if (SourceTextBox != null)
    {
        Label1.Text = SourceTextBox.Text;
    }
}

That is one way. There are some shortcuts too, such as using the PreviousPageType directive at the top of your search.aspx page:

<%@ PreviousPageType VirtualPath="~/SourcePage.aspx" %>

More details on how to use that, as well as the first method, can be found here:

http://msdn.microsoft.com/en-us/library/ms178139.aspx

Adam Nofsinger
A: 

Solved the issue, the previous page is "default.aspx", however the control doesn't reside on that page. Since I use master pages, I have to select Master rather than PreviousPage.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  If PreviousPage IsNot Nothing Then
      Dim txtBoxSrc As New TextBox
      txtBoxSrc = CType(Master.FindControl("searchbox"), TextBox)
      If txtBoxSrc IsNot Nothing Then
          MsgBox(txtBoxSrc.Text)
      End If
  End If
End Sub

<div class="gsSearch">
    <asp:TextBox ID="searchbox" runat="server"></asp:TextBox>
    <asp:Button ID="searchbutton" runat="server" Text="search" 
         UseSubmitBehavior="true" PostBackUrl="~/search.aspx" />
</div>
Anders
I'm not familiar with VB.NET code as much, but it looks as if you had that covered, by checking if PreviousPage is null.. and then if txtBoxSrc is null. Maybe it's in your CTYpe.. I don't think you have to box it as a Text box at that point.
stephenbayer
A: 

I have no idea why you would get a null reference in that code, bare with my VB non-knowledge, but I'm going to try to make a slight modification you might be able to try.

I know that the FindControl returns the type Control.. maybe you can wait to box it into a specific type.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  If PreviousPage IsNot Nothing Then
      Dim txtBoxSrc As New Control
      txtBoxSrc = PreviousPage.FindControl("searchbox")
      If txtBoxSrc IsNot Nothing Then
          MsgBox((CType(txtBoxSrc, TextBox)).Text)
      End If
  End If
End Sub

<div class="gsSearch">
    <asp:TextBox ID="searchbox" runat="server"></asp:TextBox>
    <asp:Button ID="searchbutton" runat="server" Text="search" 
         UseSubmitBehavior="true" PostBackUrl="~/search.aspx" />
</div>
stephenbayer