Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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

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;
        }
    });
});

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>

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>

What about, this (vb, sorry):

Get the value from the text box via codebehind, and simply set the postbackurl on the control like this:

dim textval = SourceTextBox.text
dim myparam = "George"

searchbutton.PostBackUrl = "~/search.aspx?myparam=" & myparam

You can just put that in the function that handles the button click, no?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top