Solved

Redirect to a page concatenating a text field value using javascript.

Posted on 2011-03-19
6
277 Views
Last Modified: 2012-05-11
I have this asp.net code.

I need to redirect to a page
llike
searchresults.aspx?Keyword=[What the user typed in the search box]

I need to do it on the imagebutton below.
<asp:TextBox id="searchTextBox" runat="server" CssClass="water" Text="Zoek"></asp:TextBox>


<asp:ImageButton  ImageUrl="/Style Library/Aurelium/images/layout/header-search-btn.png" ID="srchImage" runat="server" title="Zoek"></asp:ImageButton>
              
0
Comment
Question by:LuisEstebanValencia
  • 3
  • 2
6 Comments
 
LVL 2

Expert Comment

by:michael_madsen
ID: 35172749
Put this in you header (somewhere between <head> and </head>):

<script language="javascript">
function SearchRedirect()
{
    var objSearchTextBox = document.getElementById("searchTextBox");
    document.location.href = "searchresults.aspx?Keyword=" + objSearchTextBox.value;
}
</script>

and then modify you button like:

asp:ImageButton  ImageUrl="/Style Library/Aurelium/images/layout/header-search-btn.png" ID="srchImage" runat="server" title="Zoek" OnClientClick="SearchRedirect();return false;"></asp:ImageButton>
0
 

Author Comment

by:LuisEstebanValencia
ID: 35173235
Hello, it works but I still have a problem.

The sharepoint search results page is in a specific folder
/search/results.aspx

I need that this code works from any place in the website and the code you sent me is to be placed on a masterpage

 document.location.href = "~/search/results.aspx?k=" + objSearchTextBox.value;


the problem is that the ~ token seems that doesnt work in javascript, can you please help me with  this.

Thank you
0
 
LVL 82

Expert Comment

by:leakim971
ID: 35173427
So use the full path :

document.location.href = "http://www.yourdomain.extension/path/to/search/results.aspx?k=" + objSearchTextBox.value;

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:LuisEstebanValencia
ID: 35174799
I cant use the full path, right now I am on development environment and I have somethng as http://mycomputername

when I deploy this to staging or production that wouldnt work
I need a generic way to point to the root using javascript and that I dont have to change code when moved to another environment

thanks
0
 
LVL 2

Accepted Solution

by:
michael_madsen earned 500 total points
ID: 35177085
Sorry for not returning until now.

Please change:

    document.location.href = "searchresults.aspx?Keyword=" + objSearchTextBox.value;

to:

    document.location.href = "/search/results.aspx?k=" + objSearchTextBox.value;

(provided that the full path is: "http://<hostname>/search/results.aspx?k=something")
0
 

Author Closing Comment

by:LuisEstebanValencia
ID: 35179133
perfect
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question