Solved

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

Posted on 2011-03-19
6
276 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

856 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