Solved

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

Posted on 2011-03-19
6
275 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

806 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